Ejemplo n.º 1
0
class gMonthDay(BuiltinType, AnySimpleType):
    """gMonthDay is a gregorian date that recurs, specifically a day of the
    year such as the third of May.

    Lexical representation: --MM-DD

    """

    accepted_types = (datetime.date, ) + six.string_types
    _default_qname = xsd_ns("gMonthDay")
    _pattern = re.compile(
        r"^--(?P<month>\d\d)-(?P<day>\d\d)(?P<timezone>Z|[-+]\d\d:?\d\d)?$")

    @check_no_collection
    def xmlvalue(self, value):
        month, day, tzinfo = value
        return "--%02d-%02d%s" % (month, day, _unparse_timezone(tzinfo))

    def pythonvalue(self, value):
        match = self._pattern.match(value)
        if not match:
            raise ParseError()

        group = match.groupdict()
        return (
            int(group["month"]),
            int(group["day"]),
            _parse_timezone(group["timezone"]),
        )
Ejemplo n.º 2
0
class gYearMonth(BuiltinType, AnySimpleType):
    """gYearMonth represents a specific gregorian month in a specific gregorian
    year.

    Lexical representation: CCYY-MM

    """

    accepted_types = (datetime.date, ) + six.string_types
    _default_qname = xsd_ns("gYearMonth")
    _pattern = re.compile(
        r"^(?P<year>-?\d{4,})-(?P<month>\d\d)(?P<timezone>Z|[-+]\d\d:?\d\d)?$")

    @check_no_collection
    def xmlvalue(self, value):
        year, month, tzinfo = value
        return "%04d-%02d%s" % (year, month, _unparse_timezone(tzinfo))

    def pythonvalue(self, value):
        match = self._pattern.match(value)
        if not match:
            raise ParseError()
        group = match.groupdict()
        return (
            int(group["year"]),
            int(group["month"]),
            _parse_timezone(group["timezone"]),
        )
Ejemplo n.º 3
0
class DateTime(BuiltinType, AnySimpleType):
    _default_qname = xsd_ns("dateTime")
    accepted_types = (datetime.datetime, ) + six.string_types

    @check_no_collection
    def xmlvalue(self, value):
        if isinstance(value, six.string_types):
            return value

        # Bit of a hack, since datetime is a subclass of date we can't just
        # test it with an isinstance(). And actually, we should not really
        # care about the type, as long as it has the required attributes
        if not all(
                hasattr(value, attr) for attr in ("hour", "minute", "second")):
            value = datetime.datetime.combine(
                value,
                datetime.time(
                    getattr(value, "hour", 0),
                    getattr(value, "minute", 0),
                    getattr(value, "second", 0),
                ),
            )

        if getattr(value, "microsecond", 0):
            return isodate.isostrf.strftime(value, "%Y-%m-%dT%H:%M:%S.%f%Z")
        return isodate.isostrf.strftime(value, "%Y-%m-%dT%H:%M:%S%Z")

    def pythonvalue(self, value):

        # Determine based on the length of the value if it only contains a date
        # lazy hack ;-)
        if len(value) == 10:
            value += "T00:00:00"
        return isodate.parse_datetime(value)
Ejemplo n.º 4
0
class Schema(Base):
    name = "schema"
    attr_name = "schema"
    qname = xsd_ns("schema")

    def clone(self, qname, min_occurs=1, max_occurs=1):
        return self.__class__()

    def parse_kwargs(self, kwargs, name, available_kwargs):
        if name in available_kwargs:
            value = kwargs[name]
            available_kwargs.remove(name)
            return {name: value}
        return {}

    def parse(self, xmlelement, schema, context=None):
        from core.utilities.soap.xsd.schema import Schema

        schema = Schema(xmlelement, schema._transport)
        context.schemas.append(schema)
        return schema

    def parse_xmlelements(self, xmlelements, schema, name=None, context=None):
        if xmlelements[0].tag == self.qname:
            xmlelement = xmlelements.popleft()
            result = self.parse(xmlelement, schema, context=context)
            return result

    def resolve(self):
        return self
Ejemplo n.º 5
0
class Integer(Decimal):
    _default_qname = xsd_ns("integer")
    accepted_types = (int, float) + six.string_types

    def xmlvalue(self, value):
        return str(value)

    def pythonvalue(self, value):
        return int(value)
Ejemplo n.º 6
0
class Float(BuiltinType, AnySimpleType):
    _default_qname = xsd_ns("float")
    accepted_types = (float, _Decimal) + six.string_types

    def xmlvalue(self, value):
        return str(value).upper()

    def pythonvalue(self, value):
        return float(value)
Ejemplo n.º 7
0
class QName(BuiltinType, AnySimpleType):
    accepted_types = six.string_types
    _default_qname = xsd_ns("QName")

    @check_no_collection
    def xmlvalue(self, value):
        return value

    def pythonvalue(self, value):
        return value
Ejemplo n.º 8
0
class Base64Binary(BuiltinType, AnySimpleType):
    accepted_types = six.string_types
    _default_qname = xsd_ns("base64Binary")

    @check_no_collection
    def xmlvalue(self, value):
        return base64.b64encode(value)

    def pythonvalue(self, value):
        return base64.b64decode(value)
Ejemplo n.º 9
0
class Double(BuiltinType, AnySimpleType):
    _default_qname = xsd_ns("double")
    accepted_types = (_Decimal, float) + six.string_types

    @check_no_collection
    def xmlvalue(self, value):
        return str(value)

    def pythonvalue(self, value):
        return float(value)
