[PATCH 1 of 2 eac] [dataimport] Fix behaviour vs unexpected agent kind (eac's entityType)

Sylvain Thenault sylvain.thenault at logilab.fr
Thu May 18 16:51:43 CEST 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1495118172 -7200
#      Thu May 18 16:36:12 2017 +0200
# Node ID 1583346d51abe3ab0600c73834e3dcb203c1413c
# Parent  530c2670514c9c7aa583f8a42af8190cd7ffcd95
[dataimport] Fix behaviour vs unexpected agent kind (eac's entityType)

we should not attempt to create a new kind but rather redirect to the
unknown-agent-kind kind, which is created on that purpose.

It seems this was the legacy behaviour but it's not clear why it has changed at
some point. Anyway I fail to see a reason to create new kind while this is an
internal type that should be handled by developpers, not users.

Related to 17079137

diff --git a/cubicweb_eac/dataimport.py b/cubicweb_eac/dataimport.py
--- a/cubicweb_eac/dataimport.py
+++ b/cubicweb_eac/dataimport.py
@@ -510,11 +510,14 @@ class EACCPFImporter(object):
 
     @elem_maybe_none
     def build_agent_kind(self, elem):
         """Build a AgentKind external entity"""
         # Map EAC entity types to our terminolgy.
-        kind = TYPE_MAPPING.get(elem.text, elem.text)
+        kind = TYPE_MAPPING.get(elem.text, u'unknown-agent-kind')
+        if kind == 'unknown-agent-kind':
+            msg = self._('unexpected entity type {}').format(elem.text)
+            self.import_log.record_warning(msg, line=elem.sourceline)
         agentkind_id = 'agentkind/' + kind
         return ExtEntity('AgentKind', agentkind_id, {'name': set([text_type(kind)])})
 
     @elem_maybe_none
     @relate_to_record_through('LegalStatus', 'legal_status_agent')
diff --git a/cubicweb_eac/sobjects.py b/cubicweb_eac/sobjects.py
--- a/cubicweb_eac/sobjects.py
+++ b/cubicweb_eac/sobjects.py
@@ -105,15 +105,11 @@ class EACImportService(Service):
             """Create agent kind when necessary and remove them from the entity stream, allowing to
             set cwuri properly without attempt to update.
             """
             for extentity in extentities:
                 if extentity.etype == 'AgentKind':
-                    if extentity.extid not in extid2eid:
-                        name = next(iter(extentity.values['name']))
-                        kind = self._cw.create_entity('AgentKind', name=name,
-                                                      cwuri=text_type(extentity.extid))
-                        extid2eid[extentity.extid] = kind.eid
+                    assert extentity.extid in extid2eid, 'unexpected agent kind {}'
                 else:
                     yield extentity
 
         extentities = handle_agent_kind(extentities)
         return extentities
diff --git a/test/data/custom_kind2.xml b/test/data/custom_kind2.xml
deleted file mode 100644
--- a/test/data/custom_kind2.xml
+++ /dev/null
@@ -1,23 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!DOCTYPE ead PUBLIC "+//ISBN 1-931666-00-8//DTD ead.dtd (Encoded Archival Description (EAD) Version 2002)//EN" "ead.dtd">
-<eac-cpf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-  xsi:schemaLocation="urn:isbn:1-931666-33-4 http://eac.staatsbibliothek-berlin.de/schema/cpf.xsd"
-  xmlns="urn:isbn:1-931666-33-4"
-  xmlns:xlink="http://www.w3.org/1999/xlink">
-  <control>
-    <recordId>FRAD033_EAC_00002</recordId>
-  </control>
-
-  <cpfDescription>
-    <identity>
-      <entityId>22330001300017</entityId>
-      <entityType>a custom kind</entityType>
-      <nameEntry>
-        <part>Haute-Garonne. Conseil général</part>
-        <authorizedForm>conventionDeclaration</authorizedForm>
-      </nameEntry>
-    </identity>
-
-  </cpfDescription>
-
-</eac-cpf>
diff --git a/test/test_dataimport.py b/test/test_dataimport.py
--- a/test/test_dataimport.py
+++ b/test/test_dataimport.py
@@ -24,10 +24,11 @@ import sys
 import unittest
 
 from six import reraise
 from six.moves import map
 
+from cubicweb import NoResultError
 from cubicweb.dataimport.importer import ExtEntity, SimpleImportLog
 from cubicweb.devtools.testlib import CubicWebTC
 
 from cubicweb_eac import dataimport, testutils
 
@@ -597,15 +598,13 @@ class EACDataImportTC(CubicWebTC):
             self.assertEqual(nb_records_after - nb_records_before, 3)
 
     def test_unknown_kind(self):
         with self.admin_access.repo_cnx() as cnx:
             testutils.eac_import(cnx, self.datapath('custom_kind.xml'))
-            kind = cnx.find('AgentKind', name=u'a custom kind').one()
-            self.assertEqual(kind.cwuri, 'agentkind/a custom kind')
-            self.assertEqual(len(kind.reverse_agent_kind), 1)
-            # test reimport doesn't attempt to recreate this kind
-            testutils.eac_import(cnx, self.datapath('custom_kind2.xml'))
+            self.assertRaises(NoResultError, cnx.find('AgentKind', name=u'a custom kind').one)
+            self.assertEqual(cnx.find('AuthorityRecord').one().agent_kind[0].name,
+                             'unknown-agent-kind')
 
     def test_no_name_entry(self):
         with self.admin_access.repo_cnx() as cnx:
             with self.assertRaises(dataimport.MissingTag) as cm:
                 testutils.eac_import(cnx, self.datapath('no_name_entry.xml'))


More information about the saem-devel mailing list