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
def add_packages(self, root: Root, files: Dict[str, str]): for package in walk_packages(root): imports = copy(self.empty_imports) exports = copy(self.empty_exports) if package.name != "interfaces": # TODO: fix this hack by pipeline names = [] if package.messages: names.append("interfaces") if package.messages or package.enums: names.append("messages") if package.services: names.append("clients") for subpackage in package.subpackages.values(): names.append(subpackage.name) for name in names: imports.add_import( Import(type_=Import.Type.LOCAL, import_name="*", import_from="./{}".format(name), import_as=name)) exports.add_import( Export(type_=Import.Type.LOCAL, import_name=name)) files["/".join([p.name for p in package.full_namespace] + ["index.ts"])] = self.render("index.ts.jinja2", context={ "imports": imports, "exports": exports })
def add_flask_app(self, root: Root, files: Dict[str, str]): imports = copy(self.empty_imports) imports.add_import( Import(type_=Import.Type.LIBRARY, import_name="grpc")) imports.add_import( Import(type_=Import.Type.LIBRARY, import_name="ProxyApplication", import_from="horadric_lib.proxy.application")) imports.add_import( Import(type_=Import.Type.LIBRARY, import_name="CORS", import_from="flask_cors")) imports.add_import( Import( type_=Import.Type.LIBRARY, import_name="ProtocolConverterMiddleware", import_from="horadric_lib.proxy.middleware.type_converter", )) services = {} for package in walk_packages(package=root): if package.services: for service in package.services.values(): services[service.full_name] = service imports.add_import( Import(type_=Import.Type.LOCAL, import_name=render_path(service.namespace))) imports.add_import( Import(type_=Import.Type.LOCAL, import_name=render_path( service.client.namespace))) context = {"imports": imports, "services": services, "cors": self.cors} files["{}/flask_app.py".format(root.name)] = self.render( "flask_app.py.jinja2", context=context)
def add_services(self, root: Root, files: Dict[str, str]): for package in walk_packages(package=root): if package.services: self.add_servers(package=package, files=files)
def add_messages_and_enums(self, root: Root, files: Dict[str, str]) -> None: for package in walk_packages(package=root): if package.enums or package.messages: self.add_messages(package=package, files=files) pass