{"id":18548390,"url":"https://github.com/decitre/python-proto-topy","last_synced_at":"2026-02-24T16:33:37.962Z","repository":{"id":57479168,"uuid":"479962828","full_name":"decitre/python-proto-topy","owner":"decitre","description":"Compile .proto string to module object","archived":false,"fork":false,"pushed_at":"2024-08-29T10:01:02.000Z","size":121,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-18T16:56:33.756Z","etag":null,"topics":["protobuf","protobuf-compiler","protobuf-definitions"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/decitre.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-10T08:54:23.000Z","updated_at":"2024-08-29T09:53:41.000Z","dependencies_parsed_at":"2024-02-03T16:27:03.285Z","dependency_job_id":"3b976a94-7c86-4c10-aa2b-d1b47580d8ae","html_url":"https://github.com/decitre/python-proto-topy","commit_stats":null,"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"purl":"pkg:github/decitre/python-proto-topy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decitre%2Fpython-proto-topy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decitre%2Fpython-proto-topy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decitre%2Fpython-proto-topy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decitre%2Fpython-proto-topy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/decitre","download_url":"https://codeload.github.com/decitre/python-proto-topy/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decitre%2Fpython-proto-topy/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260288472,"owners_count":22986667,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["protobuf","protobuf-compiler","protobuf-definitions"],"created_at":"2024-11-06T20:34:25.333Z","updated_at":"2025-10-30T19:43:45.562Z","avatar_url":"https://github.com/decitre.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![test][test_badge]][test_target]\n[![version][version_badge]][pypi]\n[![wheel][wheel_badge]][pypi]\n[![python version][python_versions_badge]][pypi]\n[![python implementation][python_implementation_badge]][pypi]\n\nA Python package that\n- takes a `str` containing protobuf messages definitions\n- returns a `types.ModuleType` instance\n\nIt is useful for programs needing to en/decode protobuf messages for which the definition is provided as a string at runtime.\n\n## Installation\n\n    pip install proto-topy\n\nPrerequisite: `proto-topy` needs [protoc][protoc] to be installed. On macOS, a simple `brew install protobuf` shall suffice.\n\n## single proto example: address book\n\nAdaptation of the `protocolbuffers` [example](https://github.com/protocolbuffers/protobuf/tree/main/examples):\n\n```python\nimport requests\nfrom pathlib import Path\nfrom proto_topy import ProtoModule\n\n# Retrieve protobuf messages definitions as a string\nexample_source = requests.get(\n    \"https://raw.githubusercontent.com/protocolbuffers/protobuf/main/\"\n    \"examples/addressbook.proto\").text\n\nexample_path = Path(\n    \"protocolbuffers/protobuf/blob/main/examples/addressbook.proto\")\n\n# Compile and import\nmodule = ProtoModule(file_path=example_path, source=example_source).compiled()\n\n# Produce a serialized address book\naddress_book = module.py.AddressBook()\nperson = address_book.people.add()\nperson.id = 111\nperson.name = \"A Name\"\nperson.email = \"a.name@mail.com\"\nphone_number = person.phones.add()\nphone_number.number = \"+1234567\"\nphone_number.type = module.py.Person.MOBILE\nwith open(\"address_book.data\", \"wb\") as o:\n    o.write(address_book.SerializeToString())\n\n# Use a serialized address book\naddress_book = module.py.AddressBook()\nwith open(\"address_book.data\", \"rb\") as i:\n    address_book.ParseFromString(i.read())\n    for person in address_book.people:\n        print(person.id, person.name, person.email, phone_number.number)\n```\n\n## multiple protos example\n\nWhen several `.proto` need to be considered, use a `ProtoCollection`:\n\n```python\nimport sys\nfrom pathlib import Path\nfrom proto_topy import ProtoModule, ProtoCollection\n\nmodule1 = ProtoModule(\n    file_path=Path(\"p1/p2/other2.proto\"),\n    source=\"\"\"\n    syntax = \"proto3\";\n    import \"google/protobuf/timestamp.proto\";\n    message OtherThing2 {\n        google.protobuf.Timestamp created = 1;\n    };\"\"\"\n)\n\nmodule2 = ProtoModule(\n    file_path=Path(\"p3/p4/test6.proto\"),\n    source=\"\"\"\n    syntax = \"proto3\";\n    import \"p1/p2/other2.proto\";\n    message Test6 {\n        OtherThing2 foo = 1;\n    };\"\"\"\n)\n\ncollection = ProtoCollection(module1, module2).compiled()\nsys.modules.update({proto.name: proto.py\n                    for proto in collection.modules.values()})\nprint(sys.modules['test6'].Test6,\n      sys.modules['other2'].OtherThing2)\n```\n## Stream of delimited messages\n\nTo decode a stream of contiguous protobuf messages of the same type, use `DelimitedMessageFactory`. Example:\n\n```python\nfrom io import BytesIO\nfrom pathlib import Path\nfrom proto_topy import ProtoModule, DelimitedMessageFactory\n\n# Generate Python module\nmodule = ProtoModule(\n    file_path=Path(\"int32_streams.proto\"),\n    source=\"\"\"\n    syntax = \"proto3\";\n    message TestInt { int32 val = 1; };\"\"\"\n).compiled()\n\n# Feed a DelimitedMessageFactory with a sequence of TestInt instances for a range of 10 ints\nintegers = (module.py.TestInt(val=val) for val in range(10))\nfactory = DelimitedMessageFactory(BytesIO(), *integers)\n\n# Rewind and read the stream of 10 protobuf messages\nfactory.rewind()\nfor offset_val in factory.message_read(module.py.TestInt):\n    print(f\"TestInt message of val set to {offset_val[1]}\")\n```\n\n\n\n[pypi]: https://pypi.org/project/proto-topy\n[test_badge]: https://github.com/decitre/python-proto-topy/actions/workflows/test.yml/badge.svg\n[test_target]: https://github.com/decitre/python-proto-topy/actions\n[version_badge]: https://img.shields.io/pypi/v/proto-topy.svg\n[wheel_badge]: https://img.shields.io/pypi/wheel/proto-topy.svg\n[python_versions_badge]: https://img.shields.io/pypi/pyversions/proto-topy.svg\n[python_implementation_badge]: https://img.shields.io/pypi/implementation/proto-topy.svg\n[tests]: tests/test_proto_topy.py\n[protoc]: https://protobuf.dev/getting-started/pythontutorial/#compiling-protocol-buffers\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdecitre%2Fpython-proto-topy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdecitre%2Fpython-proto-topy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdecitre%2Fpython-proto-topy/lists"}