[PATCH 1 of 2 eac V2] Add `values_from_xpath` function

Guillaume Vandevelde guillaume.vandevelde at logilab.fr
Fri Aug 9 12:41:47 CEST 2019


# HG changeset patch
# User Guillaume Vandevelde <gvandevelde at logilab.fr>
# Date 1564500696 -7200
#      Tue Jul 30 17:31:36 2019 +0200
# Node ID f5f1fbdaf20ebc0ae599c98deb47453cb265c267
# Parent  fb45891c263f42f74b33c971ed39bfe5c4972fd6
# Available At http://hg.logilab.org/review/cubes/eac
#              hg pull http://hg.logilab.org/review/cubes/eac -r f5f1fbdaf20e
Add `values_from_xpath` function

This function automate the recurent check of the values.

diff -r fb45891c263f -r f5f1fbdaf20e cubicweb_eac/dataimport.py
--- a/cubicweb_eac/dataimport.py	Fri Aug 09 09:44:49 2019 +0200
+++ b/cubicweb_eac/dataimport.py	Tue Jul 30 17:31:36 2019 +0200
@@ -293,6 +293,14 @@
             return trace_extentity(self)(attr)
         return attr
 
+    def values_from_xpaths(self, elem, name_path_tuples, values=None):
+        values = values or {}
+        for var_name, var_path in name_path_tuples:
+            var = self._elem_find(elem, var_path)
+            if var is not None and var.text:
+                values[var_name] = set([text_type(var.text)])
+        return values
+
     def record_visited(self, elem, extentity):
         assert extentity.extid, extentity
         self._visited.setdefault(elem, set([])).add(extentity.extid)
diff -r fb45891c263f -r f5f1fbdaf20e test/test_dataimport.py
--- a/test/test_dataimport.py	Fri Aug 09 09:44:49 2019 +0200
+++ b/test/test_dataimport.py	Tue Jul 30 17:31:36 2019 +0200
@@ -23,6 +23,7 @@
 import sys
 import unittest
 
+from lxml import etree
 from six import reraise
 from six.moves import map
 
@@ -32,6 +33,16 @@
 
 from cubicweb_eac import dataimport, testutils
 
+XML_TEST = """
+<note>
+<to>Tove</to>
+<from>Jani</from>
+<heading>Reminder</heading>
+<body>Hey!</body>
+<empty></empty>
+</note>
+"""
+
 
 def mock_(string):
     return string
@@ -431,6 +442,30 @@
                           'biogHist': set([229, 232]),  # empty.
                           })
 
+    def test_values_from_xpath(self):
+        fname = "FRAD033_EAC_00001_simplified.xml"
+        fpath = self.datapath(fname)
+        self.root = etree.fromstring(XML_TEST)
+        import_log = SimpleImportLog(fpath)
+        extid_generator = map(str, count()).next
+        importer = dataimport.EACCPFImporter(fpath, import_log, mock_,
+                                             extid_generator=extid_generator)
+        values = importer.values_from_xpaths(
+            self.root,
+            (('to_value', 'to'),
+             ('from_value', 'from'),
+             ('heading_value', 'heading'),
+             ('body_value', 'body'),
+             ('empty_value', 'empty'))
+        )
+        self.assertEqual(
+            values,
+            {'to_value': set([u'Tove']),
+             'from_value': set([u'Jani']),
+             'heading_value': set([u'Reminder']),
+             'body_value': set([u'Hey!'])}
+        )
+
     def test_mandate_under_mandates(self):
         """In FRAD033_EAC_00003.xml, <mandate> element are within <mandates>."""
         entities = list(self.file_extentities('FRAD033_EAC_00003.xml'))



More information about the saem-devel mailing list