import re import json # 提取代码中函数名称 import re import os import xml.etree.ElementTree as ET def debug_print(*args, sep=' ', end='\n', file=None, flush=False): data = read_json_file('config.json') try: debug = data['debug'] except Exception: debug = 'true' if debug == 'true': print(*args, sep=sep, end=end, file=file, flush=flush) # 提取文件后缀 def extract_file_extension(file_path): # 正则表达式匹配文件扩展名,即最后一个`.`之后的内容 match = re.search(r'\.([^.]+)$', file_path) if match: return match.group(1) else: return None # file_path = r"E:\sesCode_行情\upub\dev_codes\uftbusiness\sysargpub\LS_SYSARGPUB_UMTCONFIG_GET.uftservice" # print("提取的文件后缀:", extract_file_extension(file_path)) # 提取文件名称 def extract_filename(path): """ 从路径中提取不带后缀的文件名 :param path: Windows文件路径 :return: 不带后缀的文件名 """ # 获取路径中的文件名(带后缀) filename_with_ext = os.path.basename(path) # 分割文件名和后缀 filename, _ = os.path.splitext(filename_with_ext) return filename # 提取文件中的函数名称和功能号 def extract_name_functionNo(xml_str): # 正则表达式模式 pattern = r'chineseName="([^"]+)".*objectId="([^"]+)"' # 使用findall方法提取匹配项 matches = re.findall(pattern, xml_str) if matches: chinese_name, object_id = matches[0] return matches[0] # print(f"chineseName: {chinese_name}") # print(f"objectId: {object_id}") else: # print("没有找到匹配的值") return None #xml_str = '' # xml_str = '' # chinese_name, object_id = extract_name_functionNo(xml_str) # print(f"chineseName: {chinese_name}") # print(f"objectId: {object_id}") # 读取文件第二行内容 def read_second_line(file_path): """ 读取指定文件的第二行内容并返回。 参数: file_path (str): 文件的路径。 返回: str: 第二行的内容,如果没有第二行则返回None。 """ try: with open(file_path, 'r', encoding='utf-8') as file: # 跳过第一行 next(file) # 读取第二行 second_line = next(file, None) return second_line.strip() if second_line else None except FileNotFoundError: print(f"文件 {file_path} 未找到。") return None except Exception as e: print(f"读取文件时发生错误: {e}") return None # 读取文件第五行内容 def read_five_line(file_path): """ 读取指定文件的第二行内容并返回。 参数: file_path (str): 文件的路径。 返回: str: 第二行的内容,如果没有第二行则返回None。 """ try: with open(file_path, 'r', encoding='utf-8') as file: # 跳过第一行 next(file) next(file) next(file) next(file) # 读取第五行 second_line = next(file, None) return second_line.strip() if second_line else None except FileNotFoundError: print(f"文件 {file_path} 未找到。") return None except Exception as e: print(f"读取文件时发生错误: {e}") return None # 提取文件中的函数名称和功能号 def uf20_extract_name_functionNo(xml_str): # 正则表达式模式 # pattern = r'objectId="([^"]+)".*englishName="([^"]+)"' # 20250714 fix 修复 englishName为空时提取失败问题 pattern = r'objectId="([^"]*)"[^>]*englishName="([^"]*)"' # 使用findall方法提取匹配项 matches = re.findall(pattern, xml_str) if matches: chinese_name, object_id = matches[0] return matches[0] # print(f"chineseName: {chinese_name}") # print(f"objectId: {object_id}") else: # print("没有找到匹配的值") return None # 20250807 add 增加判断函数名是否合法 def extract_and_check_prefix(input_str): # 找到第一个左方括号的位置 bracket_index = input_str.find('[') if bracket_index != -1: # 提取第一个 [ 之前的内容 prefix = input_str[:bracket_index] # 检查是否为 # 20250823 fix 修复内存等标记无法识别问题(内存与UF20伪代码不一致导致) 检查是否为 或者 is_m_prefix = prefix == "<M>" or prefix == '' or prefix == "" or prefix == "" or prefix == "" or prefix == "" return prefix, is_m_prefix else: # 如果没有 [ ,整个字符串作为前缀 return input_str, input_str == " <M>" # text = "[LS_外部接口(证券交易)_资金余额更新][][serial_no=@serial_no, enable_balance = @enable_balance, entrust_buy_balance_t = @entrust_buy_balance_t, business_frozen_balance_t = @business_frozen_balance_t][node_no = @partition_no, timeout = @time_out]" def match_func_name(text): # 正则表达式匹配第一个中括号内的内容 pattern = r'\[(.*?)\]' text = text.strip() if text.startswith("//") or text.startswith("