[PATCH eac] EACFunctionRelation add json dump support

Frank Bessou frank.bessou at logilab.fr
Mon Aug 19 14:54:17 CEST 2019



On 19/08/2019 12:16, Guillaume Vandevelde wrote:
> # HG changeset patch
> # User Guillaume Vandevelde <gvandevelde at logilab.fr>
> # Date 1565691187 -7200
> #      Tue Aug 13 12:13:07 2019 +0200
> # Node ID 62abf97445862a29d98cbc9f36fe45ed565e4de0
> # Parent  20e3f13d7250e65c0a789d3105201131d8eaac30
> # Available At http://hg.logilab.org/review/cubes/eac
> #              hg pull http://hg.logilab.org/review/cubes/eac -r 62abf9744586
> EACFunctionRelation add json dump support

Add json dump support to EACFunctionRelation


This changeset renames attributes into xml_attributes AND introduce the 
JsonAttrEntity class.

> 
> diff -r 20e3f13d7250 -r 62abf9744586 cubicweb_eac/dataimport.py
> --- a/cubicweb_eac/dataimport.py	Tue Jul 30 17:31:37 2019 +0200
> +++ b/cubicweb_eac/dataimport.py	Tue Aug 13 12:13:07 2019 +0200
> @@ -866,9 +866,11 @@
>               elem,
>               (('place_entry', 'eac:placeEntry'),
>                ('relation_entry', 'eac:relationEntry'))))
> -        attrib = sorted(dict(elem.attrib).items())
> -        if attrib:
> -            values.update({'attributes': set([text_type(attrib)])})
> +        if elem.attrib:
> +            attributes = json.dumps(dict(elem.attrib), sort_keys=True)

Explain the change of data structure from tuple to dict in commit 
message. :)

> +        else:
> +            attributes = json.dumps({})
> +        values.update({'xml_attributes': set([text_type(attributes)])})
>           yield ExtEntity('EACFunctionRelation', self._gen_extid(), values)
>   
>       @add_xml_wrap_for('EACResourceRelation')
> diff -r 20e3f13d7250 -r 62abf9744586 cubicweb_eac/entities.py
> --- a/cubicweb_eac/entities.py	Tue Jul 30 17:31:37 2019 +0200
> +++ b/cubicweb_eac/entities.py	Tue Aug 13 12:13:07 2019 +0200
> @@ -112,7 +112,14 @@
>           return self.hierarchical_child[0]
>   
>   
> -class EACResourceRelation(AnyEntity):
> +class JsonAttrEntity(object):

We generally use Mixin in mixins classname.
JsonAttrsMixin ?

> +
> +    @property
> +    def json_attrs(self):
> +        return json.loads(self.xml_attributes)
> +
> +
> +class EACResourceRelation(AnyEntity, JsonAttrEntity):
>       __regid__ = 'EACResourceRelation'
>       fetch_attrs, cw_fetch_order = fetch_config(('agent_role',
>                                                   'attributes',
> @@ -127,10 +134,6 @@
>       def resource(self):
>           return self.resource_relation_resource[0]
>   
> -    @property
> -    def json_attrs(self):
> -        return json.loads(self.xml_attributes)
> -
>       def dc_title(self):
>           agent_title = self.record.dc_title()
>           if self.agent_role:
> @@ -143,7 +146,7 @@
>                    'to': resource_title})
>   
>   
> -class EACFunctionRelation(AnyEntity):
> +class EACFunctionRelation(AnyEntity, JsonAttrEntity):
>       __regid__ = 'EACFunctionRelation'
>       fetch_attrs, cw_fetch_order = fetch_config(('r_type',
>                                                   'description',

attributes --> xml_attributes in fetch_config

> diff -r 20e3f13d7250 -r 62abf9744586 cubicweb_eac/schema.py
> --- a/cubicweb_eac/schema.py	Tue Jul 30 17:31:37 2019 +0200
> +++ b/cubicweb_eac/schema.py	Tue Aug 13 12:13:07 2019 +0200
> @@ -379,7 +379,7 @@
>       description = RichString(fulltextindexed=True)
>       relation_entry = String(fulltextindexed=True)
>       place_entry = String(fulltextindexed=True)
> -    attributes = String(fulltextindexed=True)
> +    xml_attributes = String(fulltextindexed=True)
>   
>   
>   class function_relation_agent(RelationDefinition):
> diff -r 20e3f13d7250 -r 62abf9744586 test/test_dataimport.py
> --- a/test/test_dataimport.py	Tue Jul 30 17:31:37 2019 +0200
> +++ b/test/test_dataimport.py	Tue Aug 13 12:13:07 2019 +0200
> @@ -429,10 +429,10 @@
>                                  u'">Boschiero\n\t      </namePart>\n\t      <role>\n\t\t<roleTerm'
>                                  u' type="text">autore\n\t\t</roleTerm>\n\t      </role>\n\t'
>                                  u'    </name>\n\t  </mods>\n\t']),
> -              'attributes': set([u"[('{http://www.w3.org/1999/xlink}actuate', 'onLoad'),"
> -                                 u" ('{http://www.w3.org/1999/xlink}arcrole',"
> -                                 u" 'http://test_arcrole.lol.com'), ('{http://www.w3."
> -                                 u"org/1999/xlink}role', 'http://test_role.lmao.com')]"])
> +              'xml_attributes': set([u'{"{http://www.w3.org/1999/xlink}actuate": "onLoad", "{http:'
> +                                     u'//www.w3.org/1999/xlink}arcrole": "http://test_arcrole.lol.c'
> +                                     u'om", "{http://www.w3.org/1999/xlink}role": "http://'
> +                                     u'test_role.lmao.com"}'])
>                 },
>                ),
>               ('EACFunctionRelation', _gen_extid(),
> @@ -448,7 +448,8 @@
>                 'r_type': set([u'controls']),
>                 'description_format': set([u'text/html']),
>                 'relation_entry': set([u'Establishment and abolishment\n\tof schools\n\t']),
> -              'start_date': set([datetime.date(1922, 1, 1)])
> +              'start_date': set([datetime.date(1922, 1, 1)]),
> +              'xml_attributes': set([u'{}'])
>                 },
>                ),
>               ('EACFunctionRelation', _gen_extid(),
> @@ -462,7 +463,8 @@
>                 'end_date': set([datetime.date(2001, 1, 1)]),
>                 'description_format': set([u'text/html']),
>                 'relation_entry': set([u'Some relation entry\n          ']),
> -              'start_date': set([datetime.date(1922, 1, 1)])
> +              'start_date': set([datetime.date(1922, 1, 1)]),
> +              'xml_attributes': set([u'{}'])
>                 },
>                ),
>               ('EACFunctionRelation', _gen_extid(),
> @@ -476,7 +478,8 @@
>                 'end_date': set([datetime.date(2001, 1, 1)]),
>                 'description_format': set([u'text/html']),
>                 'relation_entry': set([u'Some relation entry\n          ']),
> -              'start_date': set([datetime.date(1922, 1, 1)])
> +              'start_date': set([datetime.date(1922, 1, 1)]),
> +              'xml_attributes': set([u'{}'])
>                 },
>                ),
>               ('ExternalUri', 'ONLY_XLINK',
> @@ -777,11 +780,13 @@
>                                                   "{http://www.w3.org/1999/xlink}type": "simple"})
>           rset = cnx.find('EACFunctionRelation', r_type=u'performs')
>           func_relation = rset.one()
> -        self.assertEqual(func_relation.attributes,
> -                         u"[('{http://www.w3.org/1999/xlink}actuate', "
> -                         u"'onLoad'), ('{http://www.w3.org/1999/xlink}arcrole',"
> -                         u" 'http://test_arcrole.lol.com'), ('{http://www.w3.org"
> -                         u"/1999/xlink}role', 'http://test_role.lmao.com')]")
> +        self.assertEqual(func_relation.json_attrs,
> +                         {u'{http://www.w3.org/1999/xlink}actuate':
> +                          u'onLoad',
> +                          u'{http://www.w3.org/1999/xlink}arcrole':
> +                          u'http://test_arcrole.lol.com',
> +                          u'{http://www.w3.org/1999/xlink}role':
> +                          u'http://test_role.lmao.com'})
>           self.assertEqual(func_relation.relation_entry,
>                            u'Alumni communication\n\tmanagement, '
>                            'University of\n\tGlasgow\n\t')
> 

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



More information about the saem-devel mailing list