-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
65 lines (47 loc) · 1.69 KB
/
utils.py
File metadata and controls
65 lines (47 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import datetime
import os
import shutil
import commentjson # type: ignore
DATA_DIR = "data"
PROJECT_DIR_PATH = os.path.dirname(os.path.abspath(__file__))
def _build_ctg_file_path() -> str:
return os.path.join(PROJECT_DIR_PATH, "config", "ctg.jsonc")
ctg_cache: dict[str, dict] = {}
def load_ctg() -> dict:
global ctg_cache
if len(ctg_cache) == 0:
with open(_build_ctg_file_path(), "r", encoding="utf-8") as f:
ctg_cache = commentjson.load(f)
return ctg_cache
def _build_data_file_path_from_date(date: datetime.datetime) -> str:
return os.path.join(
PROJECT_DIR_PATH,
DATA_DIR,
f"transaction_{date.year}-{date.month:02d}-{date.day:02d}.csv",
)
def get_data_file_path() -> str:
today = datetime.datetime.now()
today_file_path = _build_data_file_path_from_date(today)
if os.path.exists(today_file_path):
return today_file_path
# 假如没有, 则向之前的日期寻找, 直到进入2024年
date = today
while date.year > 2024:
date = date - datetime.timedelta(days=1)
file_path = _build_data_file_path_from_date(date)
if os.path.exists(file_path):
shutil.copy(file_path, today_file_path)
return today_file_path
raise FileNotFoundError(f"数据文件不存在")
def check_categorys(type: str, categorys: list[str]) -> bool:
node = load_ctg()[type]
for c in categorys:
if c not in node:
return False
node = node[c]
# return len(node) == 0 # 路径刚好到叶子
return True # 路径是一个前缀
if __name__ == "__main__":
print(get_data_file_path())
print(_build_ctg_file_path())
print(load_ctg())