Skip to content

Commit

Permalink
2nd breaking change: rename ProtoCollection.compile() to compiled()
Browse files Browse the repository at this point in the history
  • Loading branch information
decitre committed Feb 5, 2024
1 parent 3671eaa commit 8fc7e80
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
20 changes: 8 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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"),
Expand All @@ -88,19 +84,19 @@ 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

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
Expand Down
4 changes: 2 additions & 2 deletions src/proto_topy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_proto_topy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Expand All @@ -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")


Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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]
)
Expand Down

0 comments on commit 8fc7e80

Please sign in to comment.