Skip to content

Commit a8def0d

Browse files
committed
Clean up appdata code on Windows
1 parent dd29b3c commit a8def0d

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

platform_utils/paths.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
import inspect
22
import os
33
import platform
4-
import string
54
import subprocess
65
import sys
6+
77
import platformdirs
88

9+
from . import _winpaths
10+
911

1012
def is_frozen():
1113
""" """
1214
# imp was removed in Python 3.12, but _imp still contains is_frozen.
1315
# This is what cffi (https://cffi.readthedocs.io) uses.
1416
import _imp
1517

16-
return hasattr(sys, "frozen") or '__compiled__' in globals() or _imp.is_frozen("__main__")
18+
return (
19+
hasattr(sys, "frozen")
20+
or "__compiled__" in globals()
21+
or _imp.is_frozen("__main__")
22+
)
1723

1824

1925
plat = platform.system()
2026
is_windows = plat == "Windows"
2127
is_mac = plat == "Darwin"
2228
is_linux = plat == "Linux"
23-
is_pyinstaller = is_frozen() and getattr(sys, '_MEIPASS', False)
29+
is_pyinstaller = is_frozen() and getattr(sys, "_MEIPASS", False)
2430

25-
# Import vendored winpaths functionality for get_program_files()
26-
from . import _winpaths
2731

2832
try:
2933
unicode
@@ -43,10 +47,9 @@ def app_data_path(app_name):
4347
"""Requires the name of the application"""
4448
if is_windows:
4549
# Use roaming=True to match winpaths.get_appdata() behavior (AppData\Roaming)
46-
return platformdirs.user_data_dir(app_name, roaming=True)
50+
path = platformdirs.user_data_dir(roaming=True)
4751
elif is_mac:
48-
path = os.path.join(os.path.expanduser(
49-
"~"), "Library", "Application Support")
52+
path = os.path.join(os.path.expanduser("~"), "Library", "Application Support")
5053
elif is_linux:
5154
path = os.path.expanduser("~")
5255
app_name = ".%s" % app_name.replace(" ", "_")
@@ -59,7 +62,7 @@ def prepare_app_data_path(app_name):
5962
"""Creates the application's data directory, given its name.
6063
6164
Args:
62-
app_name:
65+
app_name:
6366
6467
Returns:
6568
@@ -71,7 +74,7 @@ def prepare_app_data_path(app_name):
7174
def embedded_data_path():
7275
""" """
7376
if is_mac and is_frozen():
74-
return os.path.join(os.path.abspath(get_executable()), 'Contents', 'MacOS')
77+
return os.path.join(os.path.abspath(get_executable()), "Contents", "MacOS")
7578
return app_path()
7679

7780

@@ -132,6 +135,7 @@ def is_interactive():
132135
133136
"""
134137
import __main__
138+
135139
return not hasattr(__main__, "__file__")
136140

137141

@@ -157,15 +161,14 @@ def safe_filename(filename):
157161
"""Given a filename, returns a safe version with no characters that would not work on different platforms.
158162
159163
Args:
160-
filename:
164+
filename:
161165
162166
Returns:
163167
164168
"""
165169
SAFE_FILE_CHARS = "'-_.()[]{}!@#$%^&+=`~ "
166170
filename = unicode(filename)
167-
new_filename = "".join(
168-
c for c in filename if c in SAFE_FILE_CHARS or c.isalnum())
171+
new_filename = "".join(c for c in filename if c in SAFE_FILE_CHARS or c.isalnum())
169172
# Windows doesn't like directory names ending in space, macs consider filenames beginning with a dot as hidden, and windows removes dots at the ends of filenames.
170173
return new_filename.strip(" .")
171174

@@ -174,7 +177,7 @@ def ensure_path(path):
174177
"""Ensure existence of a path by creating all subdirectories.
175178
176179
Args:
177-
path:
180+
path:
178181
179182
Returns:
180183
@@ -188,7 +191,7 @@ def start_file(path):
188191
"""
189192
190193
Args:
191-
path:
194+
path:
192195
193196
Returns:
194197

0 commit comments

Comments
 (0)