[PATCH 3 of 3 eac] Modified decorator for child appending

Guillaume Vandevelde guillaume.vandevelde at logilab.fr
Thu Jul 4 11:21:54 CEST 2019


# HG changeset patch
# User Guillaume Vandevelde <gvandevelde at logilab.fr>
# Date 1560762819 -7200
#      Mon Jun 17 11:13:39 2019 +0200
# Node ID 3e2d6229319f256964f46072208034e9089dbedd
# Parent  7049863207c0a5bd19efa5d4291d59229243473b
# Available At http://hg.logilab.org/review/cubes/eac
#              hg pull http://hg.logilab.org/review/cubes/eac -r 3e2d6229319f
Modified decorator for child appending

Modified the `add_citation_for` decorator so it can handle any child appending.
Modified the History child creation and concatenation so it can use this system.

I don't like that the `builders` dictionnary in the `add_child_for` decorator is loaded every time a decorated function is called, but I can't get it back of the `wrapper` function because of the dependency of 'self' in the build functions.

Differential Revision: https://phab.logilab.fr/D3467

diff -r 7049863207c0 -r 3e2d6229319f cubicweb_eac/dataimport.py
--- a/cubicweb_eac/dataimport.py	Fri Jun 14 16:03:54 2019 +0200
+++ b/cubicweb_eac/dataimport.py	Mon Jun 17 11:13:39 2019 +0200
@@ -153,19 +153,22 @@
     return decorator
 
 
-def add_citations_for(etype):
+def add_child_for(etype, relation):
     """Handle import of citation tag for `etype` ExtEntity that is yielded by
     decorated method.
     """
     def decorator(func):
         @wraps(func)
         def wrapper(self, elem):
+            builders = {'has_citation': self.build_citation,
+                        'has_event': self.build_event}
+            build_child = builders[relation]
             for extentity in func(self, elem):
                 if extentity.etype == etype:
-                    for citation in self.build_citation(elem):
+                    for child in build_child(elem):
                         extentity.values.setdefault(
-                            'has_citation', set()).add(citation.extid)
-                        yield citation
+                            relation, set()).add(child.extid)
+                        yield child
                 yield extentity
         return wrapper
     return decorator
@@ -522,7 +525,7 @@
     @elem_maybe_none
     @relate_to_record_through('LegalStatus', 'legal_status_agent')
     @filter_empty
-    @add_citations_for('LegalStatus')
+    @add_child_for('LegalStatus', 'has_citation')
     @equivalent_concept('eac:term', 'LegalStatus')
     def build_legal_status(self, elem, **kwargs):
         """Build a `LegalStatus` external entity.
@@ -545,7 +548,7 @@
     @elem_maybe_none
     @relate_to_record_through('Mandate', 'mandate_agent')
     @filter_empty
-    @add_citations_for('Mandate')
+    @add_child_for('Mandate', 'has_citation')
     @equivalent_concept('eac:term', 'Mandate')
     def build_mandate(self, elem, **kwargs):
         """Build a `Mandate` external entity.
@@ -583,7 +586,8 @@
             yield ExtEntity('Citation', self._gen_extid(), values)
 
     @relate_to_record_through('History', 'history_agent')
-    @add_citations_for('History')
+    @add_child_for('History', 'has_event')
+    @add_child_for('History', 'has_citation')
     @elem_maybe_none
     def build_history(self, elem):
         """Build a `History` external entity."""
@@ -594,40 +598,31 @@
                       'text_format': set([desc_format])}
             if abstract is not None and abstract.text:
                 values['abstract'] = set([text_type(abstract.text)])
-            history_obj = ExtEntity('History', self._gen_extid(), values)
-            # Handle <chronItem>
-            events = self._elem_findall(elem, './/eac:chronItem')
-            if events is not None:
-                events_ent = filter(lambda x: x is not None and x.values,
-                                    [self.build_event(e) for e in events])
-                r_etypes = [e.extid for e in events_ent]
-                map(history_obj.values.setdefault(
-                    'has_event', set()).add, r_etypes)
-                yield history_obj
-                for e in events_ent:
-                    yield e
-            else:
-                yield history_obj
+            yield ExtEntity('History', self._gen_extid(), values)
 
+    @filter_none
+    @filter_empty
+    @elem_maybe_none
     def build_event(self, elem):
         """Build en `Event` external entity."""
-        values = {}
-        date = self._elem_find(elem, 'eac:date')
-        date_range = self._elem_find(elem, 'eac:dateRange')
-        event = self._elem_find(elem, 'eac:event')
-        if event is not None and event.text:
-            sentences = filter((lambda x: x != ''),
-                               map(text_type,
-                               (map(str.strip, event.text.split('\n')))))
-            values['event'] = set(["".join(sentences)])
-        if date is None and date_range:
-            date_range = self.parse_daterange(date_range)
-            values.update(date_range)
-        elif date is not None:
-            values.update({'start_date': set([self.parse_date(date)]),
-                           'end_date': set([self.parse_date(date)])})
-        if values:
-            return ExtEntity('Event', self._gen_extid(), values)
+        citems = self._elem_findall(elem, './/eac:chronItem')
+        if citems is not None:
+            for citem in citems:
+                values = {}
+                date = self._elem_find(citem, 'eac:date')
+                date_range = self._elem_find(citem, 'eac:dateRange')
+                event = self._elem_find(citem, 'eac:event')
+                if event is not None and event.text:
+                    sentences = [text_type(line.strip()) for line in event.text.split('\n')]
+                    filtered = [e for e in sentences if e != '']
+                    values['event'] = set(["".join(filtered)])
+                    if date is None and date_range:
+                        date_range = self.parse_daterange(date_range)
+                        values.update(date_range)
+                    elif date is not None:
+                        values.update({'start_date': set([self.parse_date(date)]),
+                                       'end_date': set([self.parse_date(date)])})
+                    yield ExtEntity('Event', self._gen_extid(), values)
 
     @elem_maybe_none
     @relate_to_record_through('Structure', 'structure_agent')
