[dataimport] Add explanation about why external entities can't be inserted
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 19 Apr 2017 09:05:10 +0200
changeset 12173 d13fc09301bd
parent 12172 8bb323eb6859
child 12174 02b8325720d6
[dataimport] Add explanation about why external entities can't be inserted By default after the import processed the importer indicates which external entities can't be inserted because they are missing dependency data (other entities, used in inlined or mandatory relations). It usually helps there to find out which extids / relations are missing, so add this to the log.
cubicweb/dataimport/importer.py
--- a/cubicweb/dataimport/importer.py	Thu Apr 20 17:22:36 2017 +0200
+++ b/cubicweb/dataimport/importer.py	Wed Apr 19 09:05:10 2017 +0200
@@ -253,6 +253,21 @@
                 entity_dict[rtype] = extid2eid[entity_dict[rtype]]
         return True
 
+    def why_not_ready(self, extid2eid):
+        """Return some text explaining why this ext entity is not ready.
+        """
+        assert self._schema, 'prepare() method should be called first on %s' % self
+        # as .prepare has been called, we know that .values only contains subject relation *type* as
+        # key (no more (rtype, role) tuple)
+        schema = self._schema
+        entity_dict = self.values
+        for rtype in entity_dict:
+            rschema = schema.rschema(rtype)
+            if not rschema.final:
+                if entity_dict[rtype] not in extid2eid:
+                    return u'inlined relation %s is not present (%s)' % (rtype, entity_dict[rtype])
+        raise AssertionError('this external entity seems actually ready for insertion')
+
 
 class ExtEntitiesImporter(object):
     """This class is responsible for importing externals entities, that is instances of
@@ -413,7 +428,8 @@
                     "missing data?"]
             for ext_entities in queue.values():
                 for ext_entity in ext_entities:
-                    msgs.append(str(ext_entity))
+                    msg = '{}: {}'.format(ext_entity, ext_entity.why_not_ready(self.extid2eid))
+                    msgs.append(msg)
             map(error, msgs)
             if self.raise_on_error:
                 raise Exception('\n'.join(msgs))