From 8bc30d3b85ef97447c51ab2cf830412cbb8ba780 Mon Sep 17 00:00:00 2001 From: yu_lang <534013339@qq.com> Date: Mon, 20 Sep 2021 21:04:01 +0800 Subject: [PATCH 1/6] feat(proto):add proto auto tool --- protobuf/listx.proto | 22 ++++++++ protobuf/storage.proto | 20 ++++++-- shell/gen_protobuf.py | 113 ++++++++++++++++++++++++++++------------- 3 files changed, 115 insertions(+), 40 deletions(-) create mode 100644 protobuf/listx.proto diff --git a/protobuf/listx.proto b/protobuf/listx.proto new file mode 100644 index 0000000..21effe8 --- /dev/null +++ b/protobuf/listx.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; +import "base.proto"; +option go_package = "pkg/proto"; + +message LSetRequest { + BaseKey key = 1; +} +message LGetRequest { + BaseKey key = 1; +} +message LAddRequest { + BaseKey key = 1; +} +message LQueueRequest { + BaseKey key = 1; +} +message LSetbitRequest { + BaseKey key = 1; +} +message LGetbitRequest { + BaseKey key = 1; +} diff --git a/protobuf/storage.proto b/protobuf/storage.proto index b1f024e..afd9aa8 100644 --- a/protobuf/storage.proto +++ b/protobuf/storage.proto @@ -2,12 +2,24 @@ syntax = "proto3"; option go_package = "pkg/proto"; import "stringx.proto"; - +import "listx.proto"; message CommendResponse { - repeated string result = 1; + repeated string result = 1; } service CommServer { - rpc Get (GetRequest) returns (CommendResponse); - rpc Set (SetRequest) returns (CommendResponse); + + rpc Set (SetRequest) returns (CommendResponse); + rpc Get (GetRequest) returns (CommendResponse); + rpc Add (AddRequest) returns (CommendResponse); + rpc Reduce (ReduceRequest) returns (CommendResponse); + rpc Setbit (SetbitRequest) returns (CommendResponse); + rpc Getbit (GetbitRequest) returns (CommendResponse); + rpc LSet (LSetRequest) returns (CommendResponse); + rpc LGet (LGetRequest) returns (CommendResponse); + rpc LAdd (LAddRequest) returns (CommendResponse); + rpc LQueue (LQueueRequest) returns (CommendResponse); + rpc LSetbit (LSetbitRequest) returns (CommendResponse); + rpc LGetbit (LGetbitRequest) returns (CommendResponse); + } \ No newline at end of file diff --git a/shell/gen_protobuf.py b/shell/gen_protobuf.py index f60b99d..e09659a 100644 --- a/shell/gen_protobuf.py +++ b/shell/gen_protobuf.py @@ -32,45 +32,86 @@ def load_conf(): # 生成文件,并写入 def mkdir(cfg_camel): path = "protobuf" - path = path.strip() - # 判断路径是否存在 - isExists = os.path.exists(path) - # 判断结果 - if not isExists: - os.makedirs(path) - print(path + ' 创建成功') - else: # 如果目录存在则不创建,并提示目录已存在 - # print(path + ' 目录已存在') - for key,value in cfg_camel.items(): -# print(key) - proto_path = path + '/' + key + '.proto' - if not os.path.exists(proto_path): # 如果这个文件不存在 - 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}\n') - file.close() - else: # 如果这个文件存在 - with open(proto_path) as f: - line = f.readlines() -# print(line) - f.close() - for v in value: - function = v + 'Request' - flag = 0 - for l in line: - if function in l: - flag=1 - break - if flag == 0: - file = open(proto_path,'a') - file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}\n') - file.close() - flag = 0 + storagePath = f"{protobufPath}/storage.proto" + for key,value in cfg_camel.items(): + '''生成、更新数据结构proto文件''' + proto_path = path + '/' + key + '.proto' + if not os.path.exists(proto_path): # 如果这个文件不存在 + '''生成对应的数据结构proto文件''' + 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\tBaseKey key = 1;\n}') + file.close() + '''给strong.proto添加import该新的数据结构''' + import_structure = 'import "'+ key +'.proto";\n' + mess = 'message CommendResponse {\n' + with open(storagePath) as f: + line = f.readlines() +# print(line) + f.close + for i in range(len(line)): + if mess in line[i]: + break + line.insert(i,import_structure) + file = open(storagePath,'w') + for l in line: + file.write(l) + file.close() + i=0 + for i in range(len(line)-1,0,-1): + if '}' in line[i]: + break + for v in value: + line.insert(i,'\trpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n') + file = open(storagePath,'w') + for l in line: + file.write(l) + file.close() + else: # 如果这个文件存在 + with open(proto_path) as f: + line = f.readlines() +# print(line) + f.close() + for v in value: + function = v + 'Request' + flag = 0 + for l in line: + if function in l: + flag=1 + break + if flag == 0: + file = open(proto_path,'a') + file.write('\nmessage ' + v +'Request '+'{\n\tBaseKey key = 1;\n}') + file.close() + flag = 0 + '''更新storage.proto文件''' + with open(storagePath) as f: + line = f.readlines() + f.close + for v in value: + flg=0 + for l in range(len(line)): + if '\trpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n' in line[l]: + flg = 1 + break + if flg==0: + i=0 + for i in range(len(line)-1,0,-1): + if '}' in line[i]: + break + line.insert(i,'\trpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n') + i=0 + file = open(storagePath,'w') + for l in line: + file.write(l) + file.close() + if __name__ == "__main__": conf, cfg_camel = load_conf() -# + print(cfg_camel) mkdir(cfg_camel) + From cf4b5cd63c19b749fc1f0ecc603a2ce13d78b609 Mon Sep 17 00:00:00 2001 From: yu_lang <534013339@qq.com> Date: Tue, 21 Sep 2021 09:44:22 +0800 Subject: [PATCH 2/6] feat(proto):add proto auto_tool --- protobuf/listx.proto | 1 - protobuf/storage.proto | 1 - shell/gen_protobuf.py | 4 +--- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/protobuf/listx.proto b/protobuf/listx.proto index 21effe8..85a6b5c 100644 --- a/protobuf/listx.proto +++ b/protobuf/listx.proto @@ -1,7 +1,6 @@ syntax = "proto3"; import "base.proto"; option go_package = "pkg/proto"; - message LSetRequest { BaseKey key = 1; } diff --git a/protobuf/storage.proto b/protobuf/storage.proto index afd9aa8..7a50c4d 100644 --- a/protobuf/storage.proto +++ b/protobuf/storage.proto @@ -8,7 +8,6 @@ message CommendResponse { } service CommServer { - rpc Set (SetRequest) returns (CommendResponse); rpc Get (GetRequest) returns (CommendResponse); rpc Add (AddRequest) returns (CommendResponse); diff --git a/shell/gen_protobuf.py b/shell/gen_protobuf.py index e09659a..9e7a02a 100644 --- a/shell/gen_protobuf.py +++ b/shell/gen_protobuf.py @@ -24,8 +24,6 @@ def load_conf(): key = to_camel(key).lower() cfg_camel[key] = [to_camel(v) for v in val] -# print(cfg_camel) - return cfg, cfg_camel @@ -73,7 +71,6 @@ def mkdir(cfg_camel): else: # 如果这个文件存在 with open(proto_path) as f: line = f.readlines() -# print(line) f.close() for v in value: function = v + 'Request' @@ -109,6 +106,7 @@ def mkdir(cfg_camel): file.write(l) file.close() + if __name__ == "__main__": conf, cfg_camel = load_conf() print(cfg_camel) From b9060a64d5df8663dfedbe82fda663ec449cb16f Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Mon, 20 Sep 2021 16:36:47 +0800 Subject: [PATCH 3/6] fix(gen-struct): add struct --- protobuf/listx.proto | 21 -------- shell/gen_protobuf.py | 113 ++++++++++++++---------------------------- 2 files changed, 37 insertions(+), 97 deletions(-) delete mode 100644 protobuf/listx.proto diff --git a/protobuf/listx.proto b/protobuf/listx.proto deleted file mode 100644 index 85a6b5c..0000000 --- a/protobuf/listx.proto +++ /dev/null @@ -1,21 +0,0 @@ -syntax = "proto3"; -import "base.proto"; -option go_package = "pkg/proto"; -message LSetRequest { - BaseKey key = 1; -} -message LGetRequest { - BaseKey key = 1; -} -message LAddRequest { - BaseKey key = 1; -} -message LQueueRequest { - BaseKey key = 1; -} -message LSetbitRequest { - BaseKey key = 1; -} -message LGetbitRequest { - BaseKey key = 1; -} diff --git a/shell/gen_protobuf.py b/shell/gen_protobuf.py index 9e7a02a..f60b99d 100644 --- a/shell/gen_protobuf.py +++ b/shell/gen_protobuf.py @@ -24,92 +24,53 @@ def load_conf(): key = to_camel(key).lower() cfg_camel[key] = [to_camel(v) for v in val] +# print(cfg_camel) + return cfg, cfg_camel # 生成文件,并写入 def mkdir(cfg_camel): path = "protobuf" - storagePath = f"{protobufPath}/storage.proto" - for key,value in cfg_camel.items(): - '''生成、更新数据结构proto文件''' - proto_path = path + '/' + key + '.proto' - if not os.path.exists(proto_path): # 如果这个文件不存在 - '''生成对应的数据结构proto文件''' - 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\tBaseKey key = 1;\n}') - file.close() - '''给strong.proto添加import该新的数据结构''' - import_structure = 'import "'+ key +'.proto";\n' - mess = 'message CommendResponse {\n' - with open(storagePath) as f: - line = f.readlines() -# print(line) - f.close - for i in range(len(line)): - if mess in line[i]: - break - line.insert(i,import_structure) - file = open(storagePath,'w') - for l in line: - file.write(l) - file.close() - i=0 - for i in range(len(line)-1,0,-1): - if '}' in line[i]: - break - for v in value: - line.insert(i,'\trpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n') - file = open(storagePath,'w') - for l in line: - file.write(l) - file.close() - - - else: # 如果这个文件存在 - with open(proto_path) as f: - line = f.readlines() - f.close() - for v in value: - function = v + 'Request' - flag = 0 - for l in line: - if function in l: - flag=1 - break - if flag == 0: - file = open(proto_path,'a') - file.write('\nmessage ' + v +'Request '+'{\n\tBaseKey key = 1;\n}') - file.close() - flag = 0 - '''更新storage.proto文件''' - with open(storagePath) as f: - line = f.readlines() - f.close - for v in value: - flg=0 - for l in range(len(line)): - if '\trpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n' in line[l]: - flg = 1 - break - if flg==0: - i=0 - for i in range(len(line)-1,0,-1): - if '}' in line[i]: + path = path.strip() + # 判断路径是否存在 + isExists = os.path.exists(path) + # 判断结果 + if not isExists: + os.makedirs(path) + print(path + ' 创建成功') + else: # 如果目录存在则不创建,并提示目录已存在 + # print(path + ' 目录已存在') + for key,value in cfg_camel.items(): +# print(key) + proto_path = path + '/' + key + '.proto' + if not os.path.exists(proto_path): # 如果这个文件不存在 + 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}\n') + file.close() + else: # 如果这个文件存在 + with open(proto_path) as f: + line = f.readlines() +# print(line) + f.close() + for v in value: + function = v + 'Request' + flag = 0 + for l in line: + if function in l: + flag=1 break - line.insert(i,'\trpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n') - i=0 - file = open(storagePath,'w') - for l in line: - file.write(l) - file.close() + if flag == 0: + file = open(proto_path,'a') + file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}\n') + file.close() + flag = 0 if __name__ == "__main__": conf, cfg_camel = load_conf() - print(cfg_camel) +# mkdir(cfg_camel) - From f410738b2735ed9420e96d194ff3d3b07e10ffe8 Mon Sep 17 00:00:00 2001 From: yu_lang <534013339@qq.com> Date: Tue, 21 Sep 2021 10:11:13 +0800 Subject: [PATCH 4/6] feat(proto):add proto auto tool --- protobuf/listx.proto | 22 ++++++++ protobuf/storage.proto | 14 ++++- shell/gen_protobuf.py | 113 ++++++++++++++++++++++++++++------------- 3 files changed, 112 insertions(+), 37 deletions(-) create mode 100644 protobuf/listx.proto diff --git a/protobuf/listx.proto b/protobuf/listx.proto new file mode 100644 index 0000000..21effe8 --- /dev/null +++ b/protobuf/listx.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; +import "base.proto"; +option go_package = "pkg/proto"; + +message LSetRequest { + BaseKey key = 1; +} +message LGetRequest { + BaseKey key = 1; +} +message LAddRequest { + BaseKey key = 1; +} +message LQueueRequest { + BaseKey key = 1; +} +message LSetbitRequest { + BaseKey key = 1; +} +message LGetbitRequest { + BaseKey key = 1; +} diff --git a/protobuf/storage.proto b/protobuf/storage.proto index 7a50c4d..d13c98b 100644 --- a/protobuf/storage.proto +++ b/protobuf/storage.proto @@ -1,3 +1,4 @@ + syntax = "proto3"; option go_package = "pkg/proto"; @@ -20,5 +21,16 @@ service CommServer { rpc LQueue (LQueueRequest) returns (CommendResponse); rpc LSetbit (LSetbitRequest) returns (CommendResponse); rpc LGetbit (LGetbitRequest) returns (CommendResponse); - + rpc Set (SetRequest) returns (CommendResponse); + rpc Get (GetRequest) returns (CommendResponse); + rpc Add (AddRequest) returns (CommendResponse); + rpc Reduce (ReduceRequest) returns (CommendResponse); + rpc Setbit (SetbitRequest) returns (CommendResponse); + rpc Getbit (GetbitRequest) returns (CommendResponse); + rpc LSet (LSetRequest) returns (CommendResponse); + rpc LGet (LGetRequest) returns (CommendResponse); + rpc LAdd (LAddRequest) returns (CommendResponse); + rpc LQueue (LQueueRequest) returns (CommendResponse); + rpc LSetbit (LSetbitRequest) returns (CommendResponse); + rpc LGetbit (LGetbitRequest) returns (CommendResponse); } \ No newline at end of file diff --git a/shell/gen_protobuf.py b/shell/gen_protobuf.py index f60b99d..a8242fb 100644 --- a/shell/gen_protobuf.py +++ b/shell/gen_protobuf.py @@ -32,45 +32,86 @@ def load_conf(): # 生成文件,并写入 def mkdir(cfg_camel): path = "protobuf" - path = path.strip() - # 判断路径是否存在 - isExists = os.path.exists(path) - # 判断结果 - if not isExists: - os.makedirs(path) - print(path + ' 创建成功') - else: # 如果目录存在则不创建,并提示目录已存在 - # print(path + ' 目录已存在') - for key,value in cfg_camel.items(): -# print(key) - proto_path = path + '/' + key + '.proto' - if not os.path.exists(proto_path): # 如果这个文件不存在 - 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}\n') - file.close() - else: # 如果这个文件存在 - with open(proto_path) as f: - line = f.readlines() -# print(line) - f.close() - for v in value: - function = v + 'Request' - flag = 0 - for l in line: - if function in l: - flag=1 - break - if flag == 0: - file = open(proto_path,'a') - file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}\n') - file.close() - flag = 0 + storagePath = f"{protobufPath}/storage.proto" + for key,value in cfg_camel.items(): + '''生成、更新数据结构proto文件''' + proto_path = path + '/' + key + '.proto' + if not os.path.exists(proto_path): # 如果这个文件不存在 + '''生成对应的数据结构proto文件''' + 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.close() + '''给strong.proto添加import该新的数据结构''' + import_structure = 'import "'+ key +'.proto";\n' + mess = 'message CommendResponse {\n' + with open(storagePath) as f: + line = f.readlines() +# print(line) + f.close + for i in range(len(line)): + if mess in line[i]: + break + line.insert(i,import_structure) + file = open(storagePath,'w') + for l in line: + file.write(l) + file.close() + i=0 + for i in range(len(line)-1,0,-1): + if '}' in line[i]: + break + for v in value: + line.insert(i,' rpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n') + file = open(storagePath,'w') + for l in line: + file.write(l) + file.close() + else: # 如果这个文件存在 + with open(proto_path) as f: + line = f.readlines() +# print(line) + f.close() + for v in value: + function = v + 'Request' + flag = 0 + for l in line: + if function in l: + flag=1 + break + if flag == 0: + file = open(proto_path,'a') + file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}') + file.close() + flag = 0 + '''更新storage.proto文件''' + with open(storagePath) as f: + line = f.readlines() + f.close + for v in value: + flg=0 + for l in range(len(line)): + if ' rpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n' in line[l]: + flg = 1 + break + if flg==0: + i=0 + for i in range(len(line)-1,0,-1): + if '}' in line[i]: + break + line.insert(i,' rpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n') + i=0 + file = open(storagePath,'w') + for l in line: + file.write(l) + file.close() + if __name__ == "__main__": conf, cfg_camel = load_conf() -# + print(cfg_camel) mkdir(cfg_camel) + From 53fe26db48d453054c85fec5994f15165239211f Mon Sep 17 00:00:00 2001 From: yu_lang <534013339@qq.com> Date: Tue, 21 Sep 2021 09:44:22 +0800 Subject: [PATCH 5/6] feat(proto):add proto auto_tool --- protobuf/listx.proto | 1 - shell/gen_protobuf.py | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/protobuf/listx.proto b/protobuf/listx.proto index 21effe8..85a6b5c 100644 --- a/protobuf/listx.proto +++ b/protobuf/listx.proto @@ -1,7 +1,6 @@ syntax = "proto3"; import "base.proto"; option go_package = "pkg/proto"; - message LSetRequest { BaseKey key = 1; } diff --git a/shell/gen_protobuf.py b/shell/gen_protobuf.py index a8242fb..23475e4 100644 --- a/shell/gen_protobuf.py +++ b/shell/gen_protobuf.py @@ -24,8 +24,6 @@ def load_conf(): key = to_camel(key).lower() cfg_camel[key] = [to_camel(v) for v in val] -# print(cfg_camel) - return cfg, cfg_camel @@ -73,7 +71,6 @@ def mkdir(cfg_camel): else: # 如果这个文件存在 with open(proto_path) as f: line = f.readlines() -# print(line) f.close() for v in value: function = v + 'Request' @@ -109,6 +106,7 @@ def mkdir(cfg_camel): file.write(l) file.close() + if __name__ == "__main__": conf, cfg_camel = load_conf() print(cfg_camel) From f3e894a0689af247abbfc40da03b134284a91e8e Mon Sep 17 00:00:00 2001 From: yu_lang <534013339@qq.com> Date: Tue, 21 Sep 2021 11:03:31 +0800 Subject: [PATCH 6/6] feat(proto):update proto auto tool --- pkg/structure/generate/tem.yaml | 5 ++- protobuf/listx.proto | 22 ++-------- protobuf/storage.proto | 25 +++-------- shell/gen_protobuf.py | 78 +++++++++++---------------------- 4 files changed, 39 insertions(+), 91 deletions(-) diff --git a/pkg/structure/generate/tem.yaml b/pkg/structure/generate/tem.yaml index 9aae05e..b81c1bf 100644 --- a/pkg/structure/generate/tem.yaml +++ b/pkg/structure/generate/tem.yaml @@ -6,4 +6,7 @@ STRING_X: - add - reduce - setbit - - getbit \ No newline at end of file + - getbit + +LIST_X: + - set \ No newline at end of file diff --git a/protobuf/listx.proto b/protobuf/listx.proto index 85a6b5c..4305a13 100644 --- a/protobuf/listx.proto +++ b/protobuf/listx.proto @@ -1,21 +1,7 @@ syntax = "proto3"; import "base.proto"; option go_package = "pkg/proto"; -message LSetRequest { - BaseKey key = 1; -} -message LGetRequest { - BaseKey key = 1; -} -message LAddRequest { - BaseKey key = 1; -} -message LQueueRequest { - BaseKey key = 1; -} -message LSetbitRequest { - BaseKey key = 1; -} -message LGetbitRequest { - BaseKey key = 1; -} + +message SetRequest { + BaseKey key = 1; +} \ No newline at end of file diff --git a/protobuf/storage.proto b/protobuf/storage.proto index d13c98b..634266f 100644 --- a/protobuf/storage.proto +++ b/protobuf/storage.proto @@ -1,36 +1,23 @@ +// Code generated by gen-struct. DO NOT EDIT. +// make gen-protobuf generated syntax = "proto3"; option go_package = "pkg/proto"; + import "stringx.proto"; import "listx.proto"; + message CommendResponse { - repeated string result = 1; + repeated string result = 1; } service CommServer { - rpc Set (SetRequest) returns (CommendResponse); - rpc Get (GetRequest) returns (CommendResponse); - rpc Add (AddRequest) returns (CommendResponse); - rpc Reduce (ReduceRequest) returns (CommendResponse); - rpc Setbit (SetbitRequest) returns (CommendResponse); - rpc Getbit (GetbitRequest) returns (CommendResponse); - rpc LSet (LSetRequest) returns (CommendResponse); - rpc LGet (LGetRequest) returns (CommendResponse); - rpc LAdd (LAddRequest) returns (CommendResponse); - rpc LQueue (LQueueRequest) returns (CommendResponse); - rpc LSetbit (LSetbitRequest) returns (CommendResponse); - rpc LGetbit (LGetbitRequest) returns (CommendResponse); rpc Set (SetRequest) returns (CommendResponse); rpc Get (GetRequest) returns (CommendResponse); rpc Add (AddRequest) returns (CommendResponse); rpc Reduce (ReduceRequest) returns (CommendResponse); rpc Setbit (SetbitRequest) returns (CommendResponse); rpc Getbit (GetbitRequest) returns (CommendResponse); - rpc LSet (LSetRequest) returns (CommendResponse); - rpc LGet (LGetRequest) returns (CommendResponse); - rpc LAdd (LAddRequest) returns (CommendResponse); - rpc LQueue (LQueueRequest) returns (CommendResponse); - rpc LSetbit (LSetbitRequest) returns (CommendResponse); - rpc LGetbit (LGetbitRequest) returns (CommendResponse); + rpc Set (SetRequest) returns (CommendResponse); } \ No newline at end of file diff --git a/shell/gen_protobuf.py b/shell/gen_protobuf.py index 23475e4..38c6359 100644 --- a/shell/gen_protobuf.py +++ b/shell/gen_protobuf.py @@ -27,12 +27,11 @@ def load_conf(): return cfg, cfg_camel -# 生成文件,并写入 -def mkdir(cfg_camel): +'''生成对应的数据结构proto文件''' +def mk_structure(cfg_camel): path = "protobuf" storagePath = f"{protobufPath}/storage.proto" - for key,value in cfg_camel.items(): - '''生成、更新数据结构proto文件''' + for key, value in cfg_camel.items(): proto_path = path + '/' + key + '.proto' if not os.path.exists(proto_path): # 如果这个文件不存在 '''生成对应的数据结构proto文件''' @@ -41,34 +40,9 @@ def mkdir(cfg_camel): for v in value: file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}') file.close() - '''给strong.proto添加import该新的数据结构''' - import_structure = 'import "'+ key +'.proto";\n' - mess = 'message CommendResponse {\n' - with open(storagePath) as f: - line = f.readlines() -# print(line) - f.close - for i in range(len(line)): - if mess in line[i]: - break - line.insert(i,import_structure) - file = open(storagePath,'w') - for l in line: - file.write(l) - file.close() - i=0 - for i in range(len(line)-1,0,-1): - if '}' in line[i]: - break - for v in value: - line.insert(i,' rpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n') - file = open(storagePath,'w') - for l in line: - file.write(l) - file.close() - else: # 如果这个文件存在 + '''更新数据结构proto文件''' with open(proto_path) as f: line = f.readlines() f.close() @@ -84,32 +58,30 @@ def mkdir(cfg_camel): file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}') file.close() flag = 0 - '''更新storage.proto文件''' - with open(storagePath) as f: - line = f.readlines() - f.close - for v in value: - flg=0 - for l in range(len(line)): - if ' rpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n' in line[l]: - flg = 1 - break - if flg==0: - i=0 - for i in range(len(line)-1,0,-1): - if '}' in line[i]: - break - line.insert(i,' rpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n') - i=0 - file = open(storagePath,'w') - for l in line: - file.write(l) - file.close() + + print(f"{key}.proto", "-> success") + + +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.write('\nmessage CommendResponse {\n repeated string result = 1;\n}\n\nservice CommServer {\n') + for key,value in cfg_camel.items(): + for v in value: + file.write(' rpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n') + file.write('}') + file.close() + print("storage.proto", "-> success") if __name__ == "__main__": conf, cfg_camel = load_conf() - print(cfg_camel) - mkdir(cfg_camel) + mk_structure(cfg_camel) # 生成对应的数据结构proto文件 + mk_storage(cfg_camel) + +