add use_extid_as_cwuri ext entity transform, that will be often necessary and not so easy to write at once
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 26 Jun 2015 15:00:07 +0200
changeset 10514 b29d9904482e
parent 10513 7bec01a59f92
child 10515 70ed2067fdb5
add use_extid_as_cwuri ext entity transform, that will be often necessary and not so easy to write at once
dataimport/importer.py
dataimport/test/unittest_importer.py
--- a/dataimport/importer.py	Fri Jun 26 11:15:25 2015 +0200
+++ b/dataimport/importer.py	Fri Jun 26 15:00:07 2015 +0200
@@ -24,6 +24,7 @@
 
 .. autofunction:: cwuri2eid
 .. autoclass:: RelationMapping
+.. autofunction:: cubicweb.dataimport.importer.use_extid_as_cwuri
 """
 
 from collections import defaultdict
@@ -49,6 +50,30 @@
     return dict(cnx.execute(rql, args))
 
 
+def use_extid_as_cwuri(extid2eid):
+    """Return a generator of :class:`ExtEntity` objects that will set `cwuri`
+    using entity's extid if the entity does not exist yet and has no `cwuri`
+    defined.
+
+    `extid2eid` is an extid to eid dictionary coming from an
+    :class:`ExtEntitiesImporter` instance.
+
+    Example usage:
+
+    .. code-block:: python
+
+        importer = SKOSExtEntitiesImporter(cnx, store, import_log)
+        set_cwuri = use_extid_as_cwuri(importer.extid2eid)
+        importer.import_entities(set_cwuri(extentities))
+    """
+    def use_extid_as_cwuri_filter(extentities):
+        for extentity in extentities:
+            if extentity.extid not in extid2eid:
+                extentity.values.setdefault('cwuri', set([unicode(extentity.extid)]))
+            yield extentity
+    return use_extid_as_cwuri_filter
+
+
 class RelationMapping(object):
     """Read-only mapping from relation type to set of related (subject, object) eids.
 
--- a/dataimport/test/unittest_importer.py	Fri Jun 26 11:15:25 2015 +0200
+++ b/dataimport/test/unittest_importer.py	Fri Jun 26 15:00:07 2015 +0200
@@ -18,12 +18,13 @@
 
 from collections import defaultdict
 
-from logilab.common.testlib import unittest_main
+from logilab.common.testlib import TestCase, unittest_main
 
 from cubicweb import ValidationError
 from cubicweb.devtools.testlib import CubicWebTC
 from cubicweb.dataimport import RQLObjectStore, ucsvreader
-from cubicweb.dataimport.importer import ExtEntity, ExtEntitiesImporter, SimpleImportLog, RelationMapping
+from cubicweb.dataimport.importer import (ExtEntity, ExtEntitiesImporter, SimpleImportLog,
+                                          RelationMapping, use_extid_as_cwuri)
 
 
 class RelationMappingTC(CubicWebTC):
@@ -148,6 +149,22 @@
             self.assertEqual(entity.nom, u'Richelieu Cardinal')
 
 
+class UseExtidAsCwuriTC(TestCase):
+
+    def test(self):
+        personne = ExtEntity('Personne', 1, {'nom': set([u'de la lune']),
+                                             'prenom': set([u'Jean'])})
+        mapping = {}
+        set_cwuri = use_extid_as_cwuri(mapping)
+        list(set_cwuri((personne,)))
+        self.assertIn('cwuri', personne.values)
+        self.assertEqual(personne.values['cwuri'], set(['1']))
+        mapping[1] = 'whatever'
+        personne.values.pop('cwuri')
+        list(set_cwuri((personne,)))
+        self.assertNotIn('cwuri', personne.values)
+
+
 def extentities_from_csv(fpath):
     """Yield ExtEntity read from `fpath` CSV file."""
     with open(fpath) as f: