Spaces:
Running
Running
mrhblfx
commited on
Commit
·
9b5f088
1
Parent(s):
3a561a7
Changed matching rules
Browse files- crazy_functions/解析项目源代码.py +23 -15
crazy_functions/解析项目源代码.py
CHANGED
|
@@ -268,19 +268,22 @@ def 解析一个CSharp项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, s
|
|
| 268 |
|
| 269 |
@CatchException
|
| 270 |
def 解析任意code项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
#
|
| 274 |
-
pattern_include = [_.lstrip("
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
| 284 |
if os.path.exists(txt):
|
| 285 |
project_folder = txt
|
| 286 |
else:
|
|
@@ -288,8 +291,13 @@ def 解析任意code项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, sys
|
|
| 288 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 289 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 290 |
return
|
| 291 |
-
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
if len(file_manifest) == 0:
|
| 294 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何文件: {txt}")
|
| 295 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
|
|
|
| 268 |
|
| 269 |
@CatchException
|
| 270 |
def 解析任意code项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 271 |
+
txt_pattern = plugin_kwargs.get("txt_pattern")
|
| 272 |
+
txt_pattern = txt_pattern.replace(",", ",")
|
| 273 |
+
# 将要匹配的模式(例如: *.c, *.cpp, *.py, config.toml)
|
| 274 |
+
pattern_include = [_.lstrip(" ,").rstrip(" ,") for _ in txt_pattern.split(",") if _ != "" and not _.strip().startswith("^")]
|
| 275 |
+
if not pattern_include: pattern_include = ["*"] # 不输入即全部匹配
|
| 276 |
+
# 将要忽略匹配的文件后缀(例如: ^*.c, ^*.cpp, ^*.py)
|
| 277 |
+
pattern_except_suffix = [_.lstrip(" ^*.,").rstrip(" ,") for _ in txt_pattern.split(" ") if _ != "" and _.strip().startswith("^*.")]
|
| 278 |
+
pattern_except_suffix += ['zip', 'rar', '7z', 'tar', 'gz'] # 避免解析压缩文件
|
| 279 |
+
# 将要忽略匹配的文件名(例如: ^README.md)
|
| 280 |
+
pattern_except_name = [_.lstrip(" ^*,").rstrip(" ,").replace(".", "\.") for _ in txt_pattern.split(" ") if _ != "" and _.strip().startswith("^") and not _.strip().startswith("^*.")]
|
| 281 |
+
# 生成正则表达式
|
| 282 |
+
pattern_except = '/[^/]+\.(' + "|".join(pattern_except_suffix) + ')$'
|
| 283 |
+
pattern_except += '|/(' + "|".join(pattern_except_name) + ')$' if pattern_except_name != [] else ''
|
| 284 |
+
|
| 285 |
+
history.clear()
|
| 286 |
+
import glob, os, re
|
| 287 |
if os.path.exists(txt):
|
| 288 |
project_folder = txt
|
| 289 |
else:
|
|
|
|
| 291 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 292 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 293 |
return
|
| 294 |
+
# 若上传压缩文件, 先寻找到解压的文件夹路径, 从而避免解析压缩文件
|
| 295 |
+
maybe_dir = [f for f in glob.glob(f'{project_folder}/**') if os.path.isdir(f)]
|
| 296 |
+
extract_folder_path = maybe_dir[0].replace('\\', '/') if len(maybe_dir) != 0 else ""
|
| 297 |
+
# 按输入的匹配模式寻找上传的非压缩文件和已解压的文件
|
| 298 |
+
file_manifest = [f for f in glob.glob(f'{project_folder}/**') if os.path.isfile(f) and not re.search(pattern_except, f)] + \
|
| 299 |
+
[f for _ in pattern_include for f in glob.glob(f'{extract_folder_path}/**/{_}', recursive=True) if \
|
| 300 |
+
"" != extract_folder_path and os.path.isfile(f) and not re.search(pattern_except, f)]
|
| 301 |
if len(file_manifest) == 0:
|
| 302 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何文件: {txt}")
|
| 303 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|