-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
58 lines (50 loc) · 1.85 KB
/
config.py
File metadata and controls
58 lines (50 loc) · 1.85 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
"""設定の読み書き"""
import json
import os
import sys
DEFAULT_CONFIG = {
"window": {
"x": None,
"y": None,
"width": 280,
"max_width": 500,
"always_on_top": True,
"theme": "system",
"opacity": 1.0,
},
"lang": "ja",
"icon_size": 48,
"visible_count": 30, # 折りたたみ前に表示する件数
"tags": {},
"tag_order": [],
"stars": {}, # { "item_name": 1~3 } 重要度(0=なし)
"usage": {}, # { "item_name": {"count": N, "last": "ISO timestamp"} }
}
# ユーザーデータ: %APPDATA%/DeskShelf/ (ユーザーごとに独立)
CONFIG_DIR = os.path.join(os.environ.get("APPDATA", os.path.expanduser("~")), "DeskShelf")
CONFIG_PATH = os.path.join(CONFIG_DIR, "config.json")
# アセット(アイコン等): PyInstaller展開先 or スクリプトディレクトリ
if getattr(sys, "frozen", False):
ASSETS_DIR = os.path.join(sys._MEIPASS, "data")
else:
ASSETS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
def load_config() -> dict:
if os.path.isfile(CONFIG_PATH):
try:
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
saved = json.load(f)
config = json.loads(json.dumps(DEFAULT_CONFIG))
for key in DEFAULT_CONFIG:
if key in saved:
if isinstance(DEFAULT_CONFIG[key], dict):
config[key] = {**DEFAULT_CONFIG[key], **saved[key]}
else:
config[key] = saved[key]
return config
except Exception:
pass
return json.loads(json.dumps(DEFAULT_CONFIG))
def save_config(config: dict) -> None:
os.makedirs(CONFIG_DIR, exist_ok=True)
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
json.dump(config, f, indent=2, ensure_ascii=False)