-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpw_settings.py
105 lines (93 loc) · 3 KB
/
pw_settings.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
import getpass
from widgets.imports import cmds
class settingsClass(object):
def __init__(self):
home = self.getConfigFolder()
self.active = bool(home)
if self.active:
filename = 'mgeoexporter_settings_' + getpass.getuser()
self.path = os.path.join(home, filename+'.ini')
self.data = None
self.update()
def checkActive(self):
if not self.active:
cmds.warning('Options not active')
return False
else:
return True
def getConfigFolder(self):
home = os.getenv('APPDATA')
if not home:
home = os.path.join(os.path.expanduser('~'), '.config')
if not os.path.exists(home):
try:
os.makedirs(home, 0777)
except:
home = os.path.expanduser('~')
return home
def readFile(self):
if self.checkActive():
lines = []
if os.path.exists(self.path):
try:
with open(self.path, 'r') as f:
lines = f.readlines()
except:
cmds.warning('Cant open options file')
if lines:
lines.pop(0)
return lines
else:
return []
else:
return []
def writeFile(self):
if self.checkActive():
try:
with open(self.path, 'w') as f:
f.write('#mGeo settings file\n')
for i in self.data.items():
f.write(str(i[0]) + '=' + str(i[1]) + '\n')
except:
cmds.warning('Cant open options file 1')
def getValue(self, name, default):
if self.checkActive():
if name in self.data:
return self.toType(str(self.data[name]))
else:
self.data[name] = str(default)
self.writeFile()
return default
return default
def setValue(self, name, value):
if self.checkActive():
self.data[name] = str(value)
self.writeFile()
def update(self):
if self.checkActive():
lines = self.readFile()
self.data = {}
if not lines is None:
for l in lines:
split = l.split('=')
if split:
val = self.toType(split[1])
self.data[split[0]] = val
def getData(self):
return self.data
def toType(self, val):
if val == 'True':
# print 'bool'
return True
elif val == 'False':
# print 'bool'
return False
elif val.isdigit():
# print 'integer'
return int(val)
elif val.replace('.', '').isdigit():
# print 'floatger'
return float(val)
else:
return val.strip()