How to use the xmlschema.qnames.get_namespace function in xmlschema

To help you get started, we’ve selected a few xmlschema examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sissaschool / xmlschema / xmlschema / validators / wildcards.py View on Github external
def is_matching(self, name, default_namespace=None, **kwargs):
        if name is None:
            return False
        elif not name or name[0] == '{':
            return self.is_namespace_allowed(get_namespace(name))
        elif default_namespace is None:
            return self.is_namespace_allowed('')
        else:
            return self.is_namespace_allowed('') or \
                self.is_namespace_allowed(default_namespace)
github sissaschool / xmlschema / xmlschema / validators / attributes.py View on Github external
for k in filter(lambda x: x not in attrs, self.iter_required()):
                reason = "missing required attribute: %r" % k
                yield self.validation_error(validation, reason, attrs, **kwargs)

        use_defaults = kwargs.get('use_defaults', True)
        additional_attrs = [(k, v) for k, v in self.iter_predefined(use_defaults) if k not in attrs]
        if additional_attrs:
            attrs = {k: v for k, v in attrs.items()}
            attrs.update(additional_attrs)

        result_list = []
        for name, value in attrs.items():
            try:
                xsd_attribute = self[name]
            except KeyError:
                namespace = get_namespace(name) or self.target_namespace
                if namespace == XSI_NAMESPACE:
                    try:
                        xsd_attribute = self.maps.lookup_attribute(name)
                    except LookupError:
                        if validation != 'skip':
                            reason = "%r is not an attribute of the XSI namespace." % name
                            yield self.validation_error(validation, reason, attrs, **kwargs)
                        continue
                else:
                    try:
                        xsd_attribute = self[None]  # None key ==> anyAttribute
                        value = (name, value)
                    except KeyError:
                        if validation != 'skip':
                            reason = "%r attribute not allowed for element." % name
                            yield self.validation_error(validation, reason, attrs, **kwargs)
github sissaschool / xmlschema / xmlschema / validators / wildcards.py View on Github external
def deny_qnames(self, names):
        if self.not_namespace:
            return all(x in self.not_qname or get_namespace(x) in self.not_namespace for x in names)
        elif '##any' in self.namespace:
            return all(x in self.not_qname for x in names)
        elif '##other' in self.namespace:
            return all(x in self.not_qname or get_namespace(x) == self.target_namespace for x in names)
        else:
            return all(x in self.not_qname or get_namespace(x) not in self.namespace for x in names)
github sissaschool / xmlschema / xmlschema / validators / wildcards.py View on Github external
def union(self, other):
        """
        Update an XSD wildcard with the union of itself and another XSD wildcard.
        """
        if not self.not_qname:
            self.not_qname = other.not_qname[:]
        else:
            self.not_qname = [
                x for x in self.not_qname
                if x in other.not_qname or not other.is_namespace_allowed(get_namespace(x))
            ]

        if self.not_namespace:
            if other.not_namespace:
                self.not_namespace = [ns for ns in self.not_namespace if ns in other.not_namespace]
            elif '##any' in other.namespace:
                self.not_namespace = []
                self.namespace = ['##any']
                return
            elif '##other' in other.namespace:
                not_namespace = ('', other.target_namespace)
                self.not_namespace = [ns for ns in self.not_namespace if ns in not_namespace]
            else:
                self.not_namespace = [ns for ns in self.not_namespace if ns not in other.namespace]

            if not self.not_namespace:
github sissaschool / xmlschema / xmlschema / validators / wildcards.py View on Github external
def iter_encode(self, attribute, validation='lax', **kwargs):
        name, value = attribute
        namespace = get_namespace(name)

        if not self.is_namespace_allowed(namespace):
            if validation != 'skip':
                reason = "attribute %r not allowed." % name
                yield self.validation_error(validation, reason, attribute, **kwargs)

        elif self.process_contents == 'skip':
            return

        elif self.maps.load_namespace(namespace):
            try:
                xsd_attribute = self.maps.lookup_attribute(name)
            except LookupError:
                if validation == 'skip':
                    yield unicode_type(value)
                elif self.process_contents == 'strict':
