Repository navigation
Expand file tree
/
Copy pathCoreConfigParser.py
More file actions
165 lines (117 loc) · 5.58 KB
/
Copy pathCoreConfigParser.py
File metadata and controls
165 lines (117 loc) · 5.58 KB
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
# -*- coding: utf-8 -*-
from __future__ import with_statement
from os.path import exists
from os.path import join
CONF_VERSION = 1
########################################################################
class ConfigParser:
#----------------------------------------------------------------------
def __init__(self, configdir):
"""Constructor"""
self.configdir = configdir
self.config = {}
if self.checkVersion():
self.readConfig()
#----------------------------------------------------------------------
def checkVersion(self):
if not exists(join(self.configdir, "pyload.conf")):
return False
f = open(join(self.configdir, "pyload.conf"), "rb")
v = f.readline()
f.close()
v = v[v.find(":")+1:].strip()
if int(v) < CONF_VERSION:
return False
return True
#----------------------------------------------------------------------
def readConfig(self):
"""reads the config file"""
self.config = self.parseConfig(join(self.configdir, "pyload.conf"))
#----------------------------------------------------------------------
def parseConfig(self, config):
"""parses a given configfile"""
f = open(config)
config = f.read()
config = config.split("\n")[1:]
conf = {}
section, option, value, typ, desc = "","","","",""
listmode = False
for line in config:
line = line.rpartition("#") # removes comments
if line[1]:
line = line[0]
else:
line = line[2]
line = line.strip()
try:
if line == "":
continue
elif line.endswith(":"):
section, none, desc = line[:-1].partition('-')
section = section.strip()
desc = desc.replace('"', "").strip()
conf[section] = { "desc" : desc }
else:
if listmode:
if line.endswith("]"):
listmode = False
line = line.replace("]","")
value += [self.cast(typ, x.strip()) for x in line.split(",") if x]
if not listmode:
conf[section][option] = { "desc" : desc,
"type" : typ,
"value" : value}
else:
content, none, value = line.partition("=")
content, none, desc = content.partition(":")
desc = desc.replace('"', "").strip()
typ, option = content.split()
value = value.strip()
if value.startswith("["):
if value.endswith("]"):
listmode = False
value = value[:-1]
else:
listmode = True
value = [self.cast(typ, x.strip()) for x in value[1:].split(",") if x]
else:
value = self.cast(typ, value)
if not listmode:
conf[section][option] = { "desc" : desc,
"type" : typ,
"value" : value}
except:
pass
f.close()
return conf
#----------------------------------------------------------------------
def cast(self, typ, value):
"""cast value to given format"""
if type(value) not in (str, unicode):
return value
if typ == "int":
return int(value)
elif typ == "bool":
return True if value.lower() in ("1","true", "on", "an","yes") else False
else:
return value
#----------------------------------------------------------------------
def get(self, section, option):
"""get value"""
return self.config[section][option]["value"]
#----------------------------------------------------------------------
def __getitem__(self, section):
"""provides dictonary like access: c['section']['option']"""
return Section(self, section)
########################################################################
class Section:
"""provides dictionary like access for configparser"""
#----------------------------------------------------------------------
def __init__(self, parser, section):
"""Constructor"""
self.parser = parser
self.section = section
#----------------------------------------------------------------------
def __getitem__(self, item):
"""getitem"""
return self.parser.get(self.section, item)