Skip to content

Commit 1cae337

Browse files
committed
accommodate for missing resource files
1 parent a405643 commit 1cae337

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

ballontranslator/launch.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import argparse
44
import os.path as osp
55
import os
6+
import shutil
67
import subprocess
78
from platform import platform
89

@@ -64,6 +65,36 @@ def setup_locks():
6465
RUNTIME_LOCKS['model_loading'] = QMutex()
6566

6667

68+
def ensure_resource_theme_files(program_path: str = None, logger=None) -> list:
69+
"""Copy moved stylesheet/theme files from the old config location if needed.
70+
71+
>>> ensure_resource_theme_files('/path/that/does/not/exist')
72+
[]
73+
"""
74+
75+
root = Path(program_path or shared.PROGRAM_PATH)
76+
copied = []
77+
for filename in ('stylesheet.css', 'themes.json'):
78+
target_path = root / 'resources' / filename
79+
if target_path.exists():
80+
continue
81+
82+
source_path = root / 'config' / filename
83+
if not source_path.exists():
84+
continue
85+
86+
try:
87+
target_path.parent.mkdir(parents=True, exist_ok=True)
88+
shutil.copy2(source_path, target_path)
89+
copied.append(filename)
90+
if logger is not None:
91+
logger.info(f'Copied missing resource file from old config path: {filename}')
92+
except OSError as e:
93+
if logger is not None:
94+
logger.warning(f'Failed to copy missing resource file {filename}: {e}')
95+
return copied
96+
97+
6798
def preload_msvc_runtime():
6899
"""Best-effort preload of the MSVC runtime before Qt alters DLL lookup.
69100
@@ -218,6 +249,7 @@ def main():
218249

219250
from qtpy.QtCore import QTranslator, QLocale, Qt
220251
setup_logging(shared.LOGGING_PATH)
252+
ensure_resource_theme_files(APP_DIR, LOGGER)
221253
shared.args = args
222254
shared.DEFAULT_DISPLAY_LANG = QLocale.system().name().replace('en_CN', 'zh_CN')
223255
shared.HEADLESS = args.headless

0 commit comments

Comments
 (0)