Skip to content

Commit 3c2d0e0

Browse files
committed
fix: Refactor user data path handling for better compatibility with frozen executables
1 parent faada05 commit 3c2d0e0

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

base.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,26 @@
2525

2626
VERSION = "v2.3.5"
2727

28-
if getattr(sys, 'frozen', False):
29-
# PyInstaller creates a temp folder and stores path in _MEIPASS
30-
if hasattr(sys, '_MEIPASS'):
31-
bundle_dir = sys._MEIPASS
28+
29+
def get_user_data_path(filename):
30+
"""Get the path for user data files """
31+
if getattr(sys, 'frozen', False):
32+
# If running as PyInstaller exe, put user data files next to the executable
33+
return os.path.join(os.path.dirname(sys.executable), filename)
3234
else:
33-
bundle_dir = os.path.dirname(sys.executable)
34-
os.chdir(bundle_dir)
35+
# If running as script, put files in current directory
36+
return os.path.join(os.path.abspath("."), filename)
37+
38+
39+
# if getattr(sys, 'frozen', False):
40+
# # PyInstaller creates a temp folder and stores path in _MEIPASS
41+
# if hasattr(sys, '_MEIPASS'):
42+
# bundle_dir = sys._MEIPASS
43+
# else:
44+
# bundle_dir = os.path.dirname(sys.executable)
45+
# os.chdir(bundle_dir)
3546

36-
log_file_path = "duce.log"
47+
log_file_path = get_user_data_path("duce.log")
3748

3849

3950
logger.remove()
@@ -781,8 +792,9 @@ def get_now_to_utc(self):
781792
return datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
782793

783794
def load_settings(self):
795+
settings_file = get_user_data_path(f"duce-{self.interface}-settings.json")
784796
try:
785-
with open(f"duce-{self.interface}-settings.json") as f:
797+
with open(settings_file) as f:
786798
self.settings = json.load(f)
787799
except FileNotFoundError:
788800
with open(
@@ -815,7 +827,8 @@ def load_settings(self):
815827
self.instructor_exclude = "\n".join(self.settings["instructor_exclude"])
816828

817829
def save_settings(self):
818-
with open(f"duce-{self.interface}-settings.json", "w") as f:
830+
settings_file = get_user_data_path(f"duce-{self.interface}-settings.json")
831+
with open(settings_file, "w") as f:
819832
json.dump(self.settings, f, indent=4)
820833

821834
def compare_versions(self, version1, version2):

0 commit comments

Comments
 (0)