-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
130 lines (114 loc) · 3.96 KB
/
Copy pathconfig.py
File metadata and controls
130 lines (114 loc) · 3.96 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""Configuration module for RED-Python application."""
import os
import sys
import json
from pathlib import Path
_win = os.environ.get("SystemRoot", "C:\\Windows")
DEFAULT_PROTECTED_DIRS = [
os.path.join(_win, "System32"),
os.path.join(_win, "SysWOW64"),
os.path.join(_win, "WinSxS"),
"$RECYCLE.BIN",
"System Volume Information",
]
# Unified filter rules — each rule is a dict:
# enabled : bool
# type : 'ignore_file' | 'ignore_dir' | 'never_empty'
# method : 'wildcard' | 'contains' | 'startswith' | 'endswith' |
# 'exact' | 'exact_path' | 'regex_name' | 'regex_path'
# pattern : str
DEFAULT_FILTER_RULES = [
{
"enabled": True,
"type": "ignore_file",
"method": "exact",
"pattern": "desktop.ini",
},
{"enabled": True, "type": "ignore_file", "method": "exact", "pattern": "Thumbs.db"},
{"enabled": True, "type": "ignore_file", "method": "exact", "pattern": "thumbs.db"},
{"enabled": True, "type": "ignore_file", "method": "exact", "pattern": ".DS_Store"},
{"enabled": True, "type": "ignore_file", "method": "wildcard", "pattern": "._*"},
{"enabled": True, "type": "ignore_file", "method": "exact", "pattern": ".gitkeep"},
{
"enabled": True,
"type": "ignore_dir",
"method": "exact",
"pattern": "__pycache__",
},
{"enabled": True, "type": "ignore_dir", "method": "exact", "pattern": ".venv"},
{
"enabled": True,
"type": "ignore_dir",
"method": "exact",
"pattern": ".ipynb_checkpoints",
},
{
"enabled": True,
"type": "ignore_dir",
"method": "exact",
"pattern": ".jekyll-cache",
},
]
DEFAULT_SETTINGS = {
"filter_rules": DEFAULT_FILTER_RULES,
"protected_dirs": DEFAULT_PROTECTED_DIRS,
"max_depth": 0,
"min_age_hours": 0,
"ignore_empty_files": True,
"scan_hidden": False,
"follow_symlinks": False,
"delete_mode": "recycle",
"pause_ms": 0,
"max_warnings": 10,
"recent_paths": [],
"play_sound": True,
}
CONFIG_FILENAME = "settings.json"
DEFAULT_CONFIG_PATH = Path.home() / ".red_python" / CONFIG_FILENAME
def get_config_path():
# Portable mode: if settings.json exists in the current directory (or executable dir), use it.
local_path = Path(os.getcwd()) / CONFIG_FILENAME
# Also check the directory of the script/executable
script_dir_path = (
Path(os.path.dirname(os.path.abspath(sys.argv[0]))) / CONFIG_FILENAME
)
if local_path.exists():
return local_path
if script_dir_path.exists():
return script_dir_path
return DEFAULT_CONFIG_PATH
class Settings:
def __init__(self):
self.config_path = get_config_path()
self.data = {}
for k, v in DEFAULT_SETTINGS.items():
if isinstance(v, list):
self.data[k] = [dict(i) if isinstance(i, dict) else i for i in v]
else:
self.data[k] = v
def load(self):
if self.config_path.exists():
try:
with open(self.config_path, "r", encoding="utf-8") as f:
saved = json.load(f)
self.data.update(saved)
except Exception as _e:
import sys; print(f'[DEBUG] Ignored Exception: {_e}', file=sys.stderr)
return self
def save(self):
self.config_path.parent.mkdir(parents=True, exist_ok=True)
with open(self.config_path, "w", encoding="utf-8") as f:
json.dump(self.data, f, indent=2, ensure_ascii=False)
def add_recent_path(self, path: str):
recent = self.data.get("recent_paths", [])
if path in recent:
recent.remove(path)
recent.insert(0, path)
self.data["recent_paths"] = recent[:10]
self.save()
def __getitem__(self, key):
return self.data[key]
def __setitem__(self, key, value):
self.data[key] = value
def get(self, key, default=None):
return self.data.get(key, default)