Commit 5a509723 authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

[code gen] Turn iter_rtype_roles function into a XSDMMapping method

and move the XSDM_MAPPING singleton to the xsd module.
parent fcb4c7640eb9
......@@ -24,8 +24,7 @@ from pyxst.xml_struct import graph_nodes
from cubicweb.predicates import is_instance
from cubicweb.view import EntityAdapter
from cubes.seda.xsd import BASE_TYPES
from cubes.seda.entities import XSDM_MAPPING, iter_rtype_role
from cubes.seda.xsd import BASE_TYPES, XSDM_MAPPING
JUMPED_OPTIONAL_ELEMENTS = set(('FileInfo', 'PhysicalDimensions'))
......@@ -138,7 +137,7 @@ class SEDA2XSDExport(EntityAdapter):
# element, and mark it as mandatory if one of them is mandatory, else keep it
# optional
cardinality = '0..1'
for rtype, role, _ in iter_rtype_role(occ.target.local_name):
for rtype, role, _ in XSDM_MAPPING.iter_rtype_role(occ.target.local_name):
targets = entity.related(rtype, role, entities=True)
if any(target.user_cardinality == '1' for target in targets):
cardinality = '1'
......
......@@ -21,7 +21,7 @@ from cubicweb.utils import UStringIO
# XXX alias to avoid side effect: uicfg will become our uicfg submodule. Not necessary with 3.23?
from cubicweb.web.views import uicfg as cwuicfg
from cubes.seda.entities import iter_rtype_role
from cubes.seda.xsd import XSDM_MAPPING
_ = unicode
......@@ -40,7 +40,7 @@ def rtags_from_xsd_element(etype, element_name):
"""
ordered = []
rsection = cwuicfg.PrimaryViewSectionRelationTags(__module__=__name__)
for rtype, role, path in iter_rtype_role(element_name):
for rtype, role, path in XSDM_MAPPING.iter_rtype_role(element_name):
if role == 'subject':
# mandatory elements
relation = (etype, rtype, '*', role)
......
......@@ -347,6 +347,18 @@ class XSDMMapping(object):
def __getitem__(self, element):
return self._index[element]
def iter_rtype_role(self, xsd_element):
"""Given a XSD element name, yield (rtype, role, path) where `rtype` and `role` define a
relation of the yams data model for a subelement, also including the full `path` if you need
more information
"""
subelement_defs = self[self.element_by_name(xsd_element)][1]
for occ, path in subelement_defs:
if occ.target.local_name in ('id', 'Filename'):
continue
rtype, role, _, _ = path[0]
yield rtype, role, path
def elements_by_name(self, element_name):
if self._elements_by_name_index is None:
self._elements_by_name_index = defaultdict(list)
......@@ -544,3 +556,6 @@ def _content_types(element):
def _shorten(name):
return {'Alternative': 'Alt', 'Sequence': 'Seq'}[name]
XSDM_MAPPING = XSDMMapping()
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