Initial commit

This commit is contained in:
Daniel G. Taylor
2019-10-05 08:36:23 -07:00
commit 6ed3b09f44
26 changed files with 1026 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
#!/usr/bin/env python
import os # isort: skip
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
import subprocess
import importlib
from typing import Generator, Tuple
from google.protobuf.json_format import Parse
from google.protobuf import symbol_database
from google.protobuf.descriptor_pool import DescriptorPool
root = os.path.dirname(os.path.realpath(__file__))
def get_files(end: str) -> Generator[Tuple[str, str], None, None]:
for r, dirs, files in os.walk(root):
for filename in [f for f in files if f.endswith(end)]:
parts = os.path.splitext(filename)[0].split("-")
yield [parts[0], os.path.join(r, filename)]
if __name__ == "__main__":
os.chdir(root)
for base, filename in get_files(".proto"):
subprocess.run(
f"protoc --python_out=. {os.path.basename(filename)}", shell=True
)
subprocess.run(
f"protoc --plugin=protoc-gen-custom=../../protoc-gen-betterpy.py --custom_out=. {os.path.basename(filename)}",
shell=True,
)
for base, filename in get_files(".json"):
# Reset the internal symbol database so we can import the `Test` message
# multiple times. Ugh.
sym = symbol_database.Default()
sym.pool = DescriptorPool()
imported = importlib.import_module(f"{base}_pb2")
out = filename.replace(".json", ".bin")
serialized = Parse(open(filename).read(), imported.Test()).SerializeToString()
open(out, "wb").write(serialized)

View File

@@ -0,0 +1,3 @@
{
"count": -150
}

View File

@@ -0,0 +1,3 @@
{
"count": 150
}

View File

@@ -0,0 +1,7 @@
syntax = "proto3";
// Some documentation about the Test message.
message Test {
// Some documentation about the count.
int32 count = 1;
}

View File

@@ -0,0 +1,5 @@
{
"nested": {
"count": 150
}
}

View File

@@ -0,0 +1,17 @@
syntax = "proto3";
// A test message with a nested message inside of it.
message Test {
// This is the nested type.
message Nested {
// Stores a simple counter.
int32 count = 1;
}
Nested nested = 1;
Sibling sibling = 2;
}
message Sibling {
int32 foo = 1;
}

View File

@@ -0,0 +1,3 @@
{
"names": ["one", "two", "three"]
}

View File

@@ -0,0 +1,5 @@
syntax = "proto3";
message Test {
repeated string names = 1;
}

View File

@@ -0,0 +1,3 @@
{
"counts": [1, 2, -1, -2]
}

View File

@@ -0,0 +1,5 @@
syntax = "proto3";
message Test {
repeated int32 counts = 1;
}

View File

@@ -0,0 +1,4 @@
{
"signed_32": -150,
"signed_64": -150
}

View File

@@ -0,0 +1,4 @@
{
"signed_32": 150,
"signed_64": 150
}

View File

@@ -0,0 +1,6 @@
syntax = "proto3";
message Test {
sint32 signed_32 = 1;
sint64 signed_64 = 2;
}

View File

@@ -0,0 +1,23 @@
import importlib
import pytest
import json
from generate import get_files
inputs = get_files(".bin")
@pytest.mark.parametrize("name,filename", inputs)
def test_sample(name: str, filename: str) -> None:
imported = importlib.import_module(name)
data_binary = open(filename, "rb").read()
data_dict = json.loads(open(filename.replace(".bin", ".json")).read())
t1 = imported.Test().parse(data_binary)
t2 = imported.Test().from_dict(data_dict)
print(t1)
print(t2)
assert t1 == t2
assert bytes(t1) == data_binary
assert bytes(t2) == data_binary
assert t1.to_dict() == data_dict
assert t2.to_dict() == data_dict