|
| 1 | +import json |
| 2 | +import sys |
| 3 | +import re |
| 4 | + |
| 5 | + |
| 6 | +# global variables |
| 7 | +DICT_HOST_KEY = 'host' |
| 8 | +DICT_PORT_KEY = 'port' |
| 9 | +DICT_STUB_CLASS_KEY = 'stub_class' |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +class Service: |
| 14 | + def __init__( |
| 15 | + self, |
| 16 | + host: str, |
| 17 | + port: int, |
| 18 | + name: str, |
| 19 | + proto_file_path: str, |
| 20 | + protos_output_folder: str, |
| 21 | + ): |
| 22 | + self.host = host |
| 23 | + self.port = port |
| 24 | + self.name = name |
| 25 | + self.__proto_file_path = proto_file_path |
| 26 | + self.__protos_output_folder = protos_output_folder |
| 27 | + |
| 28 | + self.module_path: str = None |
| 29 | + self.__generate_module_import_code_str() |
| 30 | + |
| 31 | + def __generate_module_import_code_str(self) -> None: |
| 32 | + module_path = self.__proto_file_path.replace('/', '.') |
| 33 | + module_path = module_path.replace('.proto', '_pb2_grpc') |
| 34 | + module_path = module_path.replace('protos', self.__protos_output_folder) |
| 35 | + self.module_path = 'from ' + module_path + ' import ' + self.name + 'Stub' |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +def convert_to_snake_case(string): |
| 40 | + pattern = r'([A-Z])' |
| 41 | + split_string = re.sub(pattern, r'_\1', string).strip('_') |
| 42 | + return split_string.upper() |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +def generate_stubs_file_code( |
| 47 | + services: list, |
| 48 | + stubs_output_file_path: str, |
| 49 | + template_output_file_path: str, |
| 50 | +) -> None: |
| 51 | + |
| 52 | + # read template file |
| 53 | + stubs_output_template_file = open(template_output_file_path, 'r') |
| 54 | + stubs_output_file_text = stubs_output_template_file.read() |
| 55 | + stubs_output_template_file.close() |
| 56 | + |
| 57 | + # replace dict keys in get_stub function |
| 58 | + stubs_output_file_text = stubs_output_file_text.replace( |
| 59 | + '$HOST_KEY', |
| 60 | + DICT_HOST_KEY, |
| 61 | + ) |
| 62 | + stubs_output_file_text = stubs_output_file_text.replace( |
| 63 | + '$PORT_KEY', |
| 64 | + DICT_PORT_KEY, |
| 65 | + ) |
| 66 | + stubs_output_file_text = stubs_output_file_text.replace( |
| 67 | + '$STUB_CLASS_KEY', |
| 68 | + DICT_STUB_CLASS_KEY, |
| 69 | + ) |
| 70 | + |
| 71 | + # import libraries code generation |
| 72 | + default_imports = [ |
| 73 | + 'grpc', |
| 74 | + ] |
| 75 | + imports_code = '' |
| 76 | + for default_import in default_imports: |
| 77 | + imports_code += f'import {default_import}\n' |
| 78 | + imports_code += '\n' |
| 79 | + |
| 80 | + for service in services: |
| 81 | + imports_code += service.module_path + '\n' |
| 82 | + |
| 83 | + stubs_output_file_text = stubs_output_file_text.replace( |
| 84 | + '{{ import libraries code }}', |
| 85 | + imports_code, |
| 86 | + ) |
| 87 | + |
| 88 | + # services code generation |
| 89 | + services_code = 'class Services:\n' |
| 90 | + for service in services: |
| 91 | + services_code += f'\t{convert_to_snake_case(service.name)} = {{\n' |
| 92 | + services_code += f'\t\t"{DICT_HOST_KEY}": "{service.host}",\n' |
| 93 | + services_code += f'\t\t"{DICT_PORT_KEY}": {service.port},\n' |
| 94 | + services_code += f'\t\t"{DICT_STUB_CLASS_KEY}": {service.name}Stub,\n' |
| 95 | + services_code += '\t}\n' |
| 96 | + stubs_output_file_text = stubs_output_file_text.replace( |
| 97 | + '{{ Services class code }}', |
| 98 | + services_code, |
| 99 | + ) |
| 100 | + |
| 101 | + # write stubs output file |
| 102 | + stubs_output_file = open(stubs_output_file_path, 'w') |
| 103 | + stubs_output_file.write(stubs_output_file_text) |
| 104 | + stubs_output_file.close() |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + # read arguments |
| 111 | + stubs_configs_file_path = sys.argv[1] |
| 112 | + stubs_output_folder = sys.argv[2] |
| 113 | + protos_output_folder = stubs_output_folder.split('/')[1] |
| 114 | + stubs_output_file_path = sys.argv[3] |
| 115 | + template_output_file_path = sys.argv[4] |
| 116 | + |
| 117 | + |
| 118 | + # read stubs configs file |
| 119 | + stubs_json_dict = json.loads(open(stubs_configs_file_path, 'r').read()) |
| 120 | + services_configs = stubs_json_dict['services'] |
| 121 | + |
| 122 | + |
| 123 | + # create services objects |
| 124 | + services = [] |
| 125 | + for service in services_configs: |
| 126 | + services.append( |
| 127 | + Service( |
| 128 | + host=service['host'], |
| 129 | + port=service['port'], |
| 130 | + name=service['name'], |
| 131 | + proto_file_path=service['proto_file_path'], |
| 132 | + protos_output_folder=protos_output_folder, |
| 133 | + ) |
| 134 | + ) |
| 135 | + |
| 136 | + generate_stubs_file_code( |
| 137 | + services=services, |
| 138 | + stubs_output_file_path=stubs_output_file_path, |
| 139 | + template_output_file_path=template_output_file_path, |
| 140 | + ) |
0 commit comments