|
|
@ -36,6 +36,46 @@ FileSuf = { # 合法文件后缀 |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
# 同步从路径中搜索后缀符合要求的文件,返回路径列表。 |
|
|
|
def findFiles( |
|
|
|
paths: List, # 初始路径列表 |
|
|
|
sufType: str, # 后缀类型,FileSuf的key |
|
|
|
isRecurrence: bool, # 若为True,则递归搜索 |
|
|
|
): |
|
|
|
if isinstance(paths, QJSValue): |
|
|
|
paths = paths.toVariant() |
|
|
|
if not isinstance(paths, list): |
|
|
|
logger.error(f"不合法的路径列表:{paths}, {type(paths)}") |
|
|
|
return [] |
|
|
|
sufs = FileSuf.get(sufType, "") |
|
|
|
if not sufs: |
|
|
|
logger.error(f"不合法的后缀类型:{sufs}") |
|
|
|
return [] |
|
|
|
|
|
|
|
def _sufMatching(path): |
|
|
|
return os.path.splitext(path)[-1].lower() in sufs |
|
|
|
|
|
|
|
filePaths = [] |
|
|
|
for p in paths: |
|
|
|
if os.path.isfile(p) and _sufMatching(p): # 是文件,直接判断 |
|
|
|
filePaths.append(os.path.abspath(p)) |
|
|
|
elif os.path.isdir(p): # 是目录 |
|
|
|
if isRecurrence: # 需要递归 |
|
|
|
for root, dirs, files in os.walk(p): |
|
|
|
for file in files: |
|
|
|
if _sufMatching(file): # 收集子文件 |
|
|
|
filePaths.append( |
|
|
|
os.path.abspath(os.path.join(root, file)) |
|
|
|
) # 将路径转换为绝对路径 |
|
|
|
else: # 不递归读取子文件夹 |
|
|
|
for file in os.listdir(p): |
|
|
|
if os.path.isfile(os.path.join(p, file)) and _sufMatching(file): |
|
|
|
filePaths.append(os.path.abspath(os.path.join(p, file))) |
|
|
|
for i, p in enumerate(filePaths): # 规范化正斜杠 |
|
|
|
filePaths[i] = p.replace("\\", "/") |
|
|
|
return filePaths |
|
|
|
|
|
|
|
|
|
|
|
# 异步从路径中搜索后缀符合要求的文件 |
|
|
|
def asynFindFiles( |
|
|
|
paths: List, # 初始路径列表 |
|
|
@ -45,19 +85,21 @@ def asynFindFiles( |
|
|
|
updateKey: str = "", # 加载中刷新进度的key,不填则无。向事件传入 (已完成的路径数量, 最近一条路径) |
|
|
|
updateTime: float = 1.0, # 刷新进度的间距 |
|
|
|
): |
|
|
|
print("开始!!!") |
|
|
|
if isinstance(paths, QJSValue): |
|
|
|
paths = paths.toVariant() |
|
|
|
if not isinstance(paths, list): |
|
|
|
logger.error(f"不合法的路径列表:{paths}, {type(paths)}") |
|
|
|
PubSubService.publish(completeKey, []) |
|
|
|
return |
|
|
|
sufs = FileSuf.get(sufType, "") |
|
|
|
if not sufs: |
|
|
|
print("不合法的后缀!!") |
|
|
|
logger.error(f"不合法的后缀类型:{sufs}") |
|
|
|
PubSubService.publish(completeKey, []) |
|
|
|
return |
|
|
|
|
|
|
|
def _sufMatching(path): |
|
|
|
return os.path.splitext(path)[-1].lower() in sufs |
|
|
|
|
|
|
|
if isinstance(paths, QJSValue): |
|
|
|
paths = paths.toVariant() |
|
|
|
if not isinstance(paths, list): |
|
|
|
logger.error(f"_findFiles 传入:{paths}, {type(paths)}") |
|
|
|
return [] |
|
|
|
if not updateKey: # 如果没有刷新事件,则刷新间隔为无穷大 |
|
|
|
updateTime = float("inf") |
|
|
|
updateTime |
|
|
@ -89,6 +131,5 @@ def asynFindFiles( |
|
|
|
fp = fp.replace("\\", "/") # 规范化正斜杠 |
|
|
|
filePaths.append(fp) |
|
|
|
updateEvent(fp) |
|
|
|
time.sleep(0.1) |
|
|
|
|
|
|
|
PubSubService.publish(completeKey, filePaths) |