From 8fc7e80c0c4bb1e38af143465162616183147dac Mon Sep 17 00:00:00 2001 From: decitre <590094+decitre@users.noreply.github.com> Date: Mon, 5 Feb 2024 19:37:38 +0100 Subject: [PATCH] 2nd breaking change: rename ProtoCollection.compile() to compiled() --- README.md | 20 ++++++++------------ src/proto_topy.py | 4 ++-- tests/test_proto_topy.py | 10 +++++----- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index b884282..7f2eb3d 100644 --- a/README.md +++ b/README.md @@ -21,12 +21,10 @@ Adaptation of the `protocolbuffers` [example](https://github.com/protocolbuffers ```python import requests -import sys -from shutil import which -from proto_topy import ProtoModule from pathlib import Path +from proto_topy import ProtoModule -# Retrieve protobuf messages definitions +# Retrieve protobuf messages definitions as a string example_source = requests.get( "https://raw.githubusercontent.com/protocolbuffers/protobuf/main/" "examples/addressbook.proto").text @@ -35,9 +33,7 @@ example_path = Path( "protocolbuffers/protobuf/blob/main/examples/addressbook.proto") # Compile and import -module = (ProtoModule(file_path=example_path, source=example_source) - .compiled(Path(which("protoc")))) -sys.modules["addressbook"] = module.py +module = ProtoModule(file_path=example_path, source=example_source).compiled() # Produce a serialized address book address_book = module.py.AddressBook() @@ -65,8 +61,8 @@ When several `.proto` need to be considered, use a `ProtoCollection`: ```python import sys -from proto_topy import ProtoModule, ProtoCollection from pathlib import Path +from proto_topy import ProtoModule, ProtoCollection module1 = ProtoModule( file_path=Path("p1/p2/other2.proto"), @@ -88,10 +84,10 @@ module2 = ProtoModule( };""" ) -collection = ProtoCollection(module1, module2).compile() -sys.modules.update({proto.name: proto.py +collection = ProtoCollection(module1, module2).compiled() +sys.modules.update({proto.name: proto.py for proto in collection.modules.values()}) -print(sys.modules['test6'].Test6, +print(sys.modules['test6'].Test6, sys.modules['other2'].OtherThing2) ``` ## Stream of delimited messages @@ -99,8 +95,8 @@ print(sys.modules['test6'].Test6, To decode a stream of contiguous protobuf messages of the same type, use `DelimitedMessageFactory`. Example: ```python -from pathlib import Path from io import BytesIO +from pathlib import Path from proto_topy import ProtoModule, DelimitedMessageFactory # Generate Python module diff --git a/src/proto_topy.py b/src/proto_topy.py index 3073778..7656843 100644 --- a/src/proto_topy.py +++ b/src/proto_topy.py @@ -79,7 +79,7 @@ def compiled(self, compiler_path: Path = None) -> "ProtoModule": """ collection = ProtoCollection(self) - collection.compile(compiler_path=compiler_path) + collection.compiled(compiler_path=compiler_path) return self @@ -130,7 +130,7 @@ def _get_compiler_path() -> Path: raise FileNotFoundError("protoc compiler not found") return compiler_path - def compile( + def compiled( self, compiler_path: Path = None, global_scope: dict = None ) -> "ProtoCollection": if not compiler_path: diff --git a/tests/test_proto_topy.py b/tests/test_proto_topy.py index b470ebf..87ea099 100644 --- a/tests/test_proto_topy.py +++ b/tests/test_proto_topy.py @@ -64,7 +64,7 @@ def test_add_proto2(): def test_bad_protoc(): dummy = unlink_proto_file("dummy") with pytest.raises(FileNotFoundError): - ProtoCollection().compile(compiler_path=dummy) + ProtoCollection().compiled(compiler_path=dummy) unlink_proto_file("dummy") @@ -83,7 +83,7 @@ def test_compile_redundant_proto(): proto1 = ProtoModule(file_path=testr_proto, source=proto_source) proto2 = ProtoModule(file_path=testr_proto, source=proto_source) with pytest.raises(KeyError, match=r"testr.proto already added"): - ProtoCollection(proto1, proto2).compile(compiler_path=protoc_path) + ProtoCollection(proto1, proto2).compiled(compiler_path=protoc_path) unlink_proto_file("testr.proto") @@ -166,7 +166,7 @@ def test_compile_ununsed_dependency(): ) modules = ProtoCollection(proto_module, other_proto_module) try: - modules.compile(compiler_path=protoc_path) + modules.compiled(compiler_path=protoc_path) except CompilationFailed: pytest.fail("Unexpected CompilationFailed ..") unlink_proto_file("test.proto") @@ -200,7 +200,7 @@ def test_compile_simple_dependency(): """, ) modules = ProtoCollection(proto_module, other_proto_module) - modules.compile(compiler_path=protoc_path) + modules.compiled(compiler_path=protoc_path) sys.modules.update({proto.name: proto.py for proto in modules.modules.values()}) atest6 = modules.modules[test_proto].py.Test6() assert isinstance(atest6.foo.created, Timestamp) @@ -218,7 +218,7 @@ def test_encode_message(): proto1 = ProtoModule(file_path=test7_proto, source=proto_source.format(n=7)) proto2 = ProtoModule(file_path=test8_proto, source=proto_source.format(n=8)) - ProtoCollection(proto1, proto2).compile(compiler_path=protoc_path) + ProtoCollection(proto1, proto2).compiled(compiler_path=protoc_path) assert array("B", proto1.py.Test7(foo=124).SerializeToString()) == array( "B", [8, 124] )