[PATCH eac V2] Add Item entity and functions to implement <list> <item>

Guillaume Vandevelde guillaume.vandevelde at logilab.fr
Thu Aug 22 16:06:53 CEST 2019


# HG changeset patch
# User Guillaume Vandevelde <gvandevelde at logilab.fr>
# Date 1563894538 -7200
#      Tue Jul 23 17:08:58 2019 +0200
# Node ID 03e6bd605f77d92dfa284a8b92201ce499c2003b
# Parent  d5006ffae86da63bb1a09210746c47935db0bc75
# Available At http://hg.logilab.org/review/cubes/eac
#              hg pull http://hg.logilab.org/review/cubes/eac -r 03e6bd605f77
Add Item entity and functions to implement <list> <item>

diff -r d5006ffae86d -r 03e6bd605f77 cubicweb_eac/dataimport.py
--- a/cubicweb_eac/dataimport.py	Fri Jul 26 13:56:10 2019 +0200
+++ b/cubicweb_eac/dataimport.py	Tue Jul 23 17:08:58 2019 +0200
@@ -136,6 +136,22 @@
     return decorator
 
 
+def add_items_for(*etypes):
+    """Add an `items` attribute in ExtEntity's values dictionnary."""
+    def decorator(func):
+        @wraps(func)
+        def wrapper(self, elem):
+            for extentity in func(self, elem):
+                if extentity.etype in etypes:
+                    items = self.parse_items(elem)
+                    if items:
+                        extentity.values['items'] = set([items])
+                        extentity.values['items_format'] = set([u'text/html'])
+                yield extentity
+        return wrapper
+    return decorator
+
+
 def relate_to_record_through(etype, rtype):
     """Add an ``rtype`` relationship from ``etype`` to the imported record."""
     def decorator(func):
@@ -313,6 +329,15 @@
                 values[var_name] = set([text_type(var.text)])
         return values
 
+    def parse_items(self, elem):
+        values = []
+        for list_elem in self._elem_findall(elem, 'eac:list'):
+            list_elem.tag = 'ul'
+            for child in self._elem_findall(list_elem, 'eac:item'):
+                child.tag = 'li'
+            values.append(list_elem)
+        return u"\n".join(etree.tostring(item, encoding='utf-8') for item in values)
+
     def record_visited(self, elem, extentity):
         assert extentity.extid, extentity
         self._visited.setdefault(elem, set([])).add(extentity.extid)
@@ -469,7 +494,8 @@
                 yield extentity
         # structure
         for structure in self._elem_findall(description, 'eac:structureOrGenealogy'):
-            yield self.build_structure(structure)
+            for extentity in self.build_structure(structure):
+                yield extentity
         # function
         for function in self.find_nested(description, 'eac:function', 'eac:functions'):
             for extentity in self.build_function(function):
@@ -567,6 +593,7 @@
     @relate_to_record_through('LegalStatus', 'legal_status_agent')
     @filter_empty
     @add_citations_for('LegalStatus')
+    @add_items_for('LegalStatus')
     @add_dates_for('LegalStatus')
     @equivalent_concept('eac:term', 'LegalStatus')
     def build_legal_status(self, elem, **kwargs):
@@ -584,6 +611,7 @@
     @relate_to_record_through('Mandate', 'mandate_agent')
     @filter_empty
     @add_citations_for('Mandate')
+    @add_items_for('Mandate')
     @add_dates_for('Mandate')
     @equivalent_concept('eac:term', 'Mandate')
     def build_mandate(self, elem, **kwargs):
@@ -620,6 +648,7 @@
     @relate_to_record_through('History', 'history_agent')
     @add_events_for('History')
     @add_citations_for('History')
+    @add_items_for('History')
     @elem_maybe_none
     def build_history(self, elem):
         """Build a `History` external entity."""
@@ -649,18 +678,20 @@
 
     @elem_maybe_none
     @relate_to_record_through('Structure', 'structure_agent')
+    @add_items_for('Structure')
     def build_structure(self, elem):
         """Build a `Structure` external entity."""
         desc, desc_format = self.parse_tag_content(elem)
         if desc:
             values = {'description': set([desc]),
                       'description_format': set([desc_format])}
-            return ExtEntity('Structure', self._gen_extid(), values)
+            yield ExtEntity('Structure', self._gen_extid(), values)
 
     @relate_to_record_through('AgentPlace', 'place_agent')
     @filter_empty
     @add_citations_for('AgentPlace')
     @add_dates_for('AgentPlace')
+    @add_items_for('AgentPlace')
     @equivalent_concept('eac:placeEntry', 'AgentPlace')
     def build_place(self, elem):
         """Build a AgentPlace external entity"""
@@ -693,6 +724,7 @@
     @filter_empty
     @add_citations_for('AgentFunction')
     @add_dates_for('AgentFunction')
+    @add_items_for('AgentFunction')
     @equivalent_concept('eac:term', 'AgentFunction')
     def build_function(self, elem):
         """Build a `AgentFunction`s external entities"""
@@ -705,6 +737,7 @@
     @relate_to_record_through('Occupation', 'occupation_agent')
     @filter_empty
     @add_citations_for('Occupation')
+    @add_items_for('Occupation')
     @add_dates_for('Occupation')
     @equivalent_concept('eac:term', 'Occupation')
     def build_occupation(self, elem):
@@ -717,6 +750,7 @@
 
     @relate_to_record_through('GeneralContext', 'general_context_of')
     @add_citations_for('GeneralContext')
+    @add_items_for('GeneralContext')
     def build_generalcontext(self, elem):
         """Build a `GeneralContext` external entity"""
         content, content_format = self.parse_tag_content(elem)
