[PATCH eac V3] Add raw_date attribute to DateEntity

Frank Bessou frank.bessou at logilab.fr
Thu Sep 5 16:18:43 CEST 2019


Applied, thanks :)

On 05/09/2019 16:16, Guillaume Vandevelde wrote:
> # HG changeset patch
> # User Guillaume Vandevelde <gvandevelde at logilab.fr>
> # Date 1567614345 -7200
> #      Wed Sep 04 18:25:45 2019 +0200
> # Node ID fde7d0a19cefdaf4cb5978c46904945f0420837a
> # Parent  a4ef144a14a5106f827f3fde11b80bac184cc56b
> # Available At http://hg.logilab.org/review/cubes/eac
> #              hg pull http://hg.logilab.org/review/cubes/eac -r fde7d0a19cef
> Add raw_date attribute to DateEntity
> 
> With the previous system the information contained by the date based tags
> was lost if it wasn't in a standard format.
> 
> Now the text date value in stored under the raw_date attribute.
> 
> diff -r a4ef144a14a5 -r fde7d0a19cef cubicweb_eac/dataimport.py
> --- a/cubicweb_eac/dataimport.py	Thu Sep 05 11:18:02 2019 +0200
> +++ b/cubicweb_eac/dataimport.py	Wed Sep 04 18:25:45 2019 +0200
> @@ -74,6 +74,12 @@
>       return ExtEntity('ExternalUri', uri, {'uri': set(values), 'cwuri': set(values)})
>   
>   
> +def get_elem_text(elem):
> +    if elem is not None and elem.text:
> +        return text_type(elem.text.strip()) or None
> +    return None
> +
> +
>   def filter_none(func):
>       """Filter None value from a generator function."""
>       def wrapped(*args, **kwargs):
> @@ -473,14 +479,30 @@
>                                                   ('alternative_form', 'eac:alternativeForm')))
>           yield ExtEntity('ParallelNames', self._gen_extid(), values)
>   
> +    def raw_date_range(self, date_range_elem):
> +        from_date = self._elem_find(date_range_elem, 'eac:fromDate')
> +        to_date = self._elem_find(date_range_elem, 'eac:toDate')
> +        date_range = [d for d in (get_elem_text(from_date),
> +                                  get_elem_text(to_date)) if d is not None]
> +        return u'-'.join(date_range)
> +
>       @filter_empty
>       def extract_dates_from(self, elem):
>           for date in self._elem_findall(elem, 'eac:date'):
> -            yield ExtEntity('DateEntity', self._gen_extid(),
> -                            {'start_date': set([self.parse_date(date)]),
> -                             'end_date': set([self.parse_date(date)])})
> +            values = {}
> +            parsed_date = self.parse_date(date)
> +            if parsed_date:
> +                values = {'start_date': {parsed_date}}
> +            raw_date = get_elem_text(date)
> +            if raw_date:
> +                values['raw_date'] = {raw_date}
> +            yield ExtEntity('DateEntity', self._gen_extid(), values)
>           for date_range in self._elem_findall(elem, 'eac:dateRange'):
> -            yield ExtEntity('DateEntity', self._gen_extid(), self.parse_daterange(date_range))
> +            values = self.parse_daterange(date_range)
> +            raw_dates = self.raw_date_range(date_range)
> +            if raw_dates:
> +                values['raw_date'] = {raw_dates}
> +            yield ExtEntity('DateEntity', self._gen_extid(), values)
>   
>       @filter_empty
>       @elem_maybe_none
> diff -r a4ef144a14a5 -r fde7d0a19cef cubicweb_eac/migration/0.9.0_Any.py
> --- a/cubicweb_eac/migration/0.9.0_Any.py	Thu Sep 05 11:18:02 2019 +0200
> +++ b/cubicweb_eac/migration/0.9.0_Any.py	Wed Sep 04 18:25:45 2019 +0200
> @@ -10,6 +10,7 @@
>   # Attributes to update
>   add_attribute('EACResourceRelation', 'xml_attributes')
>   add_attribute('EACResourceRelation', 'relation_entry')
> +add_attribute('DateEntity', 'raw_date')
>   
>   add_attribute('History', 'abstract')
>   
> diff -r a4ef144a14a5 -r fde7d0a19cef cubicweb_eac/schema.py
> --- a/cubicweb_eac/schema.py	Thu Sep 05 11:18:02 2019 +0200
> +++ b/cubicweb_eac/schema.py	Wed Sep 04 18:25:45 2019 +0200
> @@ -531,6 +531,7 @@
>       start_date = Date(constraints=[BoundaryConstraint(
>           '<=', Attribute('end_date'), msg=_('start date must be less than end date'))])
>       end_date = Date()
> +    raw_date = String(description=_('Raw data contained in the date type tag'))
>   
>   
>   @xml_wrap
> diff -r a4ef144a14a5 -r fde7d0a19cef test/data/FRAD033_EAC_00001_simplified.xml
> --- a/test/data/FRAD033_EAC_00001_simplified.xml	Thu Sep 05 11:18:02 2019 +0200
> +++ b/test/data/FRAD033_EAC_00001_simplified.xml	Wed Sep 04 18:25:45 2019 +0200
> @@ -164,15 +164,13 @@
>   	    </toDate>
>   	  </dateRange>
>   	  <dateRange localType="open">
> -	    <fromDate
> -		standardDate="1950">1950
> +	    <fromDate>TestData1
>   	    </fromDate>
> -	    <toDate>open
> +	    <toDate>TestData2
>   	    </toDate>
>   	  </dateRange>
>   	  <dateRange localType="open">
> -	    <fromDate
> -		standardDate="1950">1950
> +	    <fromDate>TestData1
>   	    </fromDate>
>   	    <toDate>open
>   	    </toDate>
> diff -r a4ef144a14a5 -r fde7d0a19cef test/test_dataimport.py
> --- a/test/test_dataimport.py	Thu Sep 05 11:18:02 2019 +0200
> +++ b/test/test_dataimport.py	Wed Sep 04 18:25:45 2019 +0200
> @@ -219,15 +219,16 @@
>                 },
>                ),
>               ('DateEntity', _gen_extid(),
> -             {'start_date': set([datetime.date(1949, 1, 1)])
> +             {'start_date': set([datetime.date(1949, 1, 1)]),
> +              'raw_date': set([u'1949-open'])
>                 },
>                ),
>               ('DateEntity', _gen_extid(),
> -             {'start_date': set([datetime.date(1950, 1, 1)])
> +             {'raw_date': set([u'TestData1-TestData2'])
>                 },
>                ),
>               ('DateEntity', _gen_extid(),
> -             {'start_date': set([datetime.date(1950, 1, 1)])
> +             {'raw_date': set([u'TestData1-open'])
>                 },
>                ),
>               ('NameEntry', _gen_extid(),
> @@ -293,7 +294,8 @@
>                ),
>               ('DateEntity', _gen_extid(),
>                {'start_date': set([datetime.date(1234, 1, 1)]),
> -              'end_date': set([datetime.date(3000, 1, 1)])
> +              'end_date': set([datetime.date(3000, 1, 1)]),
> +              'raw_date': set([u'The mystic year!-3000'])
>                 },
>                ),
>               ('Mandate', _gen_extid(),
> @@ -335,8 +337,9 @@
>                 },
>                ),
>               ('DateEntity', _gen_extid(),
> -             {'end_date': [datetime.date(1957, 1, 1)],
> -              'start_date': [datetime.date(1957, 1, 1)]}
> +             {'start_date': set([datetime.date(1957, 1, 1)]),
> +              'raw_date': set([u'1957'])
> +              },
>                ),
>               ('HistoricalEvent', _gen_extid(),
>                {'date_relation': set(['37']),
> @@ -345,8 +348,10 @@
>                 },
>                ),
>               ('DateEntity', _gen_extid(),
> -             {'end_date': [datetime.date(1961, 1, 1)],
> -              'start_date': [datetime.date(1960, 1, 1)]}
> +             {'end_date': set([datetime.date(1961, 1, 1)]),
> +              'start_date': set([datetime.date(1960, 1, 1)]),
> +              'raw_date': set([u'1960-1961'])
> +              },
>                ),
>               ('Citation', _gen_extid(),
>                {'uri': set(['http://www.assemblee-nationale.fr/histoire/images-decentralisation/'
> @@ -413,7 +418,8 @@
>                ),
>               ('DateEntity', _gen_extid(),
>                {'start_date': set([datetime.date(1987, 1, 1)]),
> -              'end_date': set([datetime.date(2099, 1, 1)])
> +              'end_date': set([datetime.date(2099, 1, 1)]),
> +              'raw_date': set([u'1987-2099'])
>                 },
>                ),
>               ('Citation', _gen_extid(),
> @@ -450,7 +456,8 @@
>                ),
>               ('DateEntity', _gen_extid(),
>                {'start_date': set([datetime.date(2008, 1, 1)]),
> -              'end_date': set([datetime.date(2099, 1, 1)])
> +              'end_date': set([datetime.date(2099, 1, 1)]),
> +              'raw_date': set([u'2008-2099'])
>                 },
>                ),
>               ('ExternalUri', 'whatever',
> @@ -472,7 +479,8 @@
>                ),
>               ('DateEntity', _gen_extid(),
>                {'start_date': set([datetime.date(1917, 1, 1)]),
> -              'end_date': set([datetime.date(2009, 1, 1)])
> +              'end_date': set([datetime.date(2009, 1, 1)]),
> +              'raw_date': set([u'1917-2009'])
>                 },
>                ),
>               ('ChronologicalRelation', _gen_extid(),
> @@ -487,7 +495,8 @@
>                 },
>                ),
>               ('DateEntity', _gen_extid(),
> -             {'start_date': set([datetime.date(2042, 1, 1)])
> +             {'start_date': set([datetime.date(2042, 1, 1)]),
> +              'raw_date': set([u'2042'])
>                 },
>                ),
>               ('IdentityRelation', _gen_extid(),
> @@ -500,7 +509,9 @@
>                                  u'xmlns:xlink="http://www.w3.org/1999/xlink">hips</gloups>'])}
>                ),
>               ('DateEntity', _gen_extid(),
> -             {'start_date': [datetime.date(2042, 1, 1)]}
> +             {'start_date': [datetime.date(2042, 1, 1)],
> +              'raw_date': set([u'2042'])
> +              },
>                ),
>               ('FamilyRelation', _gen_extid(),
>                {'date_relation': ['60'],
> @@ -510,7 +521,9 @@
>                ),
>               ('DateEntity', _gen_extid(),
>                {'end_date': [datetime.date(2009, 1, 1)],
> -              'start_date': [datetime.date(1917, 1, 1)]}
> +              'start_date': [datetime.date(1917, 1, 1)],
> +              'raw_date': set([u'1917-2009'])
> +              },
>                ),
>               ('AssociationRelation', _gen_extid(),
>                {'association_from': set(['authorityrecord-FRAD033_EAC_00001']),
> @@ -537,7 +550,8 @@
>                ),
>               ('DateEntity', _gen_extid(),
>                {'start_date': set([datetime.date(1673, 1, 1)]),
> -              'end_date': set([datetime.date(1963, 1, 1)])
> +              'end_date': set([datetime.date(1963, 1, 1)]),
> +              'raw_date': set([u'1673-1963'])
>                 },
>                ),
>               ('EACFunctionRelation', _gen_extid(),
> @@ -588,7 +602,8 @@
>                ),
>               ('DateEntity', _gen_extid(),
>                {'start_date': set([datetime.date(1922, 1, 1)]),
> -              'end_date': set([datetime.date(2001, 1, 1)])
> +              'end_date': set([datetime.date(2001, 1, 1)]),
> +              'raw_date': set([u'1922-2001'])
>                 },
>                ),
>               ('EACFunctionRelation', _gen_extid(),
> @@ -607,7 +622,8 @@
>                ),
>               ('DateEntity', _gen_extid(),
>                {'start_date': set([datetime.date(1922, 1, 1)]),
> -              'end_date': set([datetime.date(2001, 1, 1)])
> +              'end_date': set([datetime.date(2001, 1, 1)]),
> +              'raw_date': set([u'1922-2001'])
>                 },
>                ),
>               ('EACFunctionRelation', _gen_extid(),
> @@ -626,7 +642,8 @@
>                ),
>               ('DateEntity', _gen_extid(),
>                {'start_date': set([datetime.date(1922, 1, 1)]),
> -              'end_date': set([datetime.date(2001, 1, 1)])
> +              'end_date': set([datetime.date(2001, 1, 1)]),
> +              'raw_date': set([u'1922-2001'])
>                 },
>                ),
>               ('ExternalUri', 'ONLY_XLINK',
> @@ -695,11 +712,11 @@
>                             'recordId': set([8]),
>                             'maintenanceAgency': set([16]),
>                             'languageDeclaration': set([21]),
> -                          'languageUsed': set([188, 195]),
> +                          'languageUsed': set([186, 193]),
>                             'localControl': set([54]),
>                             'source': set([76]),  # empty.
> -                          'structureOrGenealogy': set([268]),  # empty.
> -                          'biogHist': set([328, 331]),  # empty.
> +                          'structureOrGenealogy': set([266]),  # empty.
> +                          'biogHist': set([326, 329]),  # empty.
>                             })
>   
>       def check_order_entities(self, entities, expected):
> 

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



More information about the saem-devel mailing list