-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_config.py
More file actions
183 lines (139 loc) · 5.25 KB
/
app_config.py
File metadata and controls
183 lines (139 loc) · 5.25 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""Shared configuration helpers for cli_paste."""
import json
import os
import shutil
import sys
APP_NAME = "cli_paste"
SETTINGS_FILE = "settings.json"
DEFAULT_CACHE_DIR = os.path.join(os.path.expanduser("~"), "Pictures", "CLI_temp")
PYTHON_ENV_VAR = "CLI_PASTE_PYTHON"
PYTHONW_ENV_VAR = "CLI_PASTE_PYTHONW"
def get_runtime_executable():
if getattr(sys, "frozen", False):
argv0 = os.path.abspath(sys.argv[0]) if sys.argv else ""
if argv0.lower().endswith(".exe"):
return argv0
return os.path.abspath(sys.executable)
return os.path.abspath(sys.executable)
def get_runtime_app_dir():
if getattr(sys, "frozen", False):
return os.path.dirname(get_runtime_executable())
return os.path.dirname(os.path.abspath(__file__))
def _normalize_path(path):
if not path:
return ""
return os.path.normcase(os.path.normpath(os.path.abspath(path)))
def _sibling_pythonw(path):
if not path:
return ""
return os.path.join(os.path.dirname(os.path.abspath(path)), "pythonw.exe")
def _get_runtime_base_python():
base_executable = getattr(sys, "_base_executable", "") or ""
if base_executable:
return os.path.abspath(base_executable)
if sys.executable:
return os.path.abspath(sys.executable)
return ""
def get_preferred_python(windowless=False):
candidates = []
seen = set()
def add_candidate(path):
if not path:
return
candidate = os.path.abspath(path)
normalized = _normalize_path(candidate)
if normalized in seen:
return
seen.add(normalized)
candidates.append(candidate)
explicit = os.environ.get(PYTHONW_ENV_VAR if windowless else PYTHON_ENV_VAR, "").strip()
add_candidate(explicit)
explicit_console = os.environ.get(PYTHON_ENV_VAR, "").strip()
if windowless and explicit_console:
add_candidate(_sibling_pythonw(explicit_console))
runtime_base_python = _get_runtime_base_python()
if windowless and runtime_base_python:
add_candidate(_sibling_pythonw(runtime_base_python))
elif runtime_base_python:
add_candidate(runtime_base_python)
runtime_python = os.path.abspath(sys.executable) if sys.executable else ""
if _normalize_path(runtime_python) != _normalize_path(runtime_base_python):
if windowless and runtime_python:
add_candidate(_sibling_pythonw(runtime_python))
elif runtime_python:
add_candidate(runtime_python)
discovered = shutil.which("pythonw" if windowless else "python")
add_candidate(discovered)
for candidate in candidates:
if os.path.exists(candidate):
return candidate
if runtime_base_python:
return runtime_base_python
return runtime_python if runtime_python else ("pythonw.exe" if windowless else "python.exe")
def get_venv_home(venv_dir):
cfg_path = os.path.join(venv_dir, "pyvenv.cfg")
try:
with open(cfg_path, "r", encoding="utf-8") as f:
for raw_line in f:
key, _, value = raw_line.partition("=")
if key.strip().lower() == "home":
return value.strip()
except OSError:
return ""
return ""
def is_venv_healthy(venv_dir, preferred_python=""):
scripts_dir = os.path.join(venv_dir, "Scripts")
venv_python = os.path.join(scripts_dir, "python.exe")
venv_pythonw = os.path.join(scripts_dir, "pythonw.exe")
if not os.path.exists(venv_python) or not os.path.exists(venv_pythonw):
return False
venv_home = get_venv_home(venv_dir)
if not venv_home:
return False
home_python = os.path.join(venv_home, "python.exe")
if not os.path.exists(home_python):
return False
if preferred_python:
expected_home = os.path.dirname(os.path.abspath(preferred_python))
return _normalize_path(venv_home) == _normalize_path(expected_home)
return True
def get_settings_dir():
base = os.environ.get("APPDATA")
if not base:
base = os.path.join(os.path.expanduser("~"), "AppData", "Roaming")
return os.path.join(base, APP_NAME)
def get_settings_path():
return os.path.join(get_settings_dir(), SETTINGS_FILE)
def _normalize_dir(path):
if not path:
return ""
return os.path.abspath(os.path.expandvars(os.path.expanduser(path)))
def load_settings():
path = get_settings_path()
try:
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, dict):
return data
except Exception:
pass
return {}
def save_settings(settings):
os.makedirs(get_settings_dir(), exist_ok=True)
path = get_settings_path()
payload = settings if isinstance(settings, dict) else {}
with open(path, "w", encoding="utf-8") as f:
json.dump(payload, f, ensure_ascii=False, indent=2)
def get_cache_dir():
settings = load_settings()
cached = settings.get("cache_dir", "")
normalized = _normalize_dir(cached)
return normalized or DEFAULT_CACHE_DIR
def set_cache_dir(path):
normalized = _normalize_dir(path)
if not normalized:
raise ValueError("cache_dir cannot be empty")
settings = load_settings()
settings["cache_dir"] = normalized
save_settings(settings)
return normalized