forked from pyfa-org/Pyfa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
459 lines (351 loc) · 13.2 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# =============================================================================
# Copyright (C) 2010 Diego Duclos
#
# This file is part of pyfa.
#
# pyfa is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyfa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyfa. If not, see <http://www.gnu.org/licenses/>.
# =============================================================================
import cPickle
import os.path
import urllib2
import config
import eos.config
from logbook import Logger
pyfalog = Logger(__name__)
class SettingsProvider(object):
BASE_PATH = os.path.join(config.savePath, 'settings')
settings = {}
_instance = None
@classmethod
def getInstance(cls):
if cls._instance is None:
cls._instance = SettingsProvider()
return cls._instance
def __init__(self):
if not os.path.exists(self.BASE_PATH):
os.mkdir(self.BASE_PATH)
def getSettings(self, area, defaults=None):
s = self.settings.get(area)
if s is None:
p = os.path.join(self.BASE_PATH, area)
if not os.path.exists(p):
info = {}
if defaults:
for item in defaults:
info[item] = defaults[item]
else:
try:
f = open(p, "rb")
info = cPickle.load(f)
for item in defaults:
if item not in info:
info[item] = defaults[item]
except:
info = {}
if defaults:
for item in defaults:
info[item] = defaults[item]
self.settings[area] = s = Settings(p, info)
return s
def saveAll(self):
for settings in self.settings.itervalues():
settings.save()
class Settings(object):
def __init__(self, location, info):
self.location = location
self.info = info
def save(self):
f = open(self.location, "wb")
cPickle.dump(self.info, f, cPickle.HIGHEST_PROTOCOL)
def __getitem__(self, k):
try:
return self.info[k]
except KeyError as e:
pyfalog.warning("Failed to get setting for '{0}'. Exception: {1}", k, e)
return None
def __setitem__(self, k, v):
self.info[k] = v
def __iter__(self):
return self.info.__iter__()
def iterkeys(self):
return self.info.iterkeys()
def itervalues(self):
return self.info.itervalues()
def iteritems(self):
return self.info.iteritems()
def keys(self):
return self.info.keys()
def values(self):
return self.info.values()
def items(self):
return self.info.items()
class NetworkSettings(object):
_instance = None
# constants for serviceNetworkDefaultSettings["mode"] parameter
PROXY_MODE_NONE = 0 # 0 - No proxy
PROXY_MODE_AUTODETECT = 1 # 1 - Auto-detected proxy settings
PROXY_MODE_MANUAL = 2 # 2 - Manual proxy settings
@classmethod
def getInstance(cls):
if cls._instance is None:
cls._instance = NetworkSettings()
return cls._instance
def __init__(self):
serviceNetworkDefaultSettings = {
"mode" : self.PROXY_MODE_AUTODETECT,
"type" : "https",
"address" : "",
"port" : "",
"access" : 15,
"login" : None,
"password": None
}
self.serviceNetworkSettings = SettingsProvider.getInstance().getSettings(
"pyfaServiceNetworkSettings", serviceNetworkDefaultSettings)
def isEnabled(self, type):
if type & self.serviceNetworkSettings["access"]:
return True
return False
def toggleAccess(self, type, toggle=True):
bitfield = self.serviceNetworkSettings["access"]
if toggle: # Turn bit on
self.serviceNetworkSettings["access"] = type | bitfield
else: # Turn bit off
self.serviceNetworkSettings["access"] = ~type & bitfield
def getMode(self):
return self.serviceNetworkSettings["mode"]
def getAddress(self):
return self.serviceNetworkSettings["address"]
def getPort(self):
return self.serviceNetworkSettings["port"]
def getType(self):
return self.serviceNetworkSettings["type"]
def getAccess(self):
return self.serviceNetworkSettings["access"]
def setMode(self, mode):
self.serviceNetworkSettings["mode"] = mode
def setAddress(self, addr):
self.serviceNetworkSettings["address"] = addr
def setPort(self, port):
self.serviceNetworkSettings["port"] = port
def setType(self, type):
self.serviceNetworkSettings["type"] = type
def setAccess(self, access):
self.serviceNetworkSettings["access"] = access
@staticmethod
def autodetect():
proxy = None
proxydict = urllib2.ProxyHandler().proxies
validPrefixes = ("http", "https")
for prefix in validPrefixes:
if prefix not in proxydict:
continue
proxyline = proxydict[prefix]
proto = "{0}://".format(prefix)
if proxyline[:len(proto)] == proto:
proxyline = proxyline[len(proto):]
proxAddr, proxPort = proxyline.split(":")
proxPort = int(proxPort.rstrip("/"))
proxy = (proxAddr, proxPort)
break
return proxy
def getProxySettings(self):
if self.getMode() == self.PROXY_MODE_NONE:
return None
if self.getMode() == self.PROXY_MODE_AUTODETECT:
return self.autodetect()
if self.getMode() == self.PROXY_MODE_MANUAL:
return self.getAddress(), int(self.getPort())
def getProxyAuthDetails(self):
if self.getMode() == self.PROXY_MODE_NONE:
return None
if (self.serviceNetworkSettings["login"] is None) or (self.serviceNetworkSettings["password"] is None):
return None
# in all other cases, return tuple of (login, password)
return self.serviceNetworkSettings["login"], self.serviceNetworkSettings["password"]
def setProxyAuthDetails(self, login, password):
if (login is None) or (password is None):
self.serviceNetworkSettings["login"] = None
self.serviceNetworkSettings["password"] = None
return
if login == "": # empty login unsets proxy auth info
self.serviceNetworkSettings["login"] = None
self.serviceNetworkSettings["password"] = None
return
self.serviceNetworkSettings["login"] = login
self.serviceNetworkSettings["password"] = password
class HTMLExportSettings(object):
"""
Settings used by the HTML export feature.
"""
_instance = None
@classmethod
def getInstance(cls):
if cls._instance is None:
cls._instance = HTMLExportSettings()
return cls._instance
def __init__(self):
serviceHTMLExportDefaultSettings = {
"enabled": False,
"path" : config.pyfaPath + os.sep + 'pyfaFits.html',
"minimal": False
}
self.serviceHTMLExportSettings = SettingsProvider.getInstance().getSettings(
"pyfaServiceHTMLExportSettings",
serviceHTMLExportDefaultSettings
)
def getEnabled(self):
return self.serviceHTMLExportSettings["enabled"]
def setEnabled(self, enabled):
self.serviceHTMLExportSettings["enabled"] = enabled
def getMinimalEnabled(self):
return self.serviceHTMLExportSettings["minimal"]
def setMinimalEnabled(self, minimal):
self.serviceHTMLExportSettings["minimal"] = minimal
def getPath(self):
return self.serviceHTMLExportSettings["path"]
def setPath(self, path):
self.serviceHTMLExportSettings["path"] = path
class UpdateSettings(object):
"""
Settings used by update notification
"""
_instance = None
@classmethod
def getInstance(cls):
if cls._instance is None:
cls._instance = UpdateSettings()
return cls._instance
def __init__(self):
# Settings
# Updates are completely suppressed via network settings
# prerelease - If True, suppress prerelease notifications
# version - Set to release tag that user does not want notifications for
serviceUpdateDefaultSettings = {"prerelease": True, 'version': None}
self.serviceUpdateSettings = SettingsProvider.getInstance().getSettings(
"pyfaServiceUpdateSettings",
serviceUpdateDefaultSettings
)
def get(self, type):
return self.serviceUpdateSettings[type]
def set(self, type, value):
self.serviceUpdateSettings[type] = value
class CRESTSettings(object):
_instance = None
@classmethod
def getInstance(cls):
if cls._instance is None:
cls._instance = CRESTSettings()
return cls._instance
def __init__(self):
# mode
# 0 - Implicit authentication
# 1 - User-supplied client details
serviceCRESTDefaultSettings = {"mode": 0, "server": 0, "clientID": "", "clientSecret": "", "timeout": 60}
self.serviceCRESTSettings = SettingsProvider.getInstance().getSettings(
"pyfaServiceCRESTSettings",
serviceCRESTDefaultSettings
)
def get(self, type):
return self.serviceCRESTSettings[type]
def set(self, type, value):
self.serviceCRESTSettings[type] = value
class StatViewSettings(object):
_instance = None
@classmethod
def getInstance(cls):
if cls._instance is None:
cls._instance = StatViewSettings()
return cls._instance
def __init__(self):
# mode
# 0 - Do not show
# 1 - Minimal/Text Only View
# 2 - Full View
serviceStatViewDefaultSettings = {
"resources" : 2,
"resistances" : 2,
"recharge" : 2,
"firepower" : 2,
"capacitor" : 2,
"targetingMisc": 1,
"price" : 2,
"miningyield" : 2,
"drones" : 2,
"outgoing" : 2,
}
# We don't have these....yet
'''
"miningyield": 2,
"drones": 2
'''
self.serviceStatViewDefaultSettings = SettingsProvider.getInstance().getSettings("pyfaServiceStatViewSettings", serviceStatViewDefaultSettings)
def get(self, type):
return self.serviceStatViewDefaultSettings[type]
def set(self, type, value):
self.serviceStatViewDefaultSettings[type] = value
class ContextMenuSettings(object):
_instance = None
@classmethod
def getInstance(cls):
if cls._instance is None:
cls._instance = ContextMenuSettings()
return cls._instance
def __init__(self):
# mode
# 0 - Do not show
# 1 - Show
ContextMenuDefaultSettings = {
"ammoPattern" : 1,
"amount" : 1,
"cargo" : 1,
"changeAffectingSkills" : 1,
"damagePattern" : 1,
"droneRemoveStack" : 1,
"droneSplit" : 1,
"factorReload" : 1,
"fighterAbilities" : 1,
"implantSet" : 1,
"itemStats" : 1,
"marketJump" : 1,
"metaSwap" : 1,
"moduleAmmoPicker" : 1,
"moduleGlobalAmmoPicker": 1,
"openFit" : 1,
"priceClear" : 1,
"project" : 1,
"shipJump" : 1,
"tacticalMode" : 1,
"targetResists" : 1,
"whProjector" : 1,
}
self.ContextMenuDefaultSettings = SettingsProvider.getInstance().getSettings("pyfaContextMenuSettings", ContextMenuDefaultSettings)
def get(self, type):
return self.ContextMenuDefaultSettings[type]
def set(self, type, value):
self.ContextMenuDefaultSettings[type] = value
class EOSSettings(object):
_instance = None
@classmethod
def getInstance(cls):
if cls._instance is None:
cls._instance = EOSSettings()
return cls._instance
def __init__(self):
self.EOSSettings = SettingsProvider.getInstance().getSettings("pyfaEOSSettings", eos.config.settings)
def get(self, type):
return self.EOSSettings[type]
def set(self, type, value):
self.EOSSettings[type] = value
# @todo: migrate fit settings (from fit service) here?