dataimport/importer.py
changeset 10514 b29d9904482e
parent 10461 37644c518705
child 10809 359cbdf3a515
equal deleted inserted replaced
10513:7bec01a59f92 10514:b29d9904482e
    22 
    22 
    23 Utilities:
    23 Utilities:
    24 
    24 
    25 .. autofunction:: cwuri2eid
    25 .. autofunction:: cwuri2eid
    26 .. autoclass:: RelationMapping
    26 .. autoclass:: RelationMapping
       
    27 .. autofunction:: cubicweb.dataimport.importer.use_extid_as_cwuri
    27 """
    28 """
    28 
    29 
    29 from collections import defaultdict
    30 from collections import defaultdict
    30 import logging
    31 import logging
    31 
    32 
    45         rql += ', X is IN (%s)' % ','.join(etypes)
    46         rql += ', X is IN (%s)' % ','.join(etypes)
    46     if source_eid is not None:
    47     if source_eid is not None:
    47         rql += ', X cw_source S, S eid %(s)s'
    48         rql += ', X cw_source S, S eid %(s)s'
    48         args['s'] = source_eid
    49         args['s'] = source_eid
    49     return dict(cnx.execute(rql, args))
    50     return dict(cnx.execute(rql, args))
       
    51 
       
    52 
       
    53 def use_extid_as_cwuri(extid2eid):
       
    54     """Return a generator of :class:`ExtEntity` objects that will set `cwuri`
       
    55     using entity's extid if the entity does not exist yet and has no `cwuri`
       
    56     defined.
       
    57 
       
    58     `extid2eid` is an extid to eid dictionary coming from an
       
    59     :class:`ExtEntitiesImporter` instance.
       
    60 
       
    61     Example usage:
       
    62 
       
    63     .. code-block:: python
       
    64 
       
    65         importer = SKOSExtEntitiesImporter(cnx, store, import_log)
       
    66         set_cwuri = use_extid_as_cwuri(importer.extid2eid)
       
    67         importer.import_entities(set_cwuri(extentities))
       
    68     """
       
    69     def use_extid_as_cwuri_filter(extentities):
       
    70         for extentity in extentities:
       
    71             if extentity.extid not in extid2eid:
       
    72                 extentity.values.setdefault('cwuri', set([unicode(extentity.extid)]))
       
    73             yield extentity
       
    74     return use_extid_as_cwuri_filter
    50 
    75 
    51 
    76 
    52 class RelationMapping(object):
    77 class RelationMapping(object):
    53     """Read-only mapping from relation type to set of related (subject, object) eids.
    78     """Read-only mapping from relation type to set of related (subject, object) eids.
    54 
    79