Beispiel #1
0
    def expand_attribute_group(self, target: Class, attr: Attr):
        """
        Expand a group attribute with the source class attributes.

        Clone the attributes and apply the group restrictions as well.
        """

        if not attr.is_group:
            return

        attr_qname = target.source_qname(attr.name)
        source = self.find_class(attr_qname)

        if not source:
            raise AnalyzerError(f"Group attribute not found: `{attr_qname}`")

        if source is target:
            target.attrs.remove(attr)
        else:
            index = target.attrs.index(attr)
            target.attrs.pop(index)
            prefix = text.prefix(attr.name)

            for source_attr in source.attrs:
                clone = self.clone_attribute(source_attr, attr.restrictions,
                                             prefix)
                target.attrs.insert(index, clone)
                index += 1

            self.copy_inner_classes(source, target)
Beispiel #2
0
    def copy_attributes(cls, source: Class, target: Class,
                        extension: Extension):
        """
        Copy the attributes and inner classes from the source class to the
        target class and remove the extension that links the two classes
        together.

        The new attributes are prepended in the list unless if they are
        supposed to be last in a sequence.
        """
        prefix = text.prefix(extension.type.name)
        target.extensions.remove(extension)
        target_attr_names = {text.suffix(attr.name) for attr in target.attrs}

        index = 0
        for attr in source.attrs:
            if text.suffix(attr.name) not in target_attr_names:
                clone = cls.clone_attribute(attr, extension.restrictions,
                                            prefix)

                if attr.index == sys.maxsize:
                    target.attrs.append(clone)
                    continue

                target.attrs.insert(index, clone)
            index += 1

        cls.copy_inner_classes(source, target)
Beispiel #3
0
    def copy_group_attributes(cls, source: Class, target: Class, attr: Attr):
        """Copy the attributes and inner classes from the source class to the
        target class and remove the group attribute that links the two classes
        together."""
        index = target.attrs.index(attr)
        target.attrs.pop(index)
        prefix = text.prefix(attr.name)

        for source_attr in source.attrs:
            clone = cls.clone_attribute(source_attr, attr.restrictions, prefix)
            target.attrs.insert(index, clone)
            index += 1

        cls.copy_inner_classes(source, target)
Beispiel #4
0
    def copy_attributes(cls, source: Class, target: Class,
                        extension: Extension):
        prefix = text.prefix(extension.type.name)
        target.extensions.remove(extension)
        target_attr_names = {text.suffix(attr.name) for attr in target.attrs}

        index = 0
        for attr in source.attrs:
            if text.suffix(attr.name) not in target_attr_names:
                clone = cls.clone_attribute(attr, extension.restrictions,
                                            prefix)

                if attr.index == sys.maxsize:
                    target.attrs.append(clone)
                    continue

                target.attrs.insert(index, clone)
            index += 1

        cls.copy_inner_classes(source, target)
Beispiel #5
0
 def prefix(self):
     return text.prefix(self.ref) if self.is_ref else None
Beispiel #6
0
 def prefix(self) -> Optional[str]:
     ref = getattr(self, "ref", None)
     return None if ref is None else text.prefix(ref)
Beispiel #7
0
 def prefix(self) -> Optional[str]:
     """Return the namespace prefix for this element's type."""
     ref = getattr(self, "ref", None)
     return None if ref is None else text.prefix(ref)