-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfiguration.py
79 lines (65 loc) · 2.64 KB
/
configuration.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import json
import logging
import requests
import threadpool
from modules.sitemap.mapper import loadmap
class Configuration(object):
#path: the location of the config file
def __init__(self, path='configuration.json'):
self.load(path)
# save current configuration to json file as default
def save(self):
tmpConfig = {}
for k in self._config:
tmpConfig[k] = getattr(self,k)
if os.access(self._path, os.W_OK):
try:
with open(self._path, 'w') as conf:
conf.write(json.dumps(tmpConfig, indent=4))
self._config = tmpConfig
logging.debug('Configuration Saved')
except:
logging.error('Saving Configuration Failed')
else:
logging.error('Configuration File occupied')
def getConfig(self):
return self._config
def addTarget(self, target):
if not target in getattr(self,'targets'):
getattr(self,'targets').append(target)
def removeTarget(self, target):
while target in getattr(self,'targets'):
getattr(self,'targets').remove(target)
def addShell(self, uri):
while not uri in getattr(self,'shellLocations'):
getattr(self,'shellLocations').append(uri)
def removeShell(self, uri):
while uri in getattr(self,'shellLocations'):
getattr(self,'shellLocations').remove(uri)
def load(self, path):
self._path = os.path.join(os.path.split(os.path.realpath(__file__))[0],path)
if os.access(self._path, os.R_OK):
with open(self._path,'r') as conf:
try:
config = json.loads(conf.read())
self._config = config
for k,v in config.items():
setattr(self,k,v)
pool = threadpool.ThreadPool(getattr(self,'poolSize',16))
setattr(self, 'pool', pool)
setattr(self, 'sitemap', loadmap())
logging.debug('Configuration file loaded')
except:
logging.error('Configuration file format error: ' + self._path)
else:
logging.error('Load configuration error: ' + self._path)
def reload(self):
self.load(self._path)
self.pool = threadpool.ThreadPool(getattr(self,'poolSize',16))
self.sitemap = loadmap()
configuration = Configuration()
if __name__ == '__main__':
print(configuration.sitemap)