[PATCH eac V2] Add new method `values_from_attrib`

Guillaume Vandevelde guillaume.vandevelde at logilab.fr
Tue Aug 20 16:18:12 CEST 2019


# HG changeset patch
# User Guillaume Vandevelde <gvandevelde at logilab.fr>
# Date 1565346949 -7200
#      Fri Aug 09 12:35:49 2019 +0200
# Node ID b8b9f1980b1a4bda9c407d96b58da5d4d5318bb0
# Parent  29d25fe26992cdd05472d0809b8a815abd91e1e9
# Available At http://hg.logilab.org/review/cubes/eac
#              hg pull http://hg.logilab.org/review/cubes/eac -r b8b9f1980b1a
Add new method `values_from_attrib`

diff -r 29d25fe26992 -r b8b9f1980b1a cubicweb_eac/dataimport.py
--- a/cubicweb_eac/dataimport.py	Tue Jul 23 15:09:22 2019 +0200
+++ b/cubicweb_eac/dataimport.py	Fri Aug 09 12:35:49 2019 +0200
@@ -294,6 +294,14 @@
             return trace_extentity(self)(attr)
         return attr
 
+    def values_from_attrib(self, elem, names):
+        values = {}
+        for var_name, attrib_name in names:
+            attrib_value = elem.attrib.get(attrib_name)
+            if attrib_value:
+                values[var_name] = set([text_type(attrib_value)])
+        return values
+
     def values_from_xpaths(self, elem, name_path_tuples):
         values = {}
         for var_name, var_path in name_path_tuples:
diff -r 29d25fe26992 -r b8b9f1980b1a test/test_dataimport.py
--- a/test/test_dataimport.py	Tue Jul 23 15:09:22 2019 +0200
+++ b/test/test_dataimport.py	Fri Aug 09 12:35:49 2019 +0200
@@ -34,7 +34,7 @@
 from cubicweb_eac import dataimport, testutils
 
 XML_TEST = """
-<note>
+<note test="test_value" test2="test2_value">
 <to>Tove</to>
 <from>Jani</from>
 <heading>Reminder</heading>
@@ -554,7 +554,7 @@
         e_lst = get_sorted(expected)
         self.assertEquals(a_lst, e_lst)
 
-    def test_values_from_xpath(self):
+    def test_values_from_functions(self):
         fname = "FRAD033_EAC_00001_simplified.xml"
         fpath = self.datapath(fname)
         self.root = etree.fromstring(XML_TEST)
@@ -575,6 +575,16 @@
              'heading_value': set([u'Reminder']),
              'body_value': set([u'Hey!'])}
         )
+        attrib = importer.values_from_attrib(
+            self.root,
+            (('test_varname', 'test'),
+             ('test_varname_2', 'test2'))
+        )
+        self.assertEqual(
+            attrib,
+            {'test_varname': set([u'test_value']),
+             'test_varname_2': set([u'test2_value'])}
+        )
 
     def test_mandate_under_mandates(self):
         """In FRAD033_EAC_00003.xml, <mandate> element are within <mandates>."""



More information about the saem-devel mailing list