示例#1
0
    def test_copy_attributes(self, mock_clone_attribute,
                             mock_copy_inner_classes):
        mock_clone_attribute.side_effect = lambda x, y, z: x.clone()
        target = ClassFactory.create(attrs=[
            AttrFactory.create(name="foo:a"),
            AttrFactory.create(name="b")
        ])
        source = ClassFactory.create(attrs=[
            AttrFactory.create(name="c", index=sys.maxsize),
            AttrFactory.create(name="a"),
            AttrFactory.create(name="boo:b"),
            AttrFactory.create(name="d"),
        ])
        extension = ExtensionFactory.create(type=AttrTypeFactory.create(
            name="foo:foo"))
        target.extensions.append(extension)

        ClassUtils.copy_attributes(source, target, extension)

        self.assertEqual(["foo:a", "b", "d", "c"],
                         [attr.name for attr in target.attrs])
        mock_copy_inner_classes.assert_called_once_with(source, target)
        mock_clone_attribute.assert_has_calls([
            mock.call(source.attrs[0], extension.restrictions, "foo"),
            mock.call(source.attrs[3], extension.restrictions, "foo"),
        ])
示例#2
0
    def process_complex_extension(cls, source: Class, target: Class, ext: Extension):
        """
        Complex flatten extension handler for primary classes eg ComplexType,
        Element.

        Compare source and target classes and either remove the
        extension completely, copy all source attributes to the target
        class or leave the extension alone.
        """
        if cls.should_remove_extension(source, target):
            target.extensions.remove(ext)
        elif cls.should_flatten_extension(source, target):
            ClassUtils.copy_attributes(source, target, ext)
        else:
            ext.type.reference = id(source)
            logger.debug("Ignore extension: %s", ext.type.name)
示例#3
0
    def merge_redefined_type(cls, source: Class, target: Class):
        """
        Copy any attributes and extensions to redefined types from the original
        definitions.

        Redefined inheritance is optional search for self references in
        extensions and attribute groups.
        """
        circular_extension = cls.find_circular_extension(target)
        circular_group = cls.find_circular_group(target)

        if circular_extension:
            ClassUtils.copy_attributes(source, target, circular_extension)
            ClassUtils.copy_extensions(source, target, circular_extension)

        if circular_group:
            ClassUtils.copy_group_attributes(source, target, circular_group)
示例#4
0
    def process_complex_extension(cls, source: Class, target: Class,
                                  ext: Extension):
        """
        Complex flatten extension handler for primary classes eg ComplexType,
        Element.

        Compare source and target classes and either remove the
        extension completely, copy all source attributes to the target
        class or leave the extension alone.
        """
        res = cls.compare_attributes(source, target)
        if res == cls.REMOVE_EXTENSION:
            target.extensions.remove(ext)
        elif res == cls.FLATTEN_EXTENSION:
            ClassUtils.copy_attributes(source, target, ext)
        else:
            logger.debug("Ignore extension: %s", ext.type.name)
示例#5
0
    def process_simple_extension(cls, source: Class, target: Class, ext: Extension):
        """
        Simple flatten extension handler for common classes eg SimpleType,
        Restriction.

        Steps:
            1. If target is source: drop the extension.
            2. If source is enumeration and target isn't create default value attribute.
            3. If both source and target are enumerations copy all attributes.
            4. If both source and target are not enumerations copy all attributes.
            5. If target is enumeration: drop the extension.
        """
        if source is target:
            target.extensions.remove(ext)
        elif source.is_enumeration and not target.is_enumeration:
            cls.add_default_attribute(target, ext)
        elif source.is_enumeration == target.is_enumeration:
            ClassUtils.copy_attributes(source, target, ext)
        else:  # this is an enumeration
            target.extensions.remove(ext)
示例#6
0
    def process_complex_extension(cls, source: Class, target: Class,
                                  ext: Extension):
        """
        Complex flatten extension handler for primary classes eg ComplexType,
        Element.

        Drop extension when:
            - source includes all target attributes
        Copy all attributes when:
            - source includes some of the target attributes
            - source has suffix attribute and target has at least one attribute
            - target has at least one suffix attribute
            - source is marked as strict type
        """
        res = cls.compare_attributes(source, target)
        if res == cls.INCLUDES_ALL:
            target.extensions.remove(ext)
        elif (res == cls.INCLUDES_SOME or source.strict_type
              or (source.has_suffix_attr and len(target.attrs) > 0)
              or target.has_suffix_attr):
            ClassUtils.copy_attributes(source, target, ext)