On 2011-10-22 19:02, Jeroen van Meeuwen (Kolab Systems) wrote:
Hello,
I would like to propose the attached patch and would
appreciate your review/comments.
I recognize I failed to hit the fixed attribute value to an element
definition, and introduced a syntax error I thus did not discover...
Please find attached an updated patch, against the master branch,
obsoleting the previous one.
Thanks in advance,
Kind regards,
Jeroen van Meeuwen
--
Senior Engineer, Kolab Systems AG
e: vanmeeuwen at kolabsys.com
t: +44 144 340 9500
m: +44 74 2516 3817
w: http://www.kolabsys.com
pgp: 9342 BF08
From 9d57ca502cd7fa3e1e9863e785122d664237bf42 Mon Sep 17 00:00:00 2001
From: "Jeroen van Meeuwen (Ergo Project)"
<jeroen.van.meeu...@ergo-project.org>
Date: Sun, 23 Oct 2011 13:52:37 +0100
Subject: [PATCH] Include support for default/fixed attribute values to
element definitions
---
pyxb/binding/content.py | 48 ++++++++++++++++++++++++++++++++++-----------
pyxb/binding/generate.py | 16 ++++++++++++++-
2 files changed, 51 insertions(+), 13 deletions(-)
diff --git a/pyxb/binding/content.py b/pyxb/binding/content.py
index daebc2d..c4f4a42 100644
--- a/pyxb/binding/content.py
+++ b/pyxb/binding/content.py
@@ -492,7 +492,7 @@ class ElementUse (ContentState_mixin, ContentModel_mixin):
return self.__isPlural
__isPlural = False
- def __init__ (self, name, id, key, is_plural, element_binding=None):
+ def __init__ (self, name, id, key, is_plural, element_binding=None, defaultValue=None, fixedValue=None):
"""Create an ElementUse instance.
@param name: The name by which the element is referenced in the XML
@@ -526,16 +526,17 @@ class ElementUse (ContentState_mixin, ContentModel_mixin):
self.__isPlural = is_plural
self.__elementBinding = element_binding
+ # Perhaps insert a check on setting both the default and fixed?
+ self.__defaultValue = defaultValue
+ self.__fixedValue = fixedValue
+
def defaultValue (self):
- """Return the default value for this element.
+ """Return the default value for this element."""
+ return self.__defaultValue
- @todo: Right now, this returns C{None} for non-plural and an empty
- list for plural elements. Need to support schema-specified default
- values for simple-type content.
- """
- if self.isPlural():
- return []
- return None
+ def fixedValue (self):
+ """Return the fixed value for this element."""
+ return self.__fixedValue
def value (self, ctd_instance):
"""Return the value for this use within the given instance."""
@@ -548,6 +549,12 @@ class ElementUse (ContentState_mixin, ContentModel_mixin):
def set (self, ctd_instance, value):
"""Set the value of this element in the given instance."""
+
+ # If a fixed value has been set, set() should perhaps throw an error.
+ # For now, just ignore.
+ if not self.__fixedValue == None:
+ return self
+
if value is None:
return self.reset(ctd_instance)
assert self.__elementBinding is not None
@@ -667,11 +674,28 @@ class ElementUse (ContentState_mixin, ContentModel_mixin):
values = symbol_set.get(self)
#print 'values %s' % (values,)
if values is None:
- return False
- used = values.pop(0)
+ if self.__defaultValue == None and self.__fixedValue == None:
+ return False
+
+ if not self.__fixedValue == None:
+ if self.isPlural():
+ used = self.__fixedValue.pop(0)
+ else:
+ used = self.__fixedValue
+ elif not self.__defaultValue == None:
+ if self.isPlural():
+ used = self.__defaultValue.pop(0)
+ else:
+ used = self.__defaultValue
+
+ else:
+ used = values.pop(0)
+
output_sequence.append( (self, used) )
- if 0 == len(values):
+
+ if not values == None and 0 == len(values):
del symbol_set[self]
+
return True
def __str__ (self):
diff --git a/pyxb/binding/generate.py b/pyxb/binding/generate.py
index b639316..8763289 100644
--- a/pyxb/binding/generate.py
+++ b/pyxb/binding/generate.py
@@ -522,6 +522,8 @@ def elementDeclarationMap (ed, binding_module, **kw):
template_map['nillable'] = binding_module.literal(ed.nillable(), **kw)
if ed.default():
template_map['defaultValue'] = binding_module.literal(ed.default(), **kw)
+ if ed.fixed():
+ template_map['fixedValue'] = binding_module.literal(ed.fixed(), **kw)
template_map['typeDefinition'] = binding_module.literal(ed.typeDefinition(), **kw)
if ed.substitutionGroupAffiliation():
template_map['substitution_group'] = binding_module.literal(ed.substitutionGroupAffiliation(), **kw)
@@ -637,7 +639,19 @@ class %{ctd} (%{superclass}):
if ed.expandedName().localName() != ef_map['id']:
print 'Element use %s.%s renamed to %s' % (ctd.expandedName(), ed.expandedName(), ef_map['id'])
- definitions.append(templates.replaceInText('''
+
+ if ef_map.has_key('defaultValue'):
+ definitions.append(templates.replaceInText('''
+ # Element %{name} uses Python identifier %{id}
+ %{use} = pyxb.binding.content.ElementUse(%{name_expr}, '%{id}', '%{key}', %{is_plural}%{aux_init}, defaultValue=%{defaultValue})
+''', name_expr=binding_module.literal(ed.expandedName(), **kw), **ef_map))
+ elif ef_map.has_key('fixedValue'):
+ definitions.append(templates.replaceInText('''
+ # Element %{name} uses Python identifier %{id}
+ %{use} = pyxb.binding.content.ElementUse(%{name_expr}, '%{id}', '%{key}', %{is_plural}%{aux_init}, fixedValue=%{fixedValue})
+''', name_expr=binding_module.literal(ed.expandedName(), **kw), **ef_map))
+ else:
+ definitions.append(templates.replaceInText('''
# Element %{name} uses Python identifier %{id}
%{use} = pyxb.binding.content.ElementUse(%{name_expr}, '%{id}', '%{key}', %{is_plural}%{aux_init})
''', name_expr=binding_module.literal(ed.expandedName(), **kw), **ef_map))
--
1.7.6.4
------------------------------------------------------------------------------
The demand for IT networking professionals continues to grow, and the
demand for specialized networking skills is growing even more rapidly.
Take a complimentary Learning@Cisco Self-Assessment and learn
about Cisco certifications, training, and career opportunities.
http://p.sf.net/sfu/cisco-dev2dev
_______________________________________________
pyxb-users mailing list
pyxb-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pyxb-users