diff --git a/gen-requirements.txt b/scripts/gen-requirements.txt similarity index 100% rename from gen-requirements.txt rename to scripts/gen-requirements.txt diff --git a/scripts/plugin.py b/scripts/plugin.py index 5740236..82345cd 100755 --- a/scripts/plugin.py +++ b/scripts/plugin.py @@ -32,6 +32,17 @@ # Inline the size function for a given proto message field def inline_size_function(proto_type: str, field_name: str, field_tag: str) -> str: + """ + For example: + + class MessageMarshaler: + def size_uint32(self, TAG: bytes, FIELD_ATTR: int) -> int: + return len(TAG) + Varint.size_varint_u32(FIELD_ATTR) + + Becomes: + + size += len(b"\x10") + Varint.size_varint_u32(self.int_value) + """ function_definition = inspect.getsource(globals()["MessageMarshaler"].__dict__[f"size_{proto_type}"]) # Remove the function header and unindent the function body function_definition = function_definition.splitlines()[1:] @@ -47,6 +58,19 @@ def inline_size_function(proto_type: str, field_name: str, field_tag: str) -> st # Inline the serialization function for a given proto message field def inline_serialize_function(proto_type: str, field_name: str, field_tag: str) -> str: + """ + For example: + + class MessageMarshaler: + def serialize_uint32(self, out: BytesIO, TAG: bytes, FIELD_ATTR: int) -> None: + out.write(TAG) + Varint.serialize_varint_u32(out, FIELD_ATTR) + + Becomes: + + out.write(b"\x10") + Varint.serialize_varint_u32(out, self.int_value) + """ function_definition = inspect.getsource(globals()["MessageMarshaler"].__dict__[f"serialize_{proto_type}"]) # Remove the function header and unindent the function body function_definition = function_definition.splitlines()[1:] diff --git a/scripts/proto_codegen.sh b/scripts/proto_codegen.sh index f067122..9db288f 100755 --- a/scripts/proto_codegen.sh +++ b/scripts/proto_codegen.sh @@ -32,7 +32,7 @@ echo "Creating temporary virtualenv at $venv_dir using $(python3 --version)" python3 -m venv $venv_dir source $venv_dir/bin/activate python -m pip install \ - -c $repo_root/gen-requirements.txt \ + -c $repo_root/scripts/gen-requirements.txt \ protobuf Jinja2 grpcio-tools black isort . echo 'python -m grpc_tools.protoc --version'