def replace_rpc_fun_param(str, pro_mod, protocol_obj): protocol_key = protocol_obj.protocol_key mod_name = pro_mod.mod_name # 替换record参数 # 注意()前面的\,会影响正则匹配 begin_str = "\nhandle\(#%s{" % (protocol_key) begin_str_1 = "\nhandle(#%s{" % (protocol_key) # 正则匹配函数 a = r"(.*)" + begin_str + "(.*?)}, Player(.*)" matchObj = re.match(a, str, re.M | re.S) if matchObj: str = matchObj.group(1) + begin_str_1 + HelpMod.get_rpc_record_param( protocol_obj) + "}, Player" + matchObj.group(3) else: # 匹配不到,说明没有这个函数 return str, False # 替换lib参数 "case memory_lib:video(Player, Type) of" fun_name = HelpMod.get_fun_name(pro_mod, protocol_key) begin_str = "%s_lib:%s\(" % (mod_name, fun_name) begin_str_1 = "case %s_lib:%s(" % (mod_name, fun_name) a = r"(.*)case " + begin_str + "(.*?) of(.*)" matchObj = re.match(a, str, re.M | re.S) if matchObj: str = matchObj.group(1) + begin_str_1 + HelpMod.get_fun_param( protocol_obj) + ") of" + matchObj.group(3) return str, True
def get_rpc_file_protocol_fun_str(pro_mod, protocol_key): # 协议函数结构 protocol_fun = """ %%%% {0} handle(#{1}{{{2}}}, Player) -> case {3}_lib:{4}({5}) of {{ok, Reply}} -> {{reply, Reply}}; {{false, Code}} -> {{reply, #{6}{{code = Code}}}} end;\n""" protocol_obj = pro_mod.protocol_map[protocol_key] # 生成协议函数 tmp_protocol_fun = protocol_fun.format( protocol_obj.desc, protocol_key, HelpMod.get_rpc_record_param(protocol_obj) # rpc参数 , pro_mod.mod_name, HelpMod.get_fun_name(pro_mod, protocol_key) # 函数名 , HelpMod.get_fun_param(protocol_obj) # 函数参数 , protocol_key.replace("request", "reply")) return tmp_protocol_fun
def add_lib_fun_param(str, pro_mod, protocol_obj): protocol_key = protocol_obj.protocol_key last_protocol_key = HelpMod.get_last_protocol_key(pro_mod, protocol_key) # 添加api_str # 上一个函数所在的位置 last_fun_name = HelpMod.get_fun_name(pro_mod, last_protocol_key) api_str = get_api_str_1(pro_mod, last_protocol_key)[:-2] last_fun_name_pos = str.find(api_str) # 上个函数下方函数的位置 end_fun_pos = last_fun_name_pos + len(api_str) api_str = get_api_str_1(pro_mod, protocol_key)[:-2] str = str[:end_fun_pos] + ",\n" + api_str + str[end_fun_pos:] # 添加lib_fun # 上一个函数所在的位置 last_fun_name = HelpMod.get_fun_name(pro_mod, last_protocol_key) find_str = "\n%s(" % (last_fun_name) last_fun_name_pos = str.find(find_str) # 上个函数下方函数的位置 end_fun_pos = str.find("\n%%", last_fun_name_pos) lib_fun_str = get_lib_file_lib_fun_str_1(pro_mod, protocol_key) str = str[:end_fun_pos] + lib_fun_str + "\n" + str[end_fun_pos:] # 添加check_fun # 上一个函数所在的位置 find_str = "\ncheck_%s(" % (last_fun_name) last_fun_name_pos = str.find(find_str) # 上个函数下方函数的位置 end_fun_pos = str.find("\n%%", last_fun_name_pos) check_fun_str = get_lib_file_check_str_1(pro_mod, protocol_key) str = str[:end_fun_pos] + check_fun_str + "\n" + str[end_fun_pos:] # 添加do_fun # 上一个函数所在的位置 find_str = "\ndo_%s(" % (last_fun_name) last_fun_name_pos = str.find(find_str) # 上个函数下方函数的位置 end_fun_pos = str.find("\n%%", last_fun_name_pos) do_fun_str = get_lib_file_do_str_1(pro_mod, protocol_key) str = str[:end_fun_pos] + do_fun_str + "\n" + str[end_fun_pos:] return str
def get_lib_file_check_str_1(pro_mod, protocol_key): check_fun = """ %%%% @doc 检查%s check_%s(%s) -> ok. """ protocol_obj = pro_mod.protocol_map[protocol_key] tmp_check_fun = check_fun % (protocol_obj.desc[2:], HelpMod.get_fun_name(pro_mod, protocol_key), HelpMod.get_fun_param(protocol_obj)) return tmp_check_fun
def get_lib_file_do_str_1(pro_mod, protocol_key): do_fun = """ %%%% @doc %s do_%s(%s) -> {ok, #%s{}}. """ protocol_obj = pro_mod.protocol_map[protocol_key] tmp_do_fun = do_fun % (protocol_obj.desc[2:], HelpMod.get_fun_name(pro_mod, protocol_key), HelpMod.get_fun_param(protocol_obj), protocol_key.replace("request", "reply")) return tmp_do_fun
def get_lib_file_lib_fun_str_1(pro_mod, protocol_key): lib_fun = """ %% @doc {0} {1}({2}) -> try check_{1}({2}) of ok -> do_{1}({2}) catch throw : Code -> {{false, Code}} end. """ protocol_obj = pro_mod.protocol_map[protocol_key] # 函数参数 tmp_lib_fun = lib_fun.format(protocol_obj.desc, HelpMod.get_fun_name(pro_mod, protocol_key), HelpMod.get_fun_param(protocol_obj)) return tmp_lib_fun
def replace_lib_fun_param(str, pro_mod, protocol_obj): protocol_key = protocol_obj.protocol_key # api_str是否存在 api_str = get_api_str_1(pro_mod, protocol_key)[:-4] if str.find(api_str) == -1: return str, False # 替换rpc接口函数参数 fun_name = HelpMod.get_fun_name(pro_mod, protocol_key) begin_str = "\n%s" % (fun_name) # 正则匹配函数 a = r"(.*)" + begin_str + "\((.*?)\) ->(.*)" matchObj = re.match(a, str, re.M | re.S) if matchObj: str = "{0}{1}({2}) ->{3}".format(matchObj.group(1), begin_str, HelpMod.get_fun_param(protocol_obj), matchObj.group(3)) else: # 匹配不到 print("替换rpc接口函数参数 err : %s" % (protocol_key)) return str, True
def get_api_str_1(pro_mod, protocol_key): protocol_obj = pro_mod.protocol_map[protocol_key] fun_name = HelpMod.get_fun_name(pro_mod, protocol_key) param_num = len(protocol_obj.param) return "\t%s/%s,\n" % (fun_name, param_num + 1)