-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.py
68 lines (56 loc) · 1.84 KB
/
config.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
from os.path import expanduser,isfile,dirname
from log import Log
from json import dumps, dump, load
from io import open
from os import mkdir,stat
class Config:
path = "/.amplifier/config"
def __init__(self,log):
self.logger = log
self.home = expanduser("~")
self.path = "/.amplifier/config"
self.full_path = self.home+self.path
self.logger.debug("Home directory is : " + self.full_path)
def check_config(self):
value_to_return = False
if isfile(self.full_path):
self.logger.debug("file found")
value_to_return = True
else:
self.logger.debug("file not found")
return value_to_return
def create_config(self,ip):
data = {}
data['address'] = ip
json_data = dumps(data)
self.logger.debug("From Config create json : " + str(json_data))
directory = dirname(self.full_path)
try:
stat(directory)
except:
mkdir(directory)
with open(self.full_path, 'w') as outfile:
dump(data, outfile)
def read_config(self):
directory = dirname(self.full_path)
try:
stat(directory)
except:
self.logger.debug("From config : file not exist")
return None
else:
with open(self.full_path) as f:
data = load(f)
self.logger.debug("reading data : " + str(data))
return data['address']
if __name__ == "__main__":
# execute only if run as a script
test = None
logger = Log(test).get_logger()
maconf = Config(logger)
if maconf.check_config():
logger.debug("path : "+str(maconf.check_config()))
ip = maconf.read_config()
logger.debug("Ip was read at : " + str(ip))
else:
maconf.create_config("192.168.0.200")