[PATCH 2 of 3 seda] [profile scheme] Use the cwuri_url static method to generate URLs for SEDA 2 export

Sylvain Thenault sylvain.thenault at logilab.fr
Thu Mar 30 17:43:18 CEST 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1490887312 -7200
#      Thu Mar 30 17:21:52 2017 +0200
# Node ID 5ac2e82f9ced1b4036fad67340196b0c089ca3c5
# Parent  92eafd2e3edaefc5dfaeacb5c726b5f21c0b6ce9
[profile scheme] Use the cwuri_url static method to generate URLs for SEDA 2 export

consistently with SEDA 1/0.2 export. By default, this method use cwuri. Make it
available through the common base class.

Related to extranet #15934739

diff --git a/cubicweb_seda/entities/profile_generation.py b/cubicweb_seda/entities/profile_generation.py
--- a/cubicweb_seda/entities/profile_generation.py
+++ b/cubicweb_seda/entities/profile_generation.py
@@ -101,17 +101,17 @@ def _concept_value(concept, language):
 def eid2xmlid(eid):
     """Return a value usable as ID/IDREF for the given eid."""
     return 'id' + text_type(eid)
 
 
-def serialize(value):
+def serialize(value, build_url):
     """Return typed `value` as an XSD string."""
     if value is None:
         return None
     if hasattr(value, 'eid'):
         if value.cw_etype == 'ConceptScheme':
-            return value.absolute_url()
+            return build_url(value)
         if value.cw_etype == 'Concept':
             return _concept_value(value, 'seda-2')
         if _internal_reference(value):
             return eid2xmlid(value.eid)
         return None  # intermediary entity
@@ -366,10 +366,18 @@ class SEDA2ExportAdapter(EntityAdapter):
 
     def jumped_element(self, profile_element):
         """Return the last generated element, for insertion of its content."""
         raise NotImplementedError()
 
+    @staticmethod
+    def cwuri_url(entity):
+        """Return "public" URI for the given entity.
+
+        In a staticmethod to ease overriding in subclasses (eg saem).
+        """
+        return entity.cwuri
+
 
 class SEDA2RelaxNGExport(RNGMixin, SEDA2ExportAdapter):
     """Adapter to build a Relax NG representation of a SEDA profile, using SEDA 2.0 specification.
     """
     __regid__ = 'SEDA-2.0.rng'
@@ -469,11 +477,11 @@ class SEDA2RelaxNGExport(RNGMixin, SEDA2
         target_element = self.element('rng:group', parent_element)  # XXX sequence
         to_process[occ.target].append((target_value, target_element))
 
     def element_xmlattribute(self, occ, profile_element, target_value, to_process, card_entity):
         parent_element = self._rng_attribute_parent(occ, card_entity, profile_element)
-        self._rng_attribute(occ.target, parent_element, serialize(target_value))
+        self._rng_attribute(occ.target, parent_element, serialize(target_value, self.cwuri_url))
 
     def element_xmlelement(self, occ, profile_element, target_value, to_process, card_entity):  # noqa
         parent_element = self._rng_element_parent(occ, card_entity, profile_element)
         xselement = occ.target
         attrs = {'documentation': getattr(card_entity, 'user_annotation', None),
@@ -528,11 +536,11 @@ class SEDA2RelaxNGExport(RNGMixin, SEDA2
         elif copy_attributes:
             for attrname, occ in xselement.attributes.items():
                 if attrname in ('id', 'href') or attrname.startswith(('list', 'scheme')):
                     parent_element = self._rng_attribute_parent(occ, None, profile_element)
                     self._rng_attribute(occ.target, parent_element)
-        fixed_value = serialize(value)
+        fixed_value = serialize(value, self.cwuri_url)
         if fixed_value is not None:
             if _internal_reference(value):
                 profile_element.attrib[self.qname('a:defaultValue')] = fixed_value
                 self.element('rng:data', profile_element, {'type': 'NCName'})
             else:
@@ -550,16 +558,16 @@ class SEDA2RelaxNGExport(RNGMixin, SEDA2
             scheme_attr = xselement_scheme_attribute(xselement)
         except KeyError:
             return
         scheme_attr = self.element('rng:attribute', type_element,
                                    attributes={'name': scheme_attr})
-        self.element('rng:value', scheme_attr, text=scheme.absolute_url())
+        self.element('rng:value', scheme_attr, text=self.cwuri_url(scheme))
 
     def fill_organization_element(self, parent_element, value):
         target_element = self.element('rng:element', parent_element, {'name': 'Identifier'})
         if value:
-            self.element('rng:value', target_element, text=value.absolute_url())
+            self.element('rng:value', target_element, text=self.cwuri_url(value))
 
     def _rng_element_parent(self, occ, card_entity, profile_element):
         minimum, maximum = element_minmax_cardinality(occ, card_entity)
         return self.rng_element_parent(profile_element, minimum, maximum)
 
@@ -1098,18 +1106,10 @@ class SEDA1XSDExport(SEDA2ExportAdapter)
 
     def system_id(self, data_object):
         return (data_object.cw_adapt_to('ITreeBase').parent().first_level_choice.content_sequence
                 .system_id)
 
-    @staticmethod
-    def cwuri_url(entity):
-        """Return "public" URI for the given entity.
-
-        In a staticmethod to ease overriding in subclasses (eg saem).
-        """
-        return entity.cwuri
-
 
 class SEDA02XSDExport(SEDA1XSDExport):
     """Adapter to build an XSD representation of a SEDA profile, using SEDA 0.2 specification"""
     __regid__ = 'SEDA-0.2.xsd'
 
diff --git a/test/test_profile_generation.py b/test/test_profile_generation.py
--- a/test/test_profile_generation.py
+++ b/test/test_profile_generation.py
@@ -486,21 +486,21 @@ class SEDA2RNGExportTC(RelaxNGTestMixin,
             kwr = self.get_element(profile, 'KeywordReference')
             self.assertElementDefinition(kwr, {'name': 'KeywordReference',
                                                'type': 'xsd:token'})
             self.assertXSDAttributes(
                 kwr,
-                [{'name': 'schemeURI', 'fixed': scheme.absolute_url()}])
+                [{'name': 'schemeURI', 'fixed': scheme.cwuri}])
 
             kwr_e.cw_set(seda_keyword_reference_to=kw_type)
             profile = self.profile_etree(transfer)
             kwr = self.get_element(profile, 'KeywordReference')
             self.assertElementDefinition(kwr, {'name': 'KeywordReference',
                                                'type': 'xsd:token',
                                                'fixed': 'theme'})
             self.assertXSDAttributes(
                 kwr,
-                [{'name': 'schemeURI', 'fixed': scheme.absolute_url()}])
+                [{'name': 'schemeURI', 'fixed': scheme.cwuri}])
 
     def test_code_list(self):
         with self.admin_access.client_cnx() as cnx:
             transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
             scheme = cnx.create_entity('ConceptScheme', title=u'Keyword Types')
@@ -509,11 +509,11 @@ class SEDA2RNGExportTC(RelaxNGTestMixin,
                               seda_mime_type_code_list_version_to=scheme)
 
             profile = self.profile_etree(transfer)
             mt_clv = self.get_element(profile, 'MimeTypeCodeListVersion')
             self.assertElementDefinition(mt_clv, {'name': 'MimeTypeCodeListVersion',
-                                                  'fixed': scheme.absolute_url(),
+                                                  'fixed': scheme.cwuri,
                                                   'type': 'xsd:token',
                                                   'minOccurs': '0'})
             # XXX also fix listSchemeURI ?
             self.assertXSDAttributes(
                 mt_clv,
@@ -647,11 +647,11 @@ class SEDAExportFuncTCMixIn(object):
         with self.admin_access.client_cnx() as cnx:
             mda_scheme = cnx.execute('ConceptScheme X').one()
             transfer = cnx.entity_from_eid(self.transfer_eid)
             root = self.profile_etree(transfer)
         self.check_xsd_profile(root, self.datapath('BV2.0_min.xml'),
-                               mda_scheme_url=mda_scheme.absolute_url())
+                               mda_scheme_url=mda_scheme.cwuri)
         # ensure jumped element without content are not there
         self.assertEqual(len(self.get_elements(root, 'Gps')), 0)
         # ensure element with skipped value are not there
         self.assertEqual(len(self.get_elements(root, 'TransactedDate')), 0)
         self.assertProfileDetails(root)


More information about the saem-devel mailing list