diff -r d5006ffae86d -r 03e6bd605f77 cubicweb_eac/migration/0.9.0_Any.py
--- a/cubicweb_eac/migration/0.9.0_Any.py	Fri Jul 26 13:56:10 2019 +0200
+++ b/cubicweb_eac/migration/0.9.0_Any.py	Tue Jul 23 17:08:58 2019 +0200
@@ -27,3 +27,8 @@
         E start_date SD, E end_date ED""".format(etype))
     drop_attribute(etype, 'start_date')
     drop_attribute(etype, 'end_date')
+
+for etype in ('GeneralContext', 'Mandate', 'Occupation', 'History',
+              'AgentFunction', 'LegalStatus', 'AgentPlace', 'Structure'):
+    add_attribute(etype, 'items')
+    
diff -r d5006ffae86d -r 03e6bd605f77 cubicweb_eac/schema.py
--- a/cubicweb_eac/schema.py	Fri Jul 26 13:56:10 2019 +0200
+++ b/cubicweb_eac/schema.py	Tue Jul 23 17:08:58 2019 +0200
@@ -38,6 +38,13 @@
     return cls
 
 
+def with_items(cls):
+    """Class decorator adding an `items` attribute to an EntityType"""
+    desc = _('HTML list of elements')
+    cls.add_relation(RichString(description=desc), name='items')
+    return cls
+
+
 Activity.add_relation(String(description=_('the agent responsible for this activity'),
                              indexed=True, fulltextindexed=True), name='agent')
 
@@ -107,6 +114,7 @@
     inlined = True
 
 
+ at with_items
 class AgentFunction(EntityType):
     """The function of an AuthorityRecord"""
     name = String(fulltextindexed=True, internationalizable=True)
@@ -123,6 +131,7 @@
     inlined = True
 
 
+ at with_items
 class AgentPlace(EntityType):
     """Qualified relation between an AuthorityRecord and a PostalAddress"""
     name = String(fulltextindexed=True,
@@ -179,6 +188,7 @@
     inlined = True
 
 
+ at with_items
 class GeneralContext(EntityType):
     """Information about the general social and cultural context of an authority record"""
     content = RichString(fulltextindexed=True)
@@ -263,12 +273,14 @@
     object = 'AuthorityRecord'
 
 
+ at with_items
 class Mandate(EntityType):
     """Reference text coming from an authority"""
     term = String(fulltextindexed=True)
     description = RichString(fulltextindexed=True)
 
 
+ at with_items
 class LegalStatus(EntityType):
     """Information relative to the legal status of an authority"""
     term = String(fulltextindexed=True)
@@ -281,6 +293,7 @@
     description = RichString(fulltextindexed=True)
 
 
+ at with_items
 class History(EntityType):
     """Biographical or historical information"""
     abstract = RichString(fulltextindexed=True)
@@ -301,11 +314,13 @@
     description = _('HistoricalEvent with date for describing an historical event')
 
 
+ at with_items
 class Structure(EntityType):
     """Information about the structure of an authority"""
     description = RichString(fulltextindexed=True)
 
 
+ at with_items
 class Occupation(EntityType):
     term = String(fulltextindexed=True)
     description = RichString(fulltextindexed=True)
diff -r d5006ffae86d -r 03e6bd605f77 test/data/FRAD033_EAC_00001_simplified.xml
--- a/test/data/FRAD033_EAC_00001_simplified.xml	Fri Jul 26 13:56:10 2019 +0200
+++ b/test/data/FRAD033_EAC_00001_simplified.xml	Tue Jul 23 17:08:58 2019 +0200
@@ -223,6 +223,26 @@
 	  </chronList>
 	  <chronList></chronList>
 	  <chronList><chronItem></chronItem></chronList>
+	  <list>
+	    <item>
+	      <span style="font-
+			   style:italic">1450-1950
+	      </span>
+	      (1929)
+	    </item>
+	    <item>
+	      <span style="font-style:italic">Globe
+	      Gliding
+	      </span>
+	      (1930)
+	    </item>
+	    <item>
+	      <span style="font-
+			   style:italic">Gems
+	      </span>
+	      (1931)
+	    </item>
+	  </list>
       </biogHist>
 
       <!--empty biogHist-->
diff -r d5006ffae86d -r 03e6bd605f77 test/test_dataimport.py
--- a/test/test_dataimport.py	Fri Jul 26 13:56:10 2019 +0200
+++ b/test/test_dataimport.py	Tue Jul 23 17:08:58 2019 +0200
@@ -245,6 +245,8 @@
                                           u"L'inspecteur Canardo"])
               ]),
               'text_format': set([u'text/html']),
+              'items': set([u'<ul xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:isbn:1-931666-33-4" xmlns:xlink="http://www.w3.org/1999/xlink">\n\t    <li>\n\t      <span style="font-       style:italic">1450-1950\n\t      </span>\n\t      (1929)\n\t    </li>\n\t    <li>\n\t      <span style="font-style:italic">Globe\n\t      Gliding\n\t      </span>\n\t      (1930)\n\t    </li>\n\t    <li>\n\t      <span style="font-       style:italic">Gems\n\t      </span>\n\t      (1931)\n\t    </li>\n\t  </ul>\n      ']),  # noqa
+              'items_format': set([u'text/html']),
               'history_agent': set(['FRAD033_EAC_00001']),
               'has_citation': set(['21', '22']),
               'has_event': set(['24', '23']),
@@ -571,7 +573,7 @@
                           'localControl': set([54]),
                           'source': set([76]),  # empty.
                           'structureOrGenealogy': set([189]),  # empty.
-                          'biogHist': set([229, 232]),  # empty.
+                          'biogHist': set([249, 252]),  # empty.
                           })
 
     def check_order_entities(self, entities, expected):



More information about the saem-devel mailing list