Ejemplo n.º 10
0
class String(BuiltinType, AnySimpleType):
    _default_qname = xsd_ns("string")
    accepted_types = six.string_types

    @check_no_collection
    def xmlvalue(self, value):
        if isinstance(value, bytes):
            return value.decode("utf-8")
        return six.text_type(value if value is not None else "")

    def pythonvalue(self, value):
        return value
Ejemplo n.º 11
0
class Date(BuiltinType, AnySimpleType):
    _default_qname = xsd_ns("date")
    accepted_types = (datetime.date, ) + six.string_types

    @check_no_collection
    def xmlvalue(self, value):
        if isinstance(value, six.string_types):
            return value
        return isodate.isostrf.strftime(value, "%Y-%m-%d")

    def pythonvalue(self, value):
        return isodate.parse_date(value)
Ejemplo n.º 12
0
class Boolean(BuiltinType, AnySimpleType):
    _default_qname = xsd_ns("boolean")
    accepted_types = (bool, )

    @check_no_collection
    def xmlvalue(self, value):
        return "true" if value and value not in ("false", "0") else "false"

    def pythonvalue(self, value):
        """Return True if the 'true' or '1'. 'false' and '0' are legal false
        values, but we consider everything not true as false.

        """
        return value in ("true", "1")
Ejemplo n.º 13
0
class Time(BuiltinType, AnySimpleType):
    _default_qname = xsd_ns("time")
    accepted_types = (datetime.time, ) + six.string_types

    @check_no_collection
    def xmlvalue(self, value):
        if isinstance(value, six.string_types):
            return value

        if value.microsecond:
            return isodate.isostrf.strftime(value, "%H:%M:%S.%f%Z")
        return isodate.isostrf.strftime(value, "%H:%M:%S%Z")

    def pythonvalue(self, value):
        return isodate.parse_time(value)
Ejemplo n.º 14
0
class Duration(BuiltinType, AnySimpleType):
    _default_qname = xsd_ns("duration")
    accepted_types = (isodate.duration.Duration, ) + six.string_types

    @check_no_collection
    def xmlvalue(self, value):
        return isodate.duration_isoformat(value)

    def pythonvalue(self, value):
        if value.startswith("PT-"):
            value = value.replace("PT-", "PT")
            result = isodate.parse_duration(value)
            return datetime.timedelta(0 - result.total_seconds())
        else:
            return isodate.parse_duration(value)
Ejemplo n.º 15
0
class gMonth(BuiltinType, AnySimpleType):
    """gMonth is a gregorian month that recurs every year.

    Lexical representation: --MM

    """

    accepted_types = (datetime.date, ) + six.string_types
    _default_qname = xsd_ns("gMonth")
    _pattern = re.compile(
        r"^--(?P<month>\d\d)(?P<timezone>Z|[-+]\d\d:?\d\d)?$")

    @check_no_collection
    def xmlvalue(self, value):
        month, tzinfo = value
        return "--%d%s" % (month, _unparse_timezone(tzinfo))

    def pythonvalue(self, value):
        match = self._pattern.match(value)
        if not match:
            raise ParseError()
        group = match.groupdict()
        return (int(group["month"]), _parse_timezone(group["timezone"]))
Ejemplo n.º 16
0
class PositiveInteger(NonNegativeInteger):
    _default_qname = xsd_ns("positiveInteger")
Ejemplo n.º 17
0
class UnsignedByte(UnsignedShort):
    _default_qname = xsd_ns("unsignedByte")
Ejemplo n.º 18
0
class UnsignedShort(UnsignedInt):
    _default_qname = xsd_ns("unsignedShort")
Ejemplo n.º 19
0
class UnsignedInt(UnsignedLong):
    _default_qname = xsd_ns("unsignedInt")
Ejemplo n.º 20
0
class UnsignedLong(NonNegativeInteger):
    _default_qname = xsd_ns("unsignedLong")
Ejemplo n.º 21
0
class NonNegativeInteger(Integer):
    _default_qname = xsd_ns("nonNegativeInteger")
Ejemplo n.º 22
0
class IDREFS(IDREF):
    _default_qname = xsd_ns("IDREFS")
Ejemplo n.º 23
0
class Short(Int):
    _default_qname = xsd_ns("short")
Ejemplo n.º 24
0
class Entities(Entity):
    _default_qname = xsd_ns("ENTITIES")
Ejemplo n.º 25
0
class Entity(NCName):
    _default_qname = xsd_ns("ENTITY")
Ejemplo n.º 26
0
class Byte(Short):
    """A signed 8-bit integer"""

    _default_qname = xsd_ns("byte")
Ejemplo n.º 27
0
class Long(Integer):
    _default_qname = xsd_ns("long")

    def pythonvalue(self, value):
        return long(value) if six.PY2 else int(value)  # noqa
Ejemplo n.º 28
0
class IDREF(NCName):
    _default_qname = xsd_ns("IDREF")
Ejemplo n.º 29
0
    "sequence",
    "group",
    "choice",
    "all",
    "list",
    "union",
    "attribute",
    "any",
    "anyAttribute",
    "attributeGroup",
    "restriction",
    "extension",
    "notation",
]:
    attr = name if name not in keyword.kwlist else name + "_"
    setattr(tags, attr, xsd_ns(name))


class SchemaVisitor(object):
    """Visitor which processes XSD files and registers global elements and
    types in the given schema.

    :param schema:
    :type schema: zeep.xsd.schema.Schema
    :param document:
    :type document: zeep.xsd.schema.SchemaDocument

    """

    def __init__(self, schema, document):
        self.document = document
Ejemplo n.º 30
0
class Int(Long):
    _default_qname = xsd_ns("int")