Commit 172c0558 authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

[xsd gen] Refactor to give cardinality entity to visit method

It will be necessary in a later cset to access required information.

To do so:

* visit method are given the occurence XSD element and the entity holding the
  cardinality,

* so initial attributes generation is moving there,

* and element_occ is renamed accordingly (actually a dispatcher depending on the
  type of occ.target)
parent c25313c12675
......@@ -21,6 +21,8 @@ from datetime import date, datetime
from lxml import etree
from pyxst.xml_struct import graph_nodes
from logilab.common import attrdict
from cubicweb.predicates import is_instance
from cubicweb.view import EntityAdapter
......@@ -152,39 +154,56 @@ class SEDA2XSDExport(EntityAdapter):
cardinality = None
# jumped element: give None as target_value but register the generated element for
# later processing
self.element_occ(profile_element, occ, None, to_process,
cardinality=cardinality)
self.dispatch_occ(profile_element, occ, None, to_process,
card_entity=attrdict({'user_cardinality': cardinality}))
to_process[occ.target].append((entity, self._parent_element(profile_element)[-1]))
else:
# print ' values', _path_target_values(entity, path)
for card_entity, target_value in _path_target_values(entity, path):
cardinality = getattr(card_entity, 'user_cardinality', None)
documentation = getattr(card_entity, 'user_annotation', None)
self.element_occ(profile_element, occ, target_value, to_process,
cardinality=cardinality, documentation=documentation)
def element_occ(self, profile_element, occ, target_value, to_process,
cardinality=None, documentation=None):
if isinstance(occ.target, graph_nodes.XMLAttribute):
attrs = xsd_attribute_cardinality(occ, cardinality)
else:
attrs = xsd_element_cardinality(occ, cardinality)
attrs['documentation'] = documentation
self.dispatch_occ(profile_element, occ, target_value, to_process,
card_entity=card_entity)
def dispatch_occ(self, profile_element, occ, target_value, to_process, card_entity):
occ.target.duplicated = isinstance(occ, dict) # XXX iirk
callback = getattr(self, 'element_' + occ.target.__class__.__name__.lower())
callback(occ.target, profile_element, target_value, attrs, to_process)
callback(occ, profile_element, target_value, to_process, card_entity)
def element_alternative(self, xselement, profile_element, target_value, attrs, to_process):
def element_alternative(self, occ, profile_element, target_value, to_process, card_entity):
attrs = xsd_element_cardinality(occ, card_entity)
parent_element = self._parent_element(profile_element)
target_element = self.element('xsd:choice', parent_element, attrs)
to_process[xselement].append((target_value, target_element))
to_process[occ.target].append((target_value, target_element))
def element_sequence(self, xselement, profile_element, target_value, attrs, to_process):
def element_sequence(self, occ, profile_element, target_value, to_process, card_entity):
attrs = xsd_element_cardinality(occ, card_entity)
parent_element = self._parent_element(profile_element)
target_element = self.element('xsd:sequence', parent_element, attrs)
to_process[xselement].append((target_value, target_element))
to_process[occ.target].append((target_value, target_element))
def element_xmlelement(self, xselement, profile_element, target_value, attrs, to_process): # noqa
def element_xmlattribute(self, occ, profile_element, target_value, to_process, card_entity):
attrs = xsd_attribute_cardinality(occ, card_entity)
q = self.qname
xpath = q('xsd:complexType') + '/' + q('xsd:simpleContent') + '/' + q('xsd:extension')
parent = profile_element.find(xpath)
if parent is None:
parent = profile_element.find(self.qname('xsd:complexType'))
assert parent is not None
xselement = occ.target
attrs['name'] = xselement.local_name
content_type = self.xsd_content_type(xselement.textual_content_type)
attrs['type'] = content_type
target_element = self.element('xsd:attribute', parent, attrs)
target_value = xsd_serialize(target_value)
if target_value is not None:
if xselement.local_name == 'id':
target_element.attrib[self.qname('seda:profid')] = target_value
else:
target_element.attrib['fixed'] = target_value
def element_xmlelement(self, occ, profile_element, target_value, to_process, card_entity): # noqa
attrs = xsd_element_cardinality(occ, card_entity)
attrs['documentation'] = getattr(card_entity, 'user_annotation', None)
xselement = occ.target
if xselement.local_name == 'Signature':
attrs['type'] = 'OpenType'
self._target_element(xselement, profile_element, attrs)
......@@ -240,24 +259,6 @@ class SEDA2XSDExport(EntityAdapter):
self.element('xsd:enumeration', restriction_element,
{'value': value.absolute_url()})
def element_xmlattribute(self, xselement, profile_element, target_value, attrs, to_process):
q = self.qname
xpath = q('xsd:complexType') + '/' + q('xsd:simpleContent') + '/' + q('xsd:extension')
parent = profile_element.find(xpath)
if parent is None:
parent = profile_element.find(self.qname('xsd:complexType'))
assert parent is not None
attrs['name'] = xselement.local_name
content_type = self.xsd_content_type(xselement.textual_content_type)
attrs['type'] = content_type
target_element = self.element('xsd:attribute', parent, attrs)
target_value = xsd_serialize(target_value)
if target_value is not None:
if xselement.local_name == 'id':
target_element.attrib[self.qname('seda:profid')] = target_value
else:
target_element.attrib['fixed'] = target_value
def _parent_element(self, profile_element):
q = self.qname
if profile_element.tag in (q('xsd:choice'), q('xsd:sequence')):
......@@ -349,10 +350,11 @@ def xsd_serialize(value):
return unicode(value) # XXX
def xsd_element_cardinality(occ, cardinality):
def xsd_element_cardinality(occ, card_entity):
"""Return XSD element cardinality for the given pyxst Occurence. Cardinality may be overriden by
the data model's user_cardinality value.
"""
cardinality = getattr(card_entity, 'user_cardinality', None)
if cardinality is None:
minimum = occ.minimum
maximum = occ.maximum
......@@ -374,10 +376,11 @@ def xsd_element_cardinality(occ, cardinality):
return attribs
def xsd_attribute_cardinality(occ, cardinality):
def xsd_attribute_cardinality(occ, card_entity):
"""Return XSD attribute cardinality for the given pyxst Occurence. Cardinality may be overriden by
the data model's user_cardinality value.
"""
cardinality = getattr(card_entity, 'user_cardinality', None)
if cardinality is None:
minimum = occ.minimum
else:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment