예제 #1
0
    def _is_empty_row(self, worksheet, row):
        """Returns true if the `row` in `worksheet` does not contain any values
        in any columns.

        """
        cols = range(worksheet.ncols)
        return not any(self._get_value(worksheet, row, col) for col in cols)
예제 #2
0
    def _is_empty_row(self, worksheet, row):
        """Returns true if the `row` in `worksheet` does not contain any values
        in any columns.

        """
        cols = range(worksheet.ncols)
        return not any(self._get_value(worksheet, row, col) for col in cols)
예제 #3
0
    def _parse_namespace_worksheet(self, worksheet):
        """Parses the Namespaces worksheet of a STIX profile. Returns a
        dictionary representation.

        ``d = { <namespace> : <namespace alias> }``

        By default, libxml2-required Saxon namespace is added to the return
        dictionary.

        """
        value = functools.partial(self._get_value, worksheet)
        is_empty = functools.partial(self._is_empty_row, worksheet)
        nsmap = {xmlconst.NS_SAXON: 'saxon'}

        def check_namespace(ns, alias):
            if ns and alias:
                return

            err = ("Missing namespace or alias: unable to parse Namespaces "
                   "worksheet")
            raise errors.ProfileParseError(err)

        for row in range(1, worksheet.nrows):  # skip the first row
            if is_empty(row):
                continue

            ns = value(row, COL_NAMESPACE)
            alias = value(row, COL_ALIAS)
            check_namespace(ns, alias)
            nsmap[ns] = alias

        return nsmap
예제 #4
0
    def _parse_namespace_worksheet(self, worksheet):
        """Parses the Namespaces worksheet of a STIX profile. Returns a
        dictionary representation.

        ``d = { <namespace> : <namespace alias> }``

        By default, libxml2-required Saxon namespace is added to the return
        dictionary.

        """
        value    = functools.partial(self._get_value, worksheet)
        is_empty = functools.partial(self._is_empty_row, worksheet)
        nsmap    = {xmlconst.NS_SAXON: 'saxon'}

        def check_namespace(ns, alias):
            if ns and alias:
                return

            err = ("Missing namespace or alias: unable to parse Namespaces "
                   "worksheet")
            raise errors.ProfileParseError(err)

        for row in range(1, worksheet.nrows):  # skip the first row
            if is_empty(row):
                continue

            ns = value(row, COL_NAMESPACE)
            alias = value(row, COL_ALIAS)
            check_namespace(ns, alias)
            nsmap[ns] = alias

        return nsmap
예제 #5
0
    def _parse_worksheet_rules(self, worksheet, instance_map):
        """Parses the rules from the profile sheet `workheet`.

        Args:
            worksheet: A profile worksheet containing rules.
            instance_map: A dictionary representation of the ``Instance
                Mapping`` worksheet.

        Returns:
            A list of ``_BaseProfileRule`` implementations for the rules
            defined in the `worksheet`.

        Raises:
            .ProfileParseError: If a rule context label has no associated
                entry in `instance_map`.

        """
        value = functools.partial(self._get_value, worksheet)
        is_empty_row = functools.partial(self._is_empty_row, worksheet)

        def check_label(label):
            if label not in instance_map:
                err = ("Worksheet '{0}' context label '{1}' has no Instance "
                       "Mapping entry.")
                raise errors.ProfileParseError(
                    err.format(worksheet.name, label))

        all_rules = []
        for i in range(1, worksheet.nrows):
            if is_empty_row(i):
                continue

            if not value(i, COL_OCCURRENCE):
                ctx_label = value(i, COL_FIELD_NAME)
                check_label(ctx_label)
                continue

            field = value(i, COL_FIELD_NAME)
            occurrence = value(i, COL_OCCURRENCE).lower()
            types = value(i, COL_XSI_TYPES)
            values = value(i, COL_ALLOWED_VALUES)

            if occurrence not in ALLOWED_OCCURRENCES:
                err = "Found unknown occurrence '{0}' in worksheet '{1}'."
                raise errors.ProfileParseError(
                    err.format(occurrence, worksheet.name))

            rules = self._build_rules(info=instance_map[ctx_label],
                                      field=field,
                                      occurrence=occurrence,
                                      types=types,
                                      values=values)

            all_rules.extend(rules)

        return all_rules
예제 #6
0
    def _get_text_list(cls):
        slist = []

        for ordinality in range(1, 10):
            text = common.StructuredText("Ordinality %s" % ordinality)
            text.ordinality = ordinality
            slist.append(text)

        random.shuffle(slist)
        return slist
예제 #7
0
    def _get_text_list(cls):
        slist = []

        for ordinality in range(1, 10):
            text = common.StructuredText("Ordinality %s" % ordinality)
            text.ordinality = ordinality
            slist.append(text)

        random.shuffle(slist)
        return slist
예제 #8
0
    def test_reset(self):
        ords = (5, 33, 167)
        slist = common.StructuredTextList()

        for o in ords:
            text = common.StructuredText("orig: %s" % o, o)
            slist.add(text)

        # test that original assignment worked correctly
        for o in ords:
            self.assertEqual(o, slist[o].ordinality)

        # reset ordinalities
        slist.reset()

        for o in range(1, len(ords) + 1):
            self.assertEqual(o, slist[o].ordinality)
