-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicconfig.py
165 lines (128 loc) · 4.72 KB
/
basicconfig.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
# From https://github.com/kdart/pycopia/blob/master/core/pycopia/basicconfig.py
"""
Basic configuration holder objects.
"""
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
import sys, os
import warnings
if sys.version_info.major == 3:
def execfile(fn, glbl, loc):
exec(open(fn, encoding='utf-8').read(), glbl, loc)
else: # python 2
execfile = execfile
class BasicConfigError(Exception):
pass
class ConfigLockError(BasicConfigError):
pass
class ConfigReadError(BasicConfigError):
pass
class ConfigHolder(dict):
"""ConfigHolder() Holds named configuration information. For convenience,
it maps attribute access to the real dictionary. This object is lockable, use
the 'lock' and 'unlock' methods to set its state. If locked, new keys or
attributes cannot be added, but existing ones may be changed."""
def __init__(self, init={}, name=None):
name = name or self.__class__.__name__.lower()
dict.__init__(self, init)
dict.__setattr__(self, "_locked", 0)
dict.__setattr__(self, "_name", name)
def __getstate__(self):
return self.__dict__.items()
def __setstate__(self, items):
for key, val in items:
self.__dict__[key] = val
def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, dict.__repr__(self))
def __str__(self):
n = self._name
s = ["{}(name={!r}):".format(self.__class__.__name__, n)]
s = s + [" {}.{} = {!r}".format(n, it[0], it[1]) for it in self.items()]
s.append("\n")
return "\n".join(s)
def __setitem__(self, key, value):
if self._locked and not key in self:
raise ConfigLockError("setting attribute on locked config holder")
return super(ConfigHolder, self).__setitem__(key, value)
def __getitem__(self, name):
return super(ConfigHolder, self).__getitem__(name)
def __delitem__(self, name):
return super(ConfigHolder, self).__delitem__(name)
__getattr__ = __getitem__
__setattr__ = __setitem__
# __delattr__ = __delitem__
def lock(self):
dict.__setattr__(self, "_locked", 1)
def unlock(self):
dict.__setattr__(self, "_locked", 0)
def islocked(self):
return self._locked
def copy(self):
ch = ConfigHolder(self)
if self.islocked():
ch.lock()
return ch
def add_section(self, name):
self.name = SECTION(name)
class SECTION(ConfigHolder):
def __init__(self, name):
super(SECTION, self).__init__(name=name)
def __repr__(self):
return super(SECTION, self).__str__()
class BasicConfig(ConfigHolder):
def mergefile(self, filename, globalspace=None):
"""Merge in a Python syntax configuration file that should assign
global variables that become keys in the configuration. Returns
True if file read OK, False otherwise."""
if os.path.isfile(filename):
gb = globalspace or {} # temporary global namespace for config files.
gb["SECTION"] = SECTION
gb["sys"] = sys # in case config stuff needs these.
gb["os"] = os
def include(fname):
execfile(get_pathname(fname), gb, self)
gb["include"] = include
try:
execfile(filename, gb, self)
except:
ex, val, tb = sys.exc_info()
warnings.warn("BasicConfig: error reading %s: %s (%s)." % (filename, ex, val))
return False
else:
return True
else:
return False
def get_pathname(basename):
# basename = os.path.expandvars(os.path.expanduser(basename))
# if os.sep not in basename:
# basename = os.path.join(os.sep, "etc", "pycopia", basename)
return basename
# main function for getting a configuration file. gets it from the common
# configuration location (/etc/pycopia), but if a full path is given then
# use that instead.
def get_config(fname, initdict=None, globalspace=None, **kwargs):
fname = get_pathname(fname)
cf = BasicConfig()
if cf.mergefile(fname, globalspace):
if initdict:
cf.update(initdict)
cf.update(kwargs)
return cf
else:
raise ConfigReadError("did not successfully read %r." % (fname,))
def check_config(fname):
"""check_config(filename) -> bool
Check is a config file can be read without errors and contains
something.
"""
fname = get_pathname(fname)
cf = BasicConfig()
if cf.mergefile(fname):
return bool(cf)
else:
return False
def _test(argv):
cf = get_config("config.conf")
print (cf)