How to use the sdv.utils function in sdv

To help you get started, we’ve selected a few sdv 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 STIXProject / stix-validator / sdv / scripts / __init__.py View on Github external
def _set_huge_tree_parser():
    parser = lxml.etree.ETCompatXMLParser(
        attribute_defaults=False,
        load_dtd=False,
        huge_tree=True,
        no_network=True,
        ns_clean=True,
        recover=False,
        remove_pis=False,
        remove_blank_text=False,
        remove_comments=False,
        resolve_entities=False,
        strip_cdata=True
    )

    utils.set_xml_parser(parser)
github STIXProject / stix-validator / sdv / validators / cybox / common.py View on Github external
def check_root(doc):
    if utils.is_cybox(doc):
        return

    error = "Input document does not contain a valid CybOX root element."
    raise errors.ValidationError(error)
github STIXProject / stix-validator / sdv / validators / stix / common.py View on Github external
def inner(*args, **kwargs):
        try:
            doc = args[1]
        except IndexError:
            doc = kwargs['doc']

        # Get the root element for the input doc
        root = utils.get_etree_root(doc)

        # Check that the root is a valid STIX root-level element
        check_root(root)

        return func(*args, **kwargs)
github STIXProject / stix-validator / sdv / validators / stix / best_practice.py View on Github external
def _check_timestamp_usage(self, root, namespaces, selectors):
        """Inspects each node in `nodes` for correct timestamp use.

        """
        results = BestPracticeWarningCollection("Timestamp Use")
        xpath = " | ".join("//%s" % x for x in selectors)
        nodes = root.xpath(xpath, namespaces=namespaces)

        for node in nodes:
            attrib      = node.attrib.get
            id_         = attrib('id')
            idref       = attrib('idref')
            timestamp   = attrib('timestamp')

            if timestamp:
                tz_set = utils.has_tzinfo(timestamp)

                if not tz_set:
                    warning = BestPracticeWarning(
                        node = node,
                        message="Timestamp without timezone information."
                    )
                    warning['timestamp'] = timestamp
                    results.append(warning)

            if id_ and not timestamp:
                warning = BestPracticeWarning(
                    node=node,
                    message="ID present but missing timestamp"
                )
            elif idref and not timestamp:
                warning = BestPracticeWarning(
github STIXProject / stix-validator / sdv / validators / stix / best_practice.py View on Github external
def _check_titles(self, root, namespaces, selectors):
        """Checks that each node in `nodes` has a ``Title`` element unless
        there is an ``@idref`` attribute set.

        """
        results = BestPracticeWarningCollection("Missing Titles")
        xpath = " | ".join("//%s" % x for x in selectors)
        nodes = root.xpath(xpath, namespaces=namespaces)

        for node in nodes:
            if 'idref' in node.attrib:
                continue

            if not any(utils.localname(x) == 'Title' for x in utils.iterchildren(node)):
                warning = BestPracticeWarning(node=node)
                results.append(warning)

        return results
github STIXProject / stix-validator / sdv / validators / stix / profile.py View on Github external
it to be "http://icl.com/saxon". The freely distributed SaxonHE
        library does not support Saxon extension functions at all.

        Returns:
            An ``etree._ElementTree`` XSLT document.

        """
        if not self._schematron:
            return None

        s = etree.tostring(self._schematron.validator_xslt)
        s = s.replace(' []', '')
        s = s.replace('xmlns:saxon="http://icl.com/saxon"', '')
        s = s.replace('', '')

        parser = utils.get_xml_parser()
        return etree.parse(StringIO(s), parser=parser)
github STIXProject / stix-validator / sdv / validators / stix / best_practice.py View on Github external
def _equal_timestamps(nodeset):
            return [x for x in nodeset if utils.is_equal_timestamp(node, x)]