예제 #9
0
    def _parse_instance_mapping_worksheet(self, worksheet, nsmap):
        """Parses the supplied Instance Mapping worksheet and returns a
        dictionary representation.

        Args:
            worksheet: The instance mapping worksheet of the profile.
            nsmap: The namespace dictionary derived from the ``Namespace``
                worksheet of the profile.

        Returns:
            A dictionary where the key is a Profile rule context label and the
            value is an instance of the :class:`InstanceMapping`.

        """
        value = functools.partial(self._get_value, worksheet)
        is_empty = functools.partial(self._is_empty_row, worksheet)
        instance_map = {}

        def check_label(label):
            if not label:
                err = "Found empty type label in Instance Mapping worksheet"
                raise errors.ProfileParseError(err)

            if label not in instance_map:
                return

            err = ("Found duplicate type label in Instance Mapping worksheet: "
                   "'{label}'")
            raise errors.ProfileParseError(err.format(label=label))

        for row in range(1, worksheet.nrows):
            if is_empty(row):
                continue

            label = value(row, COL_LABEL)
            check_label(label)

            mapping = InstanceMapping(nsmap)
            mapping.label = label
            mapping.namespace = value(row, COL_TYPE_NAMESPACE)
            mapping.selectors = value(row, COL_SELECTORS)
            mapping.validate()

            instance_map[label] = mapping

        return instance_map
예제 #10
0
    def test_reset(self):
        ords = (5,33,167)
        slist = common.StructuredTextList()

        for o in ords:
            text =  common.StructuredText("orig: %s" % o, o)
            slist.add(text)

        # test that original assignment worked correctly
        for o in ords:
            self.assertEqual(o, slist[o].ordinality)

        # reset ordinalities
        slist.reset()

        for o in range(1, len(ords) + 1):
            self.assertEqual(o, slist[o].ordinality)
예제 #11
0
    def _parse_instance_mapping_worksheet(self, worksheet, nsmap):
        """Parses the supplied Instance Mapping worksheet and returns a
        dictionary representation.

        Args:
            worksheet: The instance mapping worksheet of the profile.
            nsmap: The namespace dictionary derived from the ``Namespace``
                worksheet of the profile.

        Returns:
            A dictionary where the key is a Profile rule context label and the
            value is an instance of the :class:`InstanceMapping`.

        """
        value        = functools.partial(self._get_value, worksheet)
        is_empty     = functools.partial(self._is_empty_row, worksheet)
        instance_map = {}

        def check_label(label):
            if not label:
                err = "Found empty type label in Instance Mapping worksheet"
                raise errors.ProfileParseError(err)

            if label not in instance_map:
                return

            err = ("Found duplicate type label in Instance Mapping worksheet: "
                   "'{label}'")
            raise errors.ProfileParseError(err.format(label=label))

        for row in range(1, worksheet.nrows):
            if is_empty(row):
                continue

            label = value(row, COL_LABEL)
            check_label(label)

            mapping = InstanceMapping(nsmap)
            mapping.label = label
            mapping.namespace = value(row, COL_TYPE_NAMESPACE)
            mapping.selectors = value(row, COL_SELECTORS)
            mapping.validate()

            instance_map[label] = mapping

        return instance_map
예제 #12
0
 def test_ordinalities(self):
     all_ords = range(1, 10)
     self.assertTrue(all(x in self.slist.ordinalities for x in all_ords))
예제 #13
0
 def test_ordinalities(self):
     all_ords = range(1,10)
     self.assertTrue(all(x in self.slist.ordinalities for x in all_ords))
예제 #14
0
    def _parse_worksheet_rules(self, worksheet, instance_map):
        """Parses the rules from the profile sheet `workheet`.

        Args:
            worksheet: A profile worksheet containing rules.
            instance_map: A dictionary representation of the ``Instance
                Mapping`` worksheet.

        Returns:
            A list of ``_BaseProfileRule`` implementations for the rules
            defined in the `worksheet`.

        Raises:
            .ProfileParseError: If a rule context label has no associated
                entry in `instance_map`.

        """
        value = functools.partial(self._get_value, worksheet)
        is_empty_row = functools.partial(self._is_empty_row, worksheet)

        def check_label(label):
            if label not in instance_map:
                err = (
                    "Worksheet '{0}' context label '{1}' has no Instance "
                    "Mapping entry."
                )
                raise errors.ProfileParseError(
                    err.format(worksheet.name, label)
                )

        all_rules = []
        for i in range(1, worksheet.nrows):
            if is_empty_row(i):
                continue

            if not value(i, COL_OCCURRENCE):
                ctx_label = value(i, COL_FIELD_NAME)
                check_label(ctx_label)
                continue

            field = value(i, COL_FIELD_NAME)
            occurrence = value(i, COL_OCCURRENCE).lower()
            types = value(i, COL_XSI_TYPES)
            values = value(i, COL_ALLOWED_VALUES)

            if occurrence not in ALLOWED_OCCURRENCES:
                err = "Found unknown occurrence '{0}' in worksheet '{1}'."
                raise errors.ProfileParseError(
                    err.format(occurrence, worksheet.name)
                )

            rules = self._build_rules(
                info=instance_map[ctx_label],
                field=field,
                occurrence=occurrence,
                types=types,
                values=values
            )

            all_rules.extend(rules)

        return all_rules