示例#1
0
    def convert(self, root: Root) -> Root:
        # index operations must be before links changes
        index = NodesIndex()
        index.root = root
        index.index()

        merge_map: Dict[Package, Package] = {}  # source -> target map
        for package in walk_packages(root):
            result_name = self.rule(package)
            if result_name is not None:
                # package merging if name exists
                if result_name in package.parent.subpackages:
                    merge_map[package] = package.parent.subpackages[
                        result_name]
                else:
                    package.name = result_name

        for source, target in merge_map.items():
            del source.parent.subpackages[source.name]

            target.subpackages.update(source.subpackages)
            target.messages.update(source.messages)
            target.enums.update(source.enums)
            target.services.update(source.services)

            # TODO: fix all nested object namespace
            for item in source.children:
                for subitem in walk_children(item):
                    subitem.namespace[len(target.namespace)] = target

        self.logger.info("Convert by %s result:\n%s", self,
                         root.as_str(ident_level=1))
        return root
示例#2
0
    def convert(self, root: Root) -> Root:
        # index operations must be before links changes
        index = NodesIndex()
        index.root = root
        index.index()

        for child in walk_children(root):
            if type(child) in self.type_file_map:
                parent = child.parent
                new_package = self.get_or_create_package(child=child)
                child.namespace.insert_package(new_package)

                if isinstance(parent, Package):
                    if isinstance(child, Message):
                        new_package.messages[child.name] = child
                        del parent.messages[child.name]
                    elif isinstance(child, Enum):
                        new_package.enums[child.name] = child
                        del parent.enums[child.name]
                    elif isinstance(child, Service):
                        new_package.services[child.name] = child
                        del parent.services[child.name]
                    else:
                        raise ValueError("Bade child type: {} ({})".format(type(child), child))

        self.logger.info("Convert by %s result:\n%s", self, root.as_str(ident_level=1))
        return root
示例#3
0
    def convert(self, root: Root) -> Root:
        source_target_map: Dict[BaseNodeType, BaseNodeType] = {}
        for child in walk_children(root):
            source_target_map[child] = self.convert_node(node=child)

        # fix for links in new tree
        for child in walk_children(root):
            target = source_target_map[child]
            target.namespace = Namespace(
                [source_target_map[source] for source in child.namespace])
            target.extensions = child.extensions

            fix_func = self.fix_map.get(type(child))
            if fix_func is None:
                raise ValueError("Unknown node type `{}`".format(
                    child.__class__.__name__))
            fix_func(target=target,
                     source=child,
                     source_target_map=source_target_map)

        return source_target_map[root]
示例#4
0
    def convert(self, root: Root) -> Root:
        root.name = self.folder_name
        for child in walk_children(root):
            if isinstance(child, Service.Method):
                child.input_name = NAME_TEMPLATE.format(self.folder_name, child.input_name)
                child.output_name = NAME_TEMPLATE.format(self.folder_name, child.output_name)
            elif isinstance(child, Message.Field):
                if child.type_name is not None:
                    child.type_name = NAME_TEMPLATE.format(self.folder_name, child.type_name)
            else:
                pass

        self.logger.info("Convert by %s result:\n%s", self, root.as_str(ident_level=1))
        return root
示例#5
0
    def convert(self, root: Root) -> Root:
        source_target_map: Dict[BaseNodeType, BaseNodeType] = {}
        for child in walk_children(root):
            if type(child) in self.clone_and_cast_map:
                source_target_map[child] = self.convert_node(node=child)

        for source, target in source_target_map.items():
            target.namespace = Namespace(source.namespace)
            self.insert_link_to_parent(target=target, source=source)

            fix_func = self.fix_map.get(type(target))
            if fix_func is None:
                raise ValueError("Unknown node type `{}`".format(
                    target.__class__.__name__))
            fix_func(target=target,
                     source=source,
                     source_target_map=source_target_map)

        return source_target_map[root]
示例#6
0
    def resolve(self, root: Root):
        index = NodesIndex()
        index.root = root
        index.index()

        for child in walk_children(root):
            if isinstance(child, Message.Field):
                if child.type_name is not None:
                    child.type_obj = index.indexes[ALL]["{}.{}".format(
                        root.name, child.type_name)]
            elif isinstance(child, Service.Method):
                child.input_obj = index.indexes[ALL]["{}.{}".format(
                    root.name, child.input_name)]
                child.output_obj = index.indexes[ALL]["{}.{}".format(
                    root.name, child.output_name)]
            elif isinstance(child,
                            (Message, Service, Enum, Enum.Value, Package)):
                pass
            else:
                raise ValueError("Unknown type: %s (%s)", type(child), child)