-
Notifications
You must be signed in to change notification settings - Fork 12
/
_settings.py
185 lines (144 loc) · 5.5 KB
/
_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import os as _os
import spinmob as _s
def read_lines(path):
f = open(path, 'rU')
a = f.readlines()
f.close()
return(a)
class settings():
path_user = ''
path_settings = ''
path_home = ''
path_temp = ''
path_colormaps = ''
_databox = _s.data.databox(delimiter='=')
def __init__(self, name="spinmob"):
"""
This class holds all the user-variables, paths etc...
"""
# assemble the home and temp directory path for this environment
self.path_user = _os.path.expanduser('~')
self.path_home = _os.path.join(self.path_user, "."+name)
self.path_settings = _os.path.join(self.path_home, 'settings.txt')
# see if this is the first time running (no home directory)
if not _os.path.exists(self.path_home):
print("\nFirst run:\n Creating "+self.path_home)
_os.mkdir(self.path_home)
if not _os.path.exists(self.path_settings):
print(" Creating "+self.path_settings + "\n")
f = open(self.path_settings, 'w')
f.close()
# now read in the prefs file
self._databox.load_file(self.path_settings, header_only=True)
def set_get(self, *keys, **kwargs):
"""
Sets all keyword arguments and returns a list of values associated
with each supplied key (if any). If only one key is supplied, returns
the associated value (not a list).
settings.set_get() and settings() are the same function.
"""
# Loop over keyword arguments and set them
for key in kwargs: self.set(key, kwargs[key])
# Create a list of values
values = []
for key in keys: values.append(self.get(key))
# Return the value if there's one or the list if there are multiple
if len(values) == 1: return values[0]
elif len(values) > 1: return values
__call__ = set_get
def __getitem__(self,key): return self.get(key)
def __setitem__(self,key,value): self.set(key, value)
def __str__(self):
s = ''
for key in list(self._databox.hkeys):
s = s + key + " = " + str(self._databox.h(key)) + '\n'
return s
def __repr__(self):
s = '\nSPINMOB SETTINGS'
keys = list(self._databox.hkeys)
keys.sort()
for key in keys:
s = s + "\n " + key + " = " + repr(self._databox.h(key))
return s
def keys(self): return list(self._databox.hkeys)
def has_key(self, key): return key in self._databox.hkeys
def get(self, key):
"""
Checks if the key exists and returns it. Returns None if it doesn't
"""
if key in self._databox.hkeys: return self._databox.h(key)
else: return None
def _set_theme_figures(self, theme):
"""
Sets the matplotlib figure theme.
Parameters
----------
theme : str
Can be 'classic' (for white) or 'dark_background' for dark theme
(or any other theme that exists).
"""
_s.pylab.style.use(theme)
def set(self, key, value=None):
"""
Sets the key-value pair and dumps to the preferences file.
If value=None, pops / removes the setting.
"""
if not key is None:
if not value is None: self._databox.h(**{key:value})
else:
self._databox.pop_header(key, ignore_error=True)
self.save()
return
# If figure theme is provided, update it.
if key == 'dark_theme_figures':
self._set_theme_figures('dark_background' if self['dark_theme_figures'] else 'classic')
if not self['dark_theme_figures']:
_s.pylab.rcParams['figure.facecolor'] = 'white'
# Other pylab settings
if key in ['font_size']:
_s.pylab.rcParams.update({'font.size' : self['font_size']})
# pyqtgraph opengl
if key in ['egg_use_opengl'] and _s._pyqtgraph_ok:
_s._pyqtgraph.setConfigOptions(useOpenGL=self['egg_use_opengl'])
# Save the settings.
self.save()
def pop(self, *keys):
"""
Pops the specified keys.
"""
for key in keys: self.set(key)
def clear(self):
"""
Removes all settings.
"""
self.pop(*self.keys())
def make_dir(self, path="temp"):
"""
Creates a directory of the specified path in the .spinmob directory.
"""
full_path = _os.path.join(self.path_home, path)
# only make it if it doesn't exist!
if not _os.path.exists(full_path): _os.makedirs(full_path)
def list_dir(self, path="temp"):
"""
Returns a list of files in the specified path (directory), or an
empty list if the directory doesn't exist.
"""
full_path = _os.path.join(self.path_home, path)
# only if the path exists!
if _os.path.exists(full_path) and _os.path.isdir(full_path):
return _os.listdir(full_path)
else:
return []
def save(self):
"""
Dumps the current prefs to the preferences.txt file
"""
self._databox.save_file(self.path_settings, force_overwrite=True)
def reset(self):
"""
Clears any user settings and resets expected settings to their defaults.
"""
self.clear()
# Loop over defaults and set them
for _k in _s._defaults: self[_k] = _s._defaults[_k]