[PATCH eac] Add new method `values_from_attrib`

Frank Bessou frank.bessou at logilab.fr
Tue Aug 20 15:33:58 CEST 2019



On 20/08/2019 10:18, Guillaume Vandevelde wrote:
> # HG changeset patch
> # User Guillaume Vandevelde <gvandevelde at logilab.fr>
> # Date 1565346949 -7200
> #      Fri Aug 09 12:35:49 2019 +0200
> # Node ID 42df44a5dbad4cb1469b12b25d21752e8c9d37c3
> # Parent  081a77bad3f559fa162409e93c431a8ccdbc50f7
> # Available At http://hg.logilab.org/review/cubes/eac
> #              hg pull http://hg.logilab.org/review/cubes/eac -r 42df44a5dbad
> Add new method `values_from_attrib`
> 
> diff -r 081a77bad3f5 -r 42df44a5dbad 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, name_attrib_tuple):


name_attrib_tuple => attribs_names ? :)


> +        values = {}
> +        for var_name, attrib_name in name_attrib_tuple:
> +            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 081a77bad3f5 -r 42df44a5dbad 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>
> @@ -550,7 +550,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)
> @@ -571,6 +571,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>."""
> 

-- 
Frank Bessou
Logilab         https://www.logilab.fr



More information about the saem-devel mailing list