Hello,
Please allow me to first introduce myself. My name is Jeroen van
Meeuwen, and I work for Kolab Systems.
Part of our development for the Kolab Groupware solution[1] is using
XML Schema Definitions to describe the XML format in which Kolab stores
groupware-related information. We're currently working on these XSDs,
and part of the exercise is to provide demonstrative language-specific
bindings.
I've noticed (Trac ticket #99) PyXB does not support the default/fixed
attributes for elements, and while I'm not at all too familiar with XML
and the way to properly implement this functionality, I would like to
propose the attached patch and would appreciate your review/comments.
I suppose a test and demo .xsd file would need to be provided prior to
upstream acceptance?
Thank you in advance!
[1] http://www.kolab.org
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 f4e79eff41eccf68c91808e60d48aa0cfb2148df Mon Sep 17 00:00:00 2001
From: "Jeroen van Meeuwen (Kolab Systems)" <vanmeeu...@kolabsys.com>
Date: Sat, 22 Oct 2011 19:00:08 +0100
Subject: [PATCH] First attempt at supporting default and fixed attribute
values to an element
---
pyxb/binding/content.py | 48 ++++++++++++++++++++++++++++++++++-----------
pyxb/binding/generate.py | 14 ++++++++++++-
2 files changed, 49 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..e577b43 100644
--- a/pyxb/binding/generate.py
+++ b/pyxb/binding/generate.py
@@ -637,7 +637,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