41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
from heurams.context import config_var
|
|
import pathlib
|
|
|
|
def probe_by_filename(filename):
|
|
"""探测指定文件 (无扩展名) 的所有信息"""
|
|
paths: dict = config_var.get().get("paths")
|
|
formats = ["toml", "json"]
|
|
result = {}
|
|
for item, attr in paths.items():
|
|
for i in formats:
|
|
attr: pathlib.Path = pathlib.Path(attr) / filename + '.' + i
|
|
if attr.exists():
|
|
result[item.replace("_dir", "")] = str(attr)
|
|
return result
|
|
|
|
def probe_all(is_stem = 1):
|
|
"""依据目录探测所有信息
|
|
|
|
Args:
|
|
is_stem (boolean): 是否**删除**文件扩展名
|
|
|
|
Returns:
|
|
dict: 有三项, 每一项的键名都是文件组类型, 值都是文件组列表, 只包含文件名
|
|
"""
|
|
paths: dict = config_var.get().get("paths")
|
|
result = {}
|
|
for item, attr in paths.items():
|
|
attr: pathlib.Path = pathlib.Path(attr)
|
|
result[item.replace("_dir", "")] = list()
|
|
for i in attr.iterdir():
|
|
if not i.is_dir():
|
|
if is_stem:
|
|
result[item.replace("_dir", "")].append(str(i.stem))
|
|
else:
|
|
result[item.replace("_dir", "")].append(str(i.name))
|
|
return result
|
|
|
|
if __name__ == "__main__":
|
|
import os
|
|
print(os.getcwd())
|
|
print(probe_all()) |