将protobuf proto文件转换为CUTE示意图类文件

protobuf-schematics的Python项目详细描述


Protobuf示意图

https://img.shields.io/pypi/v/protobuf_schematics.svghttps://img.shields.io/travis/AlmogCohen/protobuf-schematics.svgDocumentation Status

将protobufproto文件转换为可爱的Schematics类文件。

google protobuf对于高性能的模式感知api来说是很好的,但是当google设计protobuf时,它并没有试图使生成的代码在python中成为惯用的代码,这给在接口模块外导出消息或具有良好的ide自动完成带来了问题。schematics是一个可爱的pythonic模式库,适用于大多数应用程序。为什么不两者都加入呢

目前,这个包不支持Protobuf二进制格式,not并且将使用任何语言的原始Protobuf API轻松生成的任何其他文本表示在编写这个包时,易用性被优先考虑,而不仅仅是性能

使用量

  1. proto文件转换为pythonSchematics类:

    protobuf_schematics <path-to-file.proto> generated_schematics_proto.py # or any output filename
    
  2. 将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)
  1. 在项目中,在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的优秀人员提供的。

这个包是用Cookiecutterelgertam/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接口。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
MIME类型多部分/混合的java无对象DCH   Java Swing中单元格和列的间距   java为什么我不能使用For循环从集合中获取元素?   mysql Java餐厅计费系统   java MediaPlayer内存消耗   println Java添加的神秘输出:一些经过评估,一些经过打印   java我应该使用哪个对象?   PHP到Java论坛游戏集成   通过GeoNetwork上的java http post请求获取XML响应   java为什么类名与使用“==”的内部字符串比较冲突?   java反应。js如何定义自定义id,而不是在html中显示reactselect1value   java中的拼字数组   java如何使ScheduledExecutorService在其计划任务取消时自动终止   java查询未正确执行   IBatis+Java:检索HashMap   要读取的加密Java代码。txt文件并转换为二进制表示形式   Java Web Start应用程序自动加载   java Swing重写对象的paintComponent