-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfighandler.py
75 lines (63 loc) · 2.28 KB
/
confighandler.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
import os
import shutil
import configparser
from threading import Lock
# Setup
lockC = Lock() # Lock for Config
CONFIG = {}
# Functions
def save_ini(fpath = 'test.ini', dict_in = {}):
'''Save a dictionary to an INI file'''
if not dict_in:
return 0 # Not saved
file = open(fpath,"w")
config_object = configparser.ConfigParser()
for section, options in dict_in.items():
config_object.add_section(section)
for key, value in options.items():
config_object.set(section, key, str(value))
config_object.write(file)
file.close()
return fpath
def read_ini(fpath):
'''Read a dictionary from an INI file'''
config_object = configparser.ConfigParser()
with open(fpath,"r", encoding='utf-8') as file:
config_object.read_file(file)
output_dict={s:dict(config_object.items(s)) for s in config_object.sections()}
return output_dict
# NOTE: output_dict is a dictionary. Where values are also dicts, their keys are in lowercase
def loadConfig(lockC, forcereload = False):
'''Return config file (read when necessary)'''
with lockC:
global CONFIG
if CONFIG and not forcereload:
return CONFIG
else:
fpath = 'config.ini'
if not os.path.isfile(fpath):
shutil.copy('default_config.ini', 'config.ini')
# READ config.ini and parse contents
CONFIG = read_ini(fpath)
print('Config Loaded from File')
return CONFIG
def editConfigFile():
'''Open config file for editing'''
fpath = 'config.ini'
if not os.path.isfile(fpath):
shutil.copy('default_config.ini', 'config.ini')
os.startfile(fpath)
# Keep for testing
if __name__ == '__main__':
#import json
# data = {'employee': {'name': 'John Doe', 'age': '35'},
# 'job': {'title': 'Software Engineer', 'department': 'IT', 'years_of_experience': '10'},
# 'address': {'street': '123 Main St.', 'city': 'San Francisco', 'state': 'CA', 'zip': '94102'}}
#f = save_ini()
#read_ini(f)
# read config.txt and parse contents as json
# with open('config.txt', 'r') as f:
# data = json.load(f)
#f = save_ini('test.ini', data)
di = read_ini('config.ini')
print(di)