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
|
# Here conf is already an instantiation of the underlying
# Config class.
#
import os
import sys
import pson
import gzip
import platform
#-- create a single instance of config object
conf = object()
#-- global configuration data ---------------------------------------------
class ConfigDict(dict):
# start
def __init__(self):
# object==dict means conf.var is conf["var"]
self.__dict__ = self # let's pray this won't leak memory due to recursion issues
|
|
>
>
<
>
|
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
|
# Here conf is already an instantiation of the underlying
# Config class.
#
import os
import sys
import json
import gzip
import platform
#-- create a single instance of config object
conf = object()
#-- global configuration data ---------------------------------------------
class ConfigDict(dict):
# start
def __init__(self):
# object==dict means conf.var is conf["var"]
self.__dict__ = self # let's pray this won't leak memory due to recursion issues
|
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
last = self.load("settings")
if (last):
self.update(last)
# store defaults in file
else:
self.save("settings")
self.firstrun = 1
# some defaults
def defaults(self):
self.browser = "sensible-browser"
self.play = {
"audio/mp3": "audacious ", # %u for url to .pls, %g for downloaded .m3u
"audio/ogg": "audacious ",
|
>
|
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
last = self.load("settings")
if (last):
self.update(last)
# store defaults in file
else:
self.save("settings")
self.firstrun = 1
# some defaults
def defaults(self):
self.browser = "sensible-browser"
self.play = {
"audio/mp3": "audacious ", # %u for url to .pls, %g for downloaded .m3u
"audio/ogg": "audacious ",
|
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
self.auto_save_appstate = 1
self.theme = "" #"MountainDew"
self.debug = False
self.channel_order = "shoutcast, xiph, internet_radio_org_uk, jamendo, myoggradio, .."
self.reuse_m3u = 1
self.google_homepage = 1
self.windows = platform.system()=="Windows"
# each plugin has a .config dict list, we add defaults here
def add_plugin_defaults(self, config, module=""):
# options
for opt in config:
|
>
|
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
self.auto_save_appstate = 1
self.theme = "" #"MountainDew"
self.debug = False
self.channel_order = "shoutcast, xiph, internet_radio_org_uk, jamendo, myoggradio, .."
self.reuse_m3u = 1
self.google_homepage = 1
self.windows = platform.system()=="Windows"
self.debug = 1
# each plugin has a .config dict list, we add defaults here
def add_plugin_defaults(self, config, module=""):
# options
for opt in config:
|
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
186
187
188
189
190
191
192
193
194
195
|
if (not os.path.exists(self.dir)):
os.makedirs(self.dir)
# store some configuration list/dict into a file
def save(self, name="settings", data=None, gz=0, nice=0):
name = name + ".json"
if (data == None):
data = dict(self.__dict__) # ANOTHER WORKAROUND: typecast to plain dict(), else json filter_data sees it as object and str()s it
nice = 1
# check for subdir
if (name.find("/") > 0):
subdir = name[0:name.find("/")]
subdir = self.dir + "/" + subdir
if (not os.path.exists(subdir)):
os.mkdir(subdir)
open(subdir+"/.nobackup", "w").close()
# write
file = self.dir + "/" + name
# .gz or normal file
if gz:
f = gzip.open(file+".gz", "w")
if os.path.exists(file):
os.unlink(file)
else:
f = open(file, "w")
# encode
pson.dump(data, f, indent=(4 if nice else None))
f.close()
# retrieve data from config file
def load(self, name):
name = name + ".json"
file = self.dir + "/" + name
try:
# .gz or normal file
if os.path.exists(file + ".gz"):
f = gzip.open(file + ".gz", "r")
elif os.path.exists(file):
f = open(file, "r")
else:
return # file not found
# decode
r = pson.load(f)
f.close()
return r
except Exception as e:
print("PSON parsing error (in "+name+")", e)
# recursive dict update
def update(self, with_new_data):
for key,value in with_new_data.items():
if type(value) == dict:
self[key].update(value)
else:
self[key] = value
# descends into sub-dicts instead of wiping them with subkeys
# check for existing filename in directory list
def find_in_dirs(self, dirs, file):
for d in dirs:
if os.path.exists(d+"/"+file):
return d+"/"+file
#-- actually fill global conf instance
conf = ConfigDict()
# wrapper for all print statements
def __print__(*args):
if conf.debug:
print(" ".join([str(a) for a in args]))
|
|
|
|
|
|
|
<
<
<
<
<
|
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
186
187
188
189
190
191
192
193
194
|
if (not os.path.exists(self.dir)):
os.makedirs(self.dir)
# store some configuration list/dict into a file
def save(self, name="settings", data=None, gz=0, nice=0):
name = name + ".json"
if (data is None):
data = dict(self.__dict__) # ANOTHER WORKAROUND: typecast to plain dict(), else json filter_data sees it as object and str()s it
nice = 1
# check for subdir
if (name.find("/") > 0):
subdir = name[0:name.find("/")]
subdir = self.dir + "/" + subdir
if (not os.path.exists(subdir)):
os.mkdir(subdir)
open(subdir+"/.nobackup", "w").close()
# write
file = self.dir + "/" + name
# .gz or normal file
if gz:
f = gzip.open(file+".gz", "w")
if os.path.exists(file):
os.unlink(file)
else:
f = open(file, "w")
# encode
json.dump(data, f, indent=(4 if nice else None))
f.close()
# retrieve data from config file
def load(self, name):
name = name + ".json"
file = self.dir + "/" + name
try:
# .gz or normal file
if os.path.exists(file + ".gz"):
f = gzip.open(file + ".gz", "rt")
elif os.path.exists(file):
f = open(file, "rt")
else:
return # file not found
# decode
r = json.load(f)
f.close()
return r
except Exception as e:
print(dbg.ERR, "PSON parsing error (in "+name+")", e)
# recursive dict update
def update(self, with_new_data):
for key,value in with_new_data.items():
if type(value) == dict:
self[key].update(value)
else:
self[key] = value
# descends into sub-dicts instead of wiping them with subkeys
# check for existing filename in directory list
def find_in_dirs(self, dirs, file):
for d in dirs:
if os.path.exists(d+"/"+file):
return d+"/"+file
# wrapper for all print statements
def __print__(*args):
if conf.debug:
print(" ".join([str(a) for a in args]))
|
203
204
205
206
207
208
209
210
211
|
"CONF": "[33m[CONF][0m", # brown CONFIG DATA
"UI": "[34m[UI][0m", # blue USER INTERFACE BEHAVIOUR
"HTTP": "[35m[HTTP][0m", # magenta HTTP REQUEST
"DATA": "[36m[DATA][0m", # cyan DATA
"INFO": "[37m[INFO][0m", # gray INFO
"STAT": "[37m[STATE][0m", # gray CONFIG STATE
})
|
>
>
>
>
>
>
>
>
>
|
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
"CONF": "[33m[CONF][0m", # brown CONFIG DATA
"UI": "[34m[UI][0m", # blue USER INTERFACE BEHAVIOUR
"HTTP": "[35m[HTTP][0m", # magenta HTTP REQUEST
"DATA": "[36m[DATA][0m", # cyan DATA
"INFO": "[37m[INFO][0m", # gray INFO
"STAT": "[37m[STATE][0m", # gray CONFIG STATE
})
#-- actually fill global conf instance
conf = ConfigDict()
if conf:
__print__(dbg.PROC, "ConfigDict() initialized")
|