/
uconfig.py
66 lines (52 loc) · 1.76 KB
/
uconfig.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
'''
Created on Mar 2, 2011
@author: Vladimir Cvetic
'''
import ConfigParser, os, sys, re
#from _winreg import OpenKey, HKEY_CURRENT_USER, KEY_ALL_ACCESS, QueryValueEx
global ConfigDefault
ConfigDefault = {
"Global":{"imdb": "1",
"storage_dir": "%USERPROFILE%\\Downloads\\Storage",
"levenshtein": "6",
"use_labels": "1",
"debug": "0"
},
"Labels":{}
}
global conf_filename
conf_filename = os.path.join(os.path.dirname(sys.argv[0]), 'config.ini')
def resetToDefault():
os.remove(conf_filename)
write_config()
def write_config(config=None):
"""
given a dictionary with key's of the form 'section.option: value'
write() generates a list of unique section names
creates sections based that list
use config.set to add entries to each section
"""
if config is None:
config = ConfigDefault
rcp = ConfigParser.ConfigParser()
rcp.read(conf_filename)
for section in config:
if rcp.has_section(section) == False:
rcp.add_section(section)
for opt in config[section]:
rcp.set(section, opt, config[section][opt])
rcp.write(open(conf_filename, "w"))
def read_config():
options = {}
config = ConfigParser.ConfigParser()
if os.path.exists(conf_filename)==False:
write_config(ConfigDefault)
config.read(conf_filename)
for section in config.sections():
options[section] = {}
opts = config.options(section)
for opt in opts:
parsed = re.sub('\%USERPROFILE\%', os.environ['USERPROFILE'], config.get(section, opt))
#print parsed
options[section][opt] = parsed
return options