-
Notifications
You must be signed in to change notification settings - Fork 5
/
user.py
51 lines (41 loc) · 1.5 KB
/
user.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
50
51
import getpass
import os
import logging
import platform
import six
logger = logging.getLogger("QtPythonConsole")
class User(object):
def __init__(self, appname=None):
self._username = getpass.getuser()
if appname is None:
try:
self.appname = os.path.splitext(os.path.basename(__file__))[0]
except NameError:
self.appname = "python"
else:
self.appname = appname
@property
def appdata(self):
if platform.system() == 'Linux':
return os.getenv('HOME')
if platform.system() in ['Darwin']:
return os.getenv('HOME')
else:
return os.getenv('APPDATA')
def getPreferenceFile(self):
preference = os.path.join(self.appdata, self.appname, "%s.pref" % self._username)
if not os.path.exists(os.path.dirname(preference)):
os.makedirs(os.path.dirname(preference))
return preference
def save(self, text_data, mode="w"):
with open(self.getPreferenceFile(), mode) as fh:
fh.write(six.text_type(text_data))
logger.debug("Preference file updated at %s" % self.getPreferenceFile())
def read(self):
if not os.path.exists(self.getPreferenceFile()):
text_data = ""
else:
with open(self.getPreferenceFile(), "r") as fh:
text_data = fh.read()
logger.debug("Preference file loaded from %s" % self.getPreferenceFile())
return text_data