[PATCH 2 of 5 seda] [profile gen] Extend element_schema to support default_value and extra_attributes parameters

Sylvain Thenault sylvain.thenault at logilab.fr
Thu Mar 9 17:13:49 CET 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1489072977 -3600
#      Thu Mar 09 16:22:57 2017 +0100
# Node ID 198ee8285875273a8a2c5694e5fe9617d9261dec
# Parent  cce71e0d563925352392d5ef6e18d03f839792ab
[profile gen] Extend element_schema to support default_value and extra_attributes parameters

This will be used by latter cset to support Integrity (digest algorithm)
elements in SEDA 0.2.

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
@@ -208,11 +208,11 @@ class RNGMixin(object):
         if minimum == 1:
             return parent
         else:
             return self.element('rng:optional', parent)
 
-    def rng_value(self, element, qualified_datatype, fixed_value=None):
+    def rng_value(self, element, qualified_datatype, fixed_value=None, default_value=None):
         """Given a (etree) schema element, a data type (e.g. 'xsd:token') and an optional fixed
         value, add RNG declaration to the element to declare the datatype and fix the value if
         necessary.
         """
         prefix, datatype = qualified_datatype.split(':')
@@ -223,10 +223,13 @@ class RNGMixin(object):
             # schema (as agape v1 was doing).
             datatype = 'string'
         type_attrs = {'type': datatype}
         if fixed_value is not None:
             self.element('rng:value', element, type_attrs, text=fixed_value)
+        elif default_value is not None:
+            element.attrib[self.qname('a:defaultValue')] = default_value
+            self.element('rng:data', element, type_attrs)
         else:
             self.element('rng:data', element, type_attrs)
 
 
 class SEDA2ExportAdapter(EntityAdapter):
@@ -833,15 +836,23 @@ class SEDA1XSDExport(SEDA2XSDExport):
         'version': '1.0',
     }
 
     concepts_language = 'seda-1'
 
-    def element_schema(self, parent, name, xsd_type=None, fixed_value=None, cardinality='1',
-                       documentation=None, xsd_attributes=()):
+    def element_schema(self, parent, name, xsd_type=None,
+                       fixed_value=None, default_value=None,
+                       cardinality='1', documentation=None,
+                       xsd_attributes=(), extra_attributes=None):
+        assert not (fixed_value and default_value), \
+            'only one of fixed_value or default_value may be specified'
         attributes = {'name': name}
+        if extra_attributes is not None:
+            attributes.update(extra_attributes)
         if fixed_value is not None:
             attributes['fixed'] = text_type(fixed_value)
+        elif default_value is not None:
+            attributes['default'] = text_type(default_value)
         if xsd_type is not None and not xsd_attributes:
             attributes['type'] = xsd_type
         assert cardinality in ('0..1', '0..n', '1', '1..n')
         if cardinality != '1':
             if cardinality[0] == '0':
@@ -1373,22 +1384,28 @@ class SEDA02XSDExport(SEDA1XSDExport):
     kw_content_tag_attributes = [XAttr('languageID', 'xsd:language')]
 
 
 class OldSEDARNGExportMixin(RNGMixin):
 
-    def element_schema(self, parent, name, xsd_type=None, fixed_value=None, cardinality='1',
-                       documentation=None, xsd_attributes=()):
+    def element_schema(self, parent, name, xsd_type=None,
+                       fixed_value=None, default_value=None,
+                       cardinality='1', documentation=None,
+                       xsd_attributes=(), extra_attributes=None):
+        assert not (fixed_value and default_value), \
+            'only one of fixed_value or default_value may be specified'
         attributes = {'name': name}
+        if extra_attributes is not None:
+            attributes.update(extra_attributes)
         if documentation:
             attributes['documentation'] = documentation
         parent = self.rng_element_parent(parent, *minmax_cardinality(cardinality))
         element = self.element('rng:element', parent, attributes)
         for xattr in xsd_attributes:
             self.attribute_schema(element, xattr)
 
         if xsd_type is not None:
-            self.rng_value(element, xsd_type, fixed_value)
+            self.rng_value(element, xsd_type, fixed_value, default_value)
         else:
             assert fixed_value is None
         return element
 
     def attribute_schema(self, parent, xattr):


More information about the saem-devel mailing list