将protobuf proto文件转换为CUTE示意图类文件
protobuf-schematics的Python项目详细描述
Protobuf示意图
将protobufproto文件转换为可爱的Schematics类文件。
google protobuf对于高性能的模式感知api来说是很好的,但是当google设计protobuf时,它并没有试图使生成的代码在python中成为惯用的代码,这给在接口模块外导出消息或具有良好的ide自动完成带来了问题。schematics是一个可爱的pythonic模式库,适用于大多数应用程序。为什么不两者都加入呢
目前,这个包不支持Protobuf二进制格式,not并且将使用任何语言的原始Protobuf API轻松生成的任何其他文本表示在编写这个包时,易用性被优先考虑,而不仅仅是性能
- 免费软件:apache软件许可证2.0
- 文档:https://protobuf-schematics.readthedocs.io。
使用量
将proto文件转换为pythonSchematics类:
protobuf_schematics <path-to-file.proto> generated_schematics_proto.py # or any output filename
将protobuf消息转换为json:
在Java中:
importcom.google.protobuf.util.JsonFormat;FileWriterfile=newFileWriter("protoBufMessage.json")JsonFormat.Printerprinter=JsonFormat.printer().preservingProtoFieldNames();Stringmessage=printer.print(someProtoBufMessage);file.write(message)
或者来自Python:
importjsonfromgoogle.protobuf.json_formatimportMessageToJsonjson=MessageToJson(org,preserving_proto_field_name=True,including_default_value_fields=True)withopen("protoBufMessage.json",'w')asoutput:json.dump(json,output)
- 在项目中,在python中将消息作为Schematics对象加载:
importjsonfromgenerated_schematics_protoimportSomeMessage# import the schematics message classschematics_root_message=SomeMessage(json.load(open('protoBufMessage.json')))
或者使用Protobuf在python中加载的消息:
importjsonfromgenerated_schematics_protoimportSomeMessage# import the schematics message class# ... get your protobuf message as the pb class representationschematics_root_message=SomeMessage.import_from_protobuf_message(protobuf_message)
示例
这个proto文件:
syntax="proto3";enumIPAddressFamily{INVALID=0;IPv4=1;IPv6=2;};messageProtocolAndPorts{repeateduint32ports=3;}messageFlowFilter{enumSomeEnum{VALUE=0;};stringid=1[deprecated=true];SomeEnumconsumer_filter_id=2;map<string,ProtocolAndPorts>ports=3;repeatedProtocolAndPortsprotocol_and_ports=4;}
将转换为:
classIPAddressFamily(Enum):INVALID=0IPv4=1IPv6=2classProtocolAndPorts(ProtobufMessageModel):ports=ListType(IntType())classFlowFilter(ProtobufMessageModel):classInnerEnum(Enum):VALUE=0id=StringType()consumer_filter_id=EnumType(SomeEnum)ports=DictType(ModelType(ProtocolAndPorts),str)protocol_and_ports=ListType(ModelType(ProtocolAndPorts))
功能
- 支持protobuf语法2和3。
- 支持内置类型,如StringType,IntType
- 支持原型映射字段为SchematicsDictType。
- 支持将repeated修饰符转换为ListType。
- 支持枚举类生成和自定义示意图EnumType
- 支持从Java转换的自定义示意图ByteArrayTypebase64编码字节数组。
发展
首先,安装pip文件并创建适当的虚拟环境:
pipenv install --dev
要使用flake8检查线头,请运行:
make lint
要针对工作的python版本运行unittests,请执行以下操作:
py.test
查看覆盖率报告:
make coverage
对所有受支持的python版本运行测试:
tox
使文档(在提交到主分支时将自动发布到readthedocs):
make docs
学分
.proto文件的解析工作是由位于PyroBuf的优秀人员提供的。
这个包是用Cookiecutter和elgertam/cookiecutter-pipenv项目模板创建的,基于audreyr/cookiecutter-pypackage
历史记录
待办事项
- 添加更有意义的单元测试。
- 改进Protobuf支持和集成
- 添加对解析protobuf生成的文件的支持,因为它比当前的pyrobuf支持更全面。
读取以释放
0.4.0(2019-01-16)
- 添加了更有意义的单元测试。
- 从模板中导出类型和模型。
- 将重定义逻辑(如字段定义)从jinja模板导出到pythonic过滤器。
- 改进与protobuf的集成。
0.2.0(2019-01-14)
- 改进自述文件-添加使用示例。
- 更新setup.py以指向正式的pyrobuf 0.8.5pypi版本。
0.1.0(2019-01-13)
- pypi上的第一个版本。
- 用于将给定的proto文件编译为示意图定义文件的cli接口。