-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_dirs.py
49 lines (43 loc) · 1.43 KB
/
app_dirs.py
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
import appdirs
import os
import sys
class AppDirs():
def __init__(self, appname, isportable=False, portabledatadirname='data'):
self.isportable = isportable
self.portabledatadirname = portabledatadirname
self.appname = appname
def get_appdir(self):
# get path of program dir.
# sys._MEIPASS - variable of pyinstaller (one-dir package) with path to executable
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
#pyinstaller detected!
appdir = sys._MEIPASS
else:
#not pyinstaller
appdir = os.path.dirname(os.path.abspath(__file__))
return appdir
def get_datadir(self):
appdir = self.get_appdir()
if self.isportable:
datadir = os.path.join(appdir, self.portabledatadirname)
else:
datadir = appdirs.user_config_dir(self.appname, False)
try:
os.makedirs(datadir, exist_ok=True)
except Exception as e:
print(e)
else:
return datadir
def get_file(self, filename):
datadir = self.get_datadir()
file = os.path.join(datadir, filename)
if os.path.isfile(file):
return file
else:
try:
with open(file, 'w') as f:
f.write('')
except Exception as e:
print(e)
else:
return file