@@ -641,7 +636,7 @@
 
     @relate_to_record_through('AgentPlace', 'place_agent')
     @filter_empty
-    @add_citations_for('AgentPlace')
+    @add_child_for('AgentPlace', 'has_citation')
     @equivalent_concept('eac:placeEntry', 'AgentPlace')
     def build_place(self, elem):
         """Build a AgentPlace external entity"""
@@ -672,7 +667,7 @@
 
     @relate_to_record_through('AgentFunction', 'function_agent')
     @filter_empty
-    @add_citations_for('AgentFunction')
+    @add_child_for('AgentFunction', 'has_citation')
     @equivalent_concept('eac:term', 'AgentFunction')
     def build_function(self, elem):
         """Build a `AgentFunction`s external entities"""
@@ -684,7 +679,7 @@
 
     @relate_to_record_through('Occupation', 'occupation_agent')
     @filter_empty
-    @add_citations_for('Occupation')
+    @add_child_for('Occupation', 'has_citation')
     @equivalent_concept('eac:term', 'Occupation')
     def build_occupation(self, elem):
         """Build a `Occupation`s external entities"""
@@ -699,7 +694,7 @@
         yield ExtEntity('Occupation', self._gen_extid(), values)
 
     @relate_to_record_through('GeneralContext', 'general_context_of')
-    @add_citations_for('GeneralContext')
+    @add_child_for('GeneralContext', 'has_citation')
     def build_generalcontext(self, elem):
         """Build a `GeneralContext` external entity"""
         content, content_format = self.parse_tag_content(elem)
diff -r 7049863207c0 -r 3e2d6229319f test/test_dataimport.py
--- a/test/test_dataimport.py	Fri Jun 14 16:03:54 2019 +0200
+++ b/test/test_dataimport.py	Mon Jun 17 11:13:39 2019 +0200
@@ -192,10 +192,16 @@
               ]),
               'text_format': set([u'text/html']),
               'history_agent': set(['FRAD033_EAC_00001']),
-              'has_citation': set(['18', '19']),
-              'has_event': set(['16', '17']),
+              'has_citation': set(['16', '17']),
+              'has_event': set(['18', '19']),
              },
             ),
+            ('Citation', _gen_extid(),
+             {'uri': set(['http://www.assemblee-nationale.fr/histoire/images-decentralisation/decentralisation/loi-du-22-decembre-1789-.pdf'])},  # noqa
+            ),
+            ('Citation', _gen_extid(),
+             {'uri': set(['http://pifgadget']), 'note': set(['Voir aussi pifgadget'])},
+            ),
             ('Event', _gen_extid(),
              {'event': [u'Left Mer and moved to the mainland.'
                         u'Worked at various jobs including canecutter'
@@ -209,12 +215,6 @@
               'end_date': set([datetime.date(1961, 1, 1)]),
               'start_date': set([datetime.date(1960, 1, 1)])}
             ),
-            ('Citation', _gen_extid(),
-             {'uri': set(['http://www.assemblee-nationale.fr/histoire/images-decentralisation/decentralisation/loi-du-22-decembre-1789-.pdf'])},  # noqa
-            ),
-            ('Citation', _gen_extid(),
-             {'uri': set(['http://pifgadget']), 'note': set(['Voir aussi pifgadget'])},
-            ),
             ('Structure', _gen_extid(),
              {'description': set([u'<p xmlns="urn:isbn:1-931666-33-4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink">Pour accomplir ses missions ...</p>']),  # noqa
               'description_format': set([u'text/html']),
@@ -349,7 +349,7 @@
             ('ExternalUri', 'http://catalogue.bnf.fr/ark:/12148/cb152418385',
              {'uri': set([u'http://catalogue.bnf.fr/ark:/12148/cb152418385']),
               'cwuri': set([u'http://catalogue.bnf.fr/ark:/12148/cb152418385'])},
-            ),
+           ),
             ('ExternalUri', 'http://pifgadget.com',
              {'uri': set([u'http://pifgadget.com']),
               'cwuri': set([u'http://pifgadget.com'])},
@@ -533,7 +533,8 @@
                          u'and railway labourer.')
         self.assertEqual(rset[1].printable_value('text',
                                                  format=u'text/plain').strip(),
-                         u'Union representative, Townsville-\nMount Isa rail construction project.')
+                         u'Union representative, Townsville-'
+                         u'Mount Isa rail construction project.')
 
     def _check_mandate(self, cnx, record):
         rset = cnx.find('Mandate', mandate_agent=record)



More information about the saem-devel mailing list