MCPcopy
hub / github.com/ChinaGodMan/UserScripts / search_in_file

Function search_in_file

utils/searcher.py:10–32  ·  view source on GitHub ↗
(file_path, search_string)

Source from the content-addressed store, hash-verified

8
9
10def search_in_file(file_path, search_string):
11 results = SearchResults()
12
13 def perform_search(content, search_str):
14 name_pattern = re.compile(r'// @name:\s*' + re.escape(search_str) + r'\s*(.+)')
15 description_pattern = re.compile(r'// @description:\s*' + re.escape(search_str) + r'\s*(.+)')
16 results.name_matches.extend([match.strip() for match in name_pattern.findall(content)])
17 results.description_matches.extend([match.strip() for match in description_pattern.findall(content)])
18
19 try:
20 with open(file_path, 'r', encoding='utf-8') as file:
21 content = file.read()
22 perform_search(content, search_string)
23
24 if not results.name_matches and not results.description_matches:
25 perform_search(content, "zh-CN")
26
27 except FileNotFoundError:
28 print("文件未找到,请检查文件路径。")
29 except Exception as e:
30 print(f"发生错误: {e}")
31
32 return results

Callers 6

read_file_to_memoryFunction · 0.90
update_version.pyFile · 0.90
generate_html_contentFunction · 0.90
generate_descriptionFunction · 0.90
generate_html_tableFunction · 0.90

Calls 2

SearchResultsClass · 0.85
perform_searchFunction · 0.85

Tested by

no test coverage detected