From e3acfcd0feb2839a4e234e557d79821203333728 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Tue, 21 Sep 2021 19:37:53 +0800 Subject: [PATCH] refactor(proto-structure): update shell --- makefile | 2 ++ shell/gen_protobuf.py | 38 +++++++++++++++++++------------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/makefile b/makefile index a27866a..ad04c6b 100644 --- a/makefile +++ b/makefile @@ -3,7 +3,9 @@ STORAGE_PATH = $(BASE_PATH)/storage BASE_OUT = $(BASE_PATH)/bin dcgen: + @make gen-protobuf @python3 ./shell/proto.py + @make gen-struct .PHONY : build build: diff --git a/shell/gen_protobuf.py b/shell/gen_protobuf.py index 38c6359..5067550 100644 --- a/shell/gen_protobuf.py +++ b/shell/gen_protobuf.py @@ -1,6 +1,5 @@ import os -from typing import Dict, List -from jinja2 import Template + import yaml sysPath = os.getcwd() @@ -8,9 +7,11 @@ tempPath = f"{sysPath}/pkg/structure/generate" structurePath = f"{sysPath}/pkg/structure" protobufPath = f"{sysPath}/protobuf" + def to_camel(val: str) -> str: return "".join([k.capitalize() for k in val.split('_')]) + def load_conf(): conf_path = f"{tempPath}/tem.yaml" with open(conf_path, 'r', encoding='utf-8') as f: @@ -28,34 +29,36 @@ def load_conf(): '''生成对应的数据结构proto文件''' + + def mk_structure(cfg_camel): path = "protobuf" storagePath = f"{protobufPath}/storage.proto" for key, value in cfg_camel.items(): proto_path = path + '/' + key + '.proto' - if not os.path.exists(proto_path): # 如果这个文件不存在 + if not os.path.exists(proto_path): # 如果这个文件不存在 '''生成对应的数据结构proto文件''' - file = open(proto_path,'w') + file = open(proto_path, 'w') file.write('syntax = "proto3";\nimport "base.proto";\noption go_package = "pkg/proto";\n') for v in value: - file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}') + file.write('\nmessage ' + v + 'Request ' + '{\n BaseKey key = 1;\n}\n') file.close() else: # 如果这个文件存在 '''更新数据结构proto文件''' with open(proto_path) as f: - line = f.readlines() + line = f.readlines() f.close() for v in value: function = v + 'Request' flag = 0 for l in line: if function in l: - flag=1 + flag = 1 break if flag == 0: - file = open(proto_path,'a') - file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}') + file = open(proto_path, 'a') + file.write('\nmessage ' + v + 'Request ' + '{\n BaseKey key = 1;\n}\n') file.close() flag = 0 @@ -64,14 +67,15 @@ def mk_structure(cfg_camel): def mk_storage(cfg_camel): storagePath = f"{protobufPath}/storage.proto" - file = open(storagePath,'w') - file.write('// Code generated by gen-struct. DO NOT EDIT.\n// make gen-protobuf generated\n\nsyntax = "proto3";\n\noption go_package = "pkg/proto";\n\n') - for key,value in cfg_camel.items(): - file.write('import "'+ key +'.proto";\n') + file = open(storagePath, 'w') + file.write( + '// Code generated by gen-struct. DO NOT EDIT.\n// make gen-protobuf generated\n\nsyntax = "proto3";\n\noption go_package = "pkg/proto";\n\n') + for key, value in cfg_camel.items(): + file.write('import "' + key + '.proto";\n') file.write('\nmessage CommendResponse {\n repeated string result = 1;\n}\n\nservice CommServer {\n') - for key,value in cfg_camel.items(): + for key, value in cfg_camel.items(): for v in value: - file.write(' rpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n') + file.write(' rpc ' + v + ' (' + v + 'Request) returns (CommendResponse);\n') file.write('}') file.close() print("storage.proto", "-> success") @@ -81,7 +85,3 @@ if __name__ == "__main__": conf, cfg_camel = load_conf() mk_structure(cfg_camel) # 生成对应的数据结构proto文件 mk_storage(cfg_camel) - - - -