Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def iterchildren_by_tag(tag):
"""
Defines a generator that produce all child elements that have a specific tag.
"""
def iterfind_function(root):
for elem in root:
if elem.tag == tag:
yield elem
iterfind_function.__name__ = 'iterfind_xsd_%ss' % '_'.join(camel_case_split(local_name(tag))).lower()
return iterfind_function
def __setattr__(self, name, value):
if name == "elem":
if not etree_iselement(value):
raise XMLSchemaTypeError("%r attribute must be an Etree Element: %r" % (name, value))
elif value.tag not in self.admitted_tags:
raise XMLSchemaValueError(
"wrong XSD element %r for %r, must be one of %r." % (
local_name(value.tag), self,
[local_name(tag) for tag in self.admitted_tags]
)
)
elif hasattr(self, 'elem'):
self._elem = self.elem # redefinition cases
elif name == "schema":
if hasattr(self, 'schema') and self.schema.target_namespace != value.target_namespace:
raise XMLSchemaValueError(
"cannot change 'schema' attribute of %r: the actual %r has a different "
"target namespace than %r." % (self, self.schema, value)
)
self.target_namespace = value.target_namespace
super(XsdBase, self).__setattr__(name, value)
@property
def local_name(self):
return local_name(self.name)
def __setattr__(self, name, value):
if name in ('attr_prefix', 'text_key', 'cdata_prefix'):
if value is not None and any(c in string.ascii_letters or c == '_' for c in value):
raise XMLSchemaValueError('%r cannot includes letters or underscores: %r' % (name, value))
elif name == 'attr_prefix':
self.ns_prefix = (value or '') + 'xmlns'
elif name == 'strip_namespaces':
if value:
self.map_qname = MethodType(local_name, self)
self.unmap_qname = MethodType(lambda x, y=None: local_name(x), self)
elif getattr(self, 'strip_namespaces', False):
# Rebuild instance methods only if necessary
self.map_qname = MethodType(XMLSchemaConverter.map_qname, self)
self.unmap_qname = MethodType(XMLSchemaConverter.unmap_qname, self)
super(XMLSchemaConverter, self).__setattr__(name, value)
xsd_globals[qname] = (elem, schema)
else:
try:
other_schema = xsd_globals[qname][1]
except (TypeError, IndexError):
pass
else:
# It's ignored or replaced in case of an override
if other_schema.override is schema:
continue
elif schema.override is other_schema:
xsd_globals[qname] = (elem, schema)
continue
msg = "global {} with name={!r} is already defined"
schema.parse_error(msg.format(local_name(tag), qname))
tags = Counter([x[0] for x in redefinitions])
for qname, elem, child, schema, redefined_schema in redefinitions:
# Checks multiple redefinitions
if tags[qname] > 1:
tags[qname] = 1
redefined_schemas = [x[-1] for x in redefinitions if x[0] == qname]
if any(redefined_schemas.count(x) > 1 for x in redefined_schemas):
msg = "multiple redefinition for {} {!r}"
schema.parse_error(msg.format(local_name(child.tag), qname), child)
else:
redefined_schemas = {x[-1]: x[-2] for x in redefinitions if x[0] == qname}
for rs, s in redefined_schemas.items():
while True:
self.unmap_qname = MethodType(lambda x, y=None: local_name(x), self)
elif getattr(self, 'strip_namespaces', False):
def _parse_content_model(self, content_model):
self.model = local_name(content_model.tag)
if self.model == 'all':
if self.max_occurs != 1:
self.parse_error("maxOccurs must be 1 for 'all' model groups")
if self.min_occurs not in (0, 1):
self.parse_error("minOccurs must be (0 | 1) for 'all' model groups")
for child in filter(lambda x: x.tag != XSD_ANNOTATION, content_model):
if child.tag == XSD_ELEMENT:
# Builds inner elements and reference groups later, for avoids circularity.
self.append((child, self.schema))
elif content_model.tag == XSD_ALL:
self.parse_error("'all' model can contains only elements.")
elif child.tag == XSD_ANY:
self.append(XsdAnyElement(child, self.schema, self))
elif child.tag in (XSD_SEQUENCE, XSD_CHOICE):
self.append(XsdGroup(child, self.schema, self))
def check_tag(elem, *tags):
if elem.tag not in tags:
print(etree_tostring(elem))
tags = (local_name(tag) for tag in tags)
raise XMLSchemaParseError("({}) expected: {}".format('|'.join(tags), elem))
def _parse_facets(self, facets):
if facets and self.base_type is not None:
if self.base_type.is_simple():
if self.base_type.name == XSD_ANY_SIMPLE_TYPE:
self.parse_error("facets not allowed for a direct derivation of xs:anySimpleType")
elif self.base_type.has_simple_content():
if self.base_type.content_type.name == XSD_ANY_SIMPLE_TYPE:
self.parse_error("facets not allowed for a direct content derivation of xs:anySimpleType")
# Checks the applicability of the facets
if any(k not in self.admitted_facets for k in facets if k is not None):
reason = "one or more facets are not applicable, admitted set is %r:"
self.parse_error(reason % {local_name(e) for e in self.admitted_facets if e})
# Check group base_type
base_type = {t.base_type for t in facets.values() if isinstance(t, XsdFacet)}
if len(base_type) > 1:
self.parse_error("facet group must have the same base_type: %r" % base_type)
base_type = base_type.pop() if base_type else None
# Checks length based facets
length = getattr(facets.get(XSD_LENGTH), 'value', None)
min_length = getattr(facets.get(XSD_MIN_LENGTH), 'value', None)
max_length = getattr(facets.get(XSD_MAX_LENGTH), 'value', None)
if length is not None:
if length < 0:
self.parse_error("'length' value must be non negative integer")
if min_length is not None:
if min_length > length: