[profile gen] Extend element_schema to support default_value and extra_attributes parameters
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 09 Mar 2017 16:22:57 +0100
changeset 2471 198ee8285875
parent 2470 cce71e0d5639
child 2472 5897c8060f8e
[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.
cubicweb_seda/entities/profile_generation.py
--- a/cubicweb_seda/entities/profile_generation.py	Thu Mar 09 16:20:07 2017 +0100
+++ b/cubicweb_seda/entities/profile_generation.py	Thu Mar 09 16:22:57 2017 +0100
@@ -210,7 +210,7 @@
         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.
@@ -225,6 +225,9 @@
         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)
 
@@ -835,11 +838,19 @@
 
     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')
@@ -1375,9 +1386,15 @@
 
 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))
@@ -1386,7 +1403,7 @@
             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