Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def raw_xml_encode(value):
"""Encodes a simple value to XML."""
if isinstance(value, bool):
return 'true' if value else 'false'
elif isinstance(value, (list, tuple)):
return ' '.join(unicode_type(e) for e in value)
else:
return unicode_type(value)
return
if is_etree_element(elem):
pass
elif elem is None:
elem = getattr(self, 'elem', None)
else:
raise XMLSchemaValueError("'elem' argument must be an Element instance, not %r." % elem)
if isinstance(error, XMLSchemaParseError):
error.validator = self
error.namespaces = getattr(self, 'namespaces', None)
error.elem = elem
error.source = getattr(self, 'source', None)
elif isinstance(error, Exception):
message = unicode_type(error).strip()
if message[0] in '\'"' and message[0] == message[-1]:
message = message.strip('\'"')
error = XMLSchemaParseError(self, message, elem)
elif isinstance(error, string_base_type):
error = XMLSchemaParseError(self, error, elem)
else:
raise XMLSchemaValueError("'error' argument must be an exception or a string, not %r." % error)
if validation == 'lax':
self.errors.append(error)
else:
raise error
def __unicode__(self):
if not self.negative:
return '[%s]' % unicode_type(self.positive)
elif not self.positive:
return '[^%s]' % unicode_type(self.negative)
else:
return '[%s%s]' % (
unicode_type(UnicodeSubset(self.negative.complement())), unicode_type(self.positive)
)
'to_python': datatypes.GregorianYearMonth10.fromstring,
}, # [-][Y*]YYYY-MM
)
XSD_11_BUILTIN_TYPES = XSD_COMMON_BUILTIN_TYPES + (
# --- Year related primitive types (year 0 allowed and mapped to 1 BCE) ---
{
'name': XSD_DATETIME,
'python_type': (unicode_type, str, datatypes.DateTime),
'admitted_facets': DATETIME_FACETS,
'facets': [COLLAPSE_WHITE_SPACE_ELEMENT],
'to_python': datatypes.DateTime.fromstring,
}, # [-][Y*]YYYY-MM-DD[Thh:mm:ss]
{
'name': XSD_DATE,
'python_type': (unicode_type, str, datatypes.Date),
'admitted_facets': DATETIME_FACETS,
'facets': [COLLAPSE_WHITE_SPACE_ELEMENT],
'to_python': datatypes.Date.fromstring,
}, # [-][Y*]YYYY-MM-DD
{
'name': XSD_GYEAR,
'python_type': (unicode_type, str, datatypes.GregorianYear),
'admitted_facets': DATETIME_FACETS,
'facets': [COLLAPSE_WHITE_SPACE_ELEMENT],
'to_python': datatypes.GregorianYear.fromstring,
}, # [-][Y*]YYYY
{
'name': XSD_GYEAR_MONTH,
'python_type': (unicode_type, str, datatypes.GregorianYearMonth),
'admitted_facets': DATETIME_FACETS,
'facets': [COLLAPSE_WHITE_SPACE_ELEMENT],
return
if is_etree_element(elem):
pass
elif elem is None:
elem = getattr(self, 'elem', None)
else:
raise XMLSchemaValueError("'elem' argument must be an Element instance, not %r." % elem)
if isinstance(error, XMLSchemaParseError):
error.validator = self
error.namespaces = getattr(self, 'namespaces', None)
error.elem = elem
error.source = getattr(self, 'source', None)
elif isinstance(error, Exception):
message = unicode_type(error).strip()
if message[0] in '\'"' and message[0] == message[-1]:
message = message.strip('\'"')
error = XMLSchemaParseError(self, message, elem)
elif isinstance(error, string_base_type):
error = XMLSchemaParseError(self, error, elem)
else:
raise XMLSchemaValueError("'error' argument must be an exception or a string, not %r." % error)
if validation == 'lax':
self.errors.append(error)
else:
raise error
def raw_xml_encode(value):
"""Encodes a simple value to XML."""
if isinstance(value, bool):
return 'true' if value else 'false'
elif isinstance(value, (list, tuple)):
return ' '.join(unicode_type(e) for e in value)
else:
return unicode_type(value)
def python_to_boolean(obj):
return unicode_type(obj).lower()
def iter_encode(self, obj, validation='lax', **kwargs):
if not hasattr(obj, '__iter__') or isinstance(obj, (str, unicode_type, bytes)):
obj = [obj]
encoded_items = []
for item in obj:
for result in self.base_type.iter_encode(item, validation, **kwargs):
if isinstance(result, XMLSchemaValidationError):
yield result
else:
encoded_items.append(result)
yield ' '.join(item for item in encoded_items if item is not None)
def _parse(self):
super(XsdFacet, self)._parse()
if 'fixed' in self.elem.attrib and self.elem.attrib['fixed'] in ('true', '1'):
self.fixed = True
base_facet = self.base_facet
self.base_value = None if base_facet is None else base_facet.value
try:
self._parse_value(self.elem)
except (KeyError, ValueError, XMLSchemaDecodeError) as err:
self.value = None
self.parse_error(unicode_type(err))
else:
if base_facet is not None and base_facet.fixed and \
base_facet.value is not None and self.value != base_facet.value:
self.parse_error("%r facet value is fixed to %r" % (self.elem.tag, base_facet.value))