forked from hemanta212/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_writer.py
165 lines (143 loc) · 5.05 KB
/
config_writer.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
'''
Config manager file to quickly write app configurations
'''
import json
import datetime
import os
from logger_file import Logger
logger = Logger(console=False).get_logger()
class Config:
'''
Config class with methods
write(key, value)
read() #reads value of a key
get_dict(value)#gives a key from value
delete_key(key)#deletes a key
delete_config()#deletes a config file
empty_config()#empties a config file
'''
def __init__(self, file, backup_dir='~/.cli_backup/'):
self.file_path = os.path.expanduser(file)
self.file = os.path.basename(file)
self.backup_dir = os.path.expanduser(backup_dir)
def write(self, key, value):
'''
writes to a config file as a dictionary
params:
key:name of setting
value: value of setting
Usage:
Config.write('user.email','a@a.com')
'''
if not os.path.exists(self.file_path):
with open(self.file_path, 'w'):
logger.debug("file created")
with open(self.file_path, 'r')as rf:
content = rf.read()
if content != "":
logger.debug("file not empty, appending..")
read_dict = json.loads(content)
read_dict[key] = value
self.write_dict(read_dict)
else:
logger.debug("file empty, first entry")
dump_dict = {}
dump_dict[key] = value
self.write_dict(dump_dict)
def write_dict(self, new_dict):
'''
params: dictionary containing configs
returns : nothing
'''
with open(self.file_path, 'w')as rf:
json.dump(new_dict, rf)
logger.debug("succesfully added config dict")
def delete_config(self, backup=True):
'''
Deletes config_file to backup_dir (both specified in Config class)
Params:
backup [boolean] : Defaults to True
'''
# manage name acc to current datetime
name = str(datetime.datetime.now()) + '.cfg'
if backup:
if not os.path.exists(self.backup_dir):
os.makedirs(self.backup_dir)
logger.debug('creating backup dir')
if os.path.exists(self.file_path):
new_name = os.path.join(self.backup_dir, name)
os.rename(self.file_path, new_name)
logger.debug('deleted')
else:
logger.debug("file not found, check if it exists")
else:
os.remove(self.file_path)
logger.debug('deleted permanently')
def file_exists(self):
'''returns True or False '''
if os.path.exists(self.file_path):
return True
else:
return False
def get_dict(self):
'''
returns: A python dictionary of all configs
'''
with open(self.file_path, 'r')as rf:
json_data = rf.read()
try:
content = json.loads(json_data)
return content
except Exception as e:
raise e # ("file maybe empty or not contain json data")
def read(self, key=None, value=None, all_keys=False, all_values=False):
'''
Reads and return key or value from config file
(returns config dict if no parameter)
Params:
[o] key: Key of dictionary to get the value of
[o] value : value of dictionary to get key of
[o] all_keys [bool] : True returns all keys dict object
[o] all_values [bool]: True returns all values dict obj.
'''
# Check if more than 1 kwargs given
arguments = (key, value, all_keys, all_values)
given = [1 for i in arguments if i]
if len(given) >= 2:
raise ValueError("More than 1 arguments given")
# ensure the file exists.
self.file_exists()
# load the dictionary from config
configs = self.get_dict()
if all_keys:
return configs.keys()
elif all_values:
return configs.values()
elif key:
try:
return configs[key]
except KeyError:
raise KeyError("The key doesnot exist in config")
elif value:
key = [k for k, v in configs.items() if v == value]
if len(key) == 1:
return key[0]
elif len(key) > 1:
return key
else:
raise KeyError("The value doesnot exist in config")
else:
return self.get_dict()
def init_config(self):
'''Empties the file
Raises error with .read() method but applicable with write()
'''
with open(self.file_path, 'w'):
logger.debug("file emptied")
def delete_key(self, key):
'''
Deletes a given key from the config
'''
config_dict = self.get_dict()
del config_dict[key]
self.write_dict(config_dict)