github sissaschool / xmlschema / xmlschema / validators / attributes.py View on Github external
use_defaults = kwargs.get('use_defaults', True)
        id_map = kwargs.get('id_map', '')
        num_id = len(id_map)

        additional_attrs = [(k, v) for k, v in self.iter_predefined(use_defaults) if k not in attrs]
        if additional_attrs:
            attrs = {k: v for k, v in attrs.items()}
            attrs.update(additional_attrs)

        filler = kwargs.get('filler')
        result_list = []
        for name, value in attrs.items():
            try:
                xsd_attribute = self[name]
            except KeyError:
                if get_namespace(name) == XSI_NAMESPACE:
                    try:
                        xsd_attribute = self.maps.lookup_attribute(name)
                    except LookupError:
                        if validation != 'skip':
                            reason = "%r is not an attribute of the XSI namespace." % name
                            yield self.validation_error(validation, reason, attrs, **kwargs)
                        continue
                else:
                    try:
                        xsd_attribute = self[None]  # None key ==> anyAttribute
                        value = (name, value)
                    except KeyError:
                        if validation != 'skip':
                            reason = "%r attribute not allowed for element." % name
                            yield self.validation_error(validation, reason, attrs, **kwargs)
                        continue
github sissaschool / xmlschema / xmlschema / validators / wildcards.py View on Github external
def iter_decode(self, elem, validation='lax', **kwargs):
        if not self.is_matching(elem.tag):
            if validation != 'skip':
                reason = "element %r not allowed here." % elem.tag
                yield self.validation_error(validation, reason, elem, **kwargs)

        elif self.process_contents == 'skip':
            return

        elif self.maps.load_namespace(get_namespace(elem.tag)):
            try:
                xsd_element = self.maps.lookup_element(elem.tag)
            except LookupError:
                if validation == 'skip':
                    yield self.any_type.decode(elem) if len(elem) > 0 else elem.text
                elif self.process_contents == 'strict':
                    reason = "element %r not found." % elem.tag
                    yield self.validation_error(validation, reason, elem, **kwargs)
            else:
                for result in xsd_element.iter_decode(elem, validation, **kwargs):
                    yield result

        elif validation == 'skip':
            yield self.any_type.decode(elem) if len(elem) > 0 else elem.text

        elif self.process_contents == 'strict':
github sissaschool / xmlschema / xmlschema / validators / wildcards.py View on Github external
def is_matching(self, name, default_namespace=None, group=None, occurs=None):
        """
        Returns `True` if the component name is matching the name provided as argument,
        `False` otherwise. For XSD elements the matching is extended to substitutes.

        :param name: a local or fully-qualified name.
        :param default_namespace: used if it's not None and not empty for completing \
        the name argument in case it's a local name.
        :param group: used only by XSD 1.1 any element wildcards to verify siblings in \
        case of ##definedSibling value in notQName attribute.
        :param occurs: a Counter instance for verify model occurrences counting.
        """
        if name is None:
            return False
        elif not name or name[0] == '{':
            if not self.is_namespace_allowed(get_namespace(name)):
                return False
        elif default_namespace is not None:
            if not self.is_namespace_allowed(''):
                return False
        else:
            name = '{%s}%s' % (default_namespace, name)
            if not self.is_namespace_allowed('') and not self.is_namespace_allowed(default_namespace):
                return False

        if group in self.precedences:
            if occurs is None:
                if any(e.is_matching(name) for e in self.precedences[group]):
                    return False
            elif any(e.is_matching(name) and not e.is_over(occurs[e]) for e in self.precedences[group]):
                return False
github sissaschool / xmlschema / xmlschema / validators / wildcards.py View on Github external
def iter_encode(self, obj, validation='lax', **kwargs):
        name, value = obj
        namespace = get_namespace(name)

        if not self.is_namespace_allowed(namespace):
            if validation != 'skip':
                reason = "element %r not allowed here." % name
                yield self.validation_error(validation, reason, value, **kwargs)

        elif self.process_contents == 'skip':
            return

        elif self.maps.load_namespace(namespace):
            try:
                xsd_element = self.maps.lookup_element(name)
            except LookupError:
                if validation == 'skip':
                    yield self.any_type.encode(value)
                elif self.process_contents == 'strict':