diff --git a/pyproject.toml b/pyproject.toml index 3a76fd8..1e80e19 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pyprotostuben" -version = "0.1.0" +version = "0.1.1" description = "" authors = ["zerlok "] readme = "README.md" diff --git a/src/pyprotostuben/codegen/abc.py b/src/pyprotostuben/codegen/abc.py index 9f3e0fe..17fdc12 100644 --- a/src/pyprotostuben/codegen/abc.py +++ b/src/pyprotostuben/codegen/abc.py @@ -15,5 +15,5 @@ def run(self, request: CodeGeneratorRequest) -> CodeGeneratorResponse: class ProtoFileGenerator(metaclass=abc.ABCMeta): @abc.abstractmethod - def run(self, file: ProtoFile) -> t.Iterable[t.Tuple[ProtoFile, Path, str]]: + def run(self, file: ProtoFile) -> t.Sequence[t.Tuple[ProtoFile, Path, str]]: raise NotImplementedError diff --git a/src/pyprotostuben/codegen/module_ast.py b/src/pyprotostuben/codegen/module_ast.py index a692436..c9f59d9 100644 --- a/src/pyprotostuben/codegen/module_ast.py +++ b/src/pyprotostuben/codegen/module_ast.py @@ -4,7 +4,7 @@ from pathlib import Path from pyprotostuben.codegen.abc import ProtoFileGenerator -from pyprotostuben.logging import LoggerMixin +from pyprotostuben.logging import Logger, LoggerMixin from pyprotostuben.protobuf.file import ProtoFile from pyprotostuben.protobuf.visitor.abc import visit from pyprotostuben.protobuf.visitor.decorator import ProtoVisitorDecorator @@ -21,7 +21,7 @@ class ModuleASTBasedProtoFileGenerator(ProtoFileGenerator, LoggerMixin): def __init__(self, factory: ModuleASTProtoVisitorDecoratorFactory) -> None: self.__factory = factory - def run(self, file: ProtoFile) -> t.Iterable[t.Tuple[ProtoFile, Path, str]]: + def run(self, file: ProtoFile) -> t.Sequence[t.Tuple[ProtoFile, Path, str]]: log = self._log.bind_details(file_name=file.name) log.debug("file received") @@ -33,6 +33,14 @@ def run(self, file: ProtoFile) -> t.Iterable[t.Tuple[ProtoFile, Path, str]]: visit(DFSWalkingProtoVisitor(generator), file.descriptor) log.debug("proto visited", modules=modules) + return list(self.__gen_modules(file, modules, log)) + + def __gen_modules( + self, + file: ProtoFile, + modules: t.Mapping[Path, ast.Module], + log: Logger, + ) -> t.Iterable[t.Tuple[ProtoFile, Path, str]]: for path, module_ast in modules.items(): if not module_ast.body: continue diff --git a/src/pyprotostuben/logging.py b/src/pyprotostuben/logging.py index 18bd426..4669ffb 100644 --- a/src/pyprotostuben/logging.py +++ b/src/pyprotostuben/logging.py @@ -8,7 +8,11 @@ from logging.config import dictConfig from pathlib import Path -from typing_extensions import Self +if t.TYPE_CHECKING: + from typing_extensions import Self + +else: + Self = t.Any class Logger( @@ -39,7 +43,7 @@ def configure(cls) -> None: }, }, "root": { - "level": os.getenv("LOGGING_LEVEL", "INFO").strip().upper(), + "level": os.getenv("LOGGING_LEVEL", "WARNING").strip().upper(), "handlers": ["stderr"], }, } diff --git a/src/pyprotostuben/protobuf/context.py b/src/pyprotostuben/protobuf/context.py index 969b124..391ca0c 100644 --- a/src/pyprotostuben/protobuf/context.py +++ b/src/pyprotostuben/protobuf/context.py @@ -50,7 +50,7 @@ def build(cls, request: CodeGeneratorRequest) -> CodeGeneratorContext: return CodeGeneratorContext( request=request, params=parser.parse(request.parameter), - files=[files[file.name] for file in request.proto_file], + files=[files[name] for name in request.file_to_generate], registry=TypeRegistry(infos, map_entries), ) diff --git a/tests/integration/cases/004_message_import/expected_gen/message_import_pb2.pyi b/tests/integration/cases/004_message_import/expected_gen/message_import_pb2.pyi index 6af9aca..bc816db 100644 --- a/tests/integration/cases/004_message_import/expected_gen/message_import_pb2.pyi +++ b/tests/integration/cases/004_message_import/expected_gen/message_import_pb2.pyi @@ -1,5 +1,6 @@ import builtins import google.protobuf.message +import google.protobuf.timestamp_pb2 import message1_pb2 import message2_pb2 import message3_pb2 @@ -7,7 +8,7 @@ import typing class MessageImport(google.protobuf.message.Message): - def __init__(self, *, m1: message1_pb2.Message1, m2: message2_pb2.Message2, m3: message3_pb2.Message3) -> None:... + def __init__(self, *, m1: message1_pb2.Message1, m2: message2_pb2.Message2, m3: message3_pb2.Message3, ts: google.protobuf.timestamp_pb2.Timestamp) -> None:... @builtins.property def m1(self) -> message1_pb2.Message1:... @@ -18,6 +19,9 @@ class MessageImport(google.protobuf.message.Message): @builtins.property def m3(self) -> message3_pb2.Message3:... + @builtins.property + def ts(self) -> google.protobuf.timestamp_pb2.Timestamp:... + def HasField(self, field_name: typing.NoReturn) -> typing.NoReturn:... def WhichOneof(self, oneof_group: typing.NoReturn) -> typing.NoReturn:... \ No newline at end of file diff --git a/tests/integration/cases/004_message_import/proto/message_import.proto b/tests/integration/cases/004_message_import/proto/message_import.proto index eff23a9..e785469 100644 --- a/tests/integration/cases/004_message_import/proto/message_import.proto +++ b/tests/integration/cases/004_message_import/proto/message_import.proto @@ -5,9 +5,11 @@ package message_import; import "message1.proto"; import "message2.proto"; import "message3.proto"; +import "google/protobuf/timestamp.proto"; message MessageImport { message_import.Message1 m1 = 1; message_import.Message2 m2 = 2; message3.Message3 m3 = 3; + google.protobuf.Timestamp ts = 4; }