Esempio n. 1
0
    def _finalize_namespaces(self, ns_dict=None):
        """Returns a dictionary of namespaces to be exported with an XML
        document.

        This loops over all the namespaces that were discovered and built
        during the execution of ``collect()`` and
        ``_parse_collected_classes()`` and attempts to merge them all.

        Returns:
            An ``alias: namespace`` dictionary containing all namespaces
            required to be present on an exported document.

        Raises:
            .DuplicatePrefixError: If namespace prefix was mapped to more than
                one namespace.

        """
        if not ns_dict:
            ns_dict = {}

        # Copy and flip the input dictionary from ns=>alias to alias=>ns
        user_namespaces = {}
        for ns, alias in ns_dict.iteritems():
            user_namespaces[alias] = ns

        # Our return value
        ns_dict = collections.defaultdict(set)

        # Add the ID namespaces
        id_alias = idgen.get_id_namespace_alias()
        id_ns = idgen.get_id_namespace()
        ns_dict[id_alias].add(id_ns)

        # Build namespace dictionaries from the collected Entity objects.
        collected_prefixed = dict(self._collected_namespaces.iteritems())

        # Pop the unprefixed entries.
        no_prefix = collected_prefixed.pop(None, set())

        # Resolve namespace aliases for the unprefixed namespaces.
        collected_unprefixed = self._resolve_unprefixed(no_prefix)

        # Remap the example namespace to the one expected by the APIs if the
        # sample example namespace is found.
        self._fix_example_namespace()

        # All the namespaces dictionaries we need to merge and export.
        namespace_dicts = itertools.chain(
            self._BASELINE_NAMESPACES.iteritems(),
            self._input_namespaces.iteritems(), collected_prefixed.iteritems(),
            collected_unprefixed.iteritems(), user_namespaces.iteritems())

        # Build our merged namespace dictionary. It will be inspected for
        # duplicate ns prefix mappings.
        for alias, ns in namespace_dicts:
            ns_dict[alias].add(ns)

        # Check that all the prefixes are mapped to only one namespace
        self._check_namespaces(ns_dict)

        # Flatten the dictionary by popping the namespace from the namespace
        # set values in ns_dict.
        flattened = {}
        for alias, ns_set in ns_dict.iteritems():
            flattened[alias] = ns_set.pop()

        # Return the flattened dictionary
        return flattened
Esempio n. 2
0
 def test_get_id_namespace(self):
     self.assertEqual(idgen.get_id_namespace(), TEST_NS.name)
     self.assertEqual(idgen.get_id_namespace_prefix(), TEST_NS.prefix)
     self.assertEqual(idgen.get_id_namespace_alias(), TEST_NS.prefix)
Esempio n. 3
0
 def test_get_id_namespace(self):
     self.assertEqual(idgen.get_id_namespace(), TEST_NS.name)
     self.assertEqual(idgen.get_id_namespace_prefix(), TEST_NS.prefix)
     self.assertEqual(idgen.get_id_namespace_alias(), TEST_NS.prefix)
Esempio n. 4
0
    def _finalize_namespaces(self, ns_dict=None):
        """Returns a dictionary of namespaces to be exported with an XML
        document.

        This loops over all the namespaces that were discovered and built
        during the execution of ``collect()`` and
        ``_parse_collected_classes()`` and attempts to merge them all.

        Returns:
            An ``alias: namespace`` dictionary containing all namespaces
            required to be present on an exported document.

        Raises:
            .DuplicatePrefixError: If namespace prefix was mapped to more than
                one namespace.

        """
        if not ns_dict:
            ns_dict = {}

        # Copy and flip the input dictionary from ns=>alias to alias=>ns
        user_namespaces = {}
        for ns, alias in ns_dict.iteritems():
            user_namespaces[alias] = ns

        # Our return value
        ns_dict = collections.defaultdict(set)

        # Add the ID namespaces
        id_alias = idgen.get_id_namespace_alias()
        id_ns = idgen.get_id_namespace()
        ns_dict[id_alias].add(id_ns)

        # Build namespace dictionaries from the collected Entity objects.
        collected_prefixed = dict(self._collected_namespaces.iteritems())

        # Pop the unprefixed entries.
        no_prefix = collected_prefixed.pop(None, set())

        # Resolve namespace aliases for the unprefixed namespaces.
        collected_unprefixed = self._resolve_unprefixed(no_prefix)

        # Remap the example namespace to the one expected by the APIs if the
        # sample example namespace is found.
        self._fix_example_namespace()

        # All the namespaces dictionaries we need to merge and export.
        namespace_dicts = itertools.chain(
            self._BASELINE_NAMESPACES.iteritems(),
            self._input_namespaces.iteritems(),
            collected_prefixed.iteritems(),
            collected_unprefixed.iteritems(),
            user_namespaces.iteritems()
        )

        # Build our merged namespace dictionary. It will be inspected for
        # duplicate ns prefix mappings.
        for alias, ns in namespace_dicts:
            ns_dict[alias].add(ns)

        # Check that all the prefixes are mapped to only one namespace
        self._check_namespaces(ns_dict)

        # Flatten the dictionary by popping the namespace from the namespace
        # set values in ns_dict.
        flattened = {}
        for alias, ns_set in ns_dict.iteritems():
            flattened[alias] = ns_set.pop()

        # Return the flattened dictionary
        return flattened