Expand config module for arbitrary section reading
This commit is contained in:
43
config.py
43
config.py
@@ -30,6 +30,7 @@
|
|||||||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
|
import types
|
||||||
|
|
||||||
class Config(object):
|
class Config(object):
|
||||||
"""
|
"""
|
||||||
@@ -40,32 +41,50 @@ class Config(object):
|
|||||||
if (filename and not default) or \
|
if (filename and not default) or \
|
||||||
(not filename and not default): return
|
(not filename and not default): return
|
||||||
|
|
||||||
|
sections = set(default.iterkeys())
|
||||||
if filename:
|
if filename:
|
||||||
cfg = ConfigParser.ConfigParser()
|
cfg = ConfigParser.ConfigParser()
|
||||||
cfg.optionxform = str
|
cfg.optionxform = str
|
||||||
cfg.read(filename)
|
cfg.read(filename)
|
||||||
|
sections.update(cfg.sections())
|
||||||
for h,v in default.iteritems():
|
|
||||||
if not v:
|
for section in sections:
|
||||||
|
if type(section) == types.FunctionType: continue
|
||||||
|
|
||||||
|
match = None
|
||||||
|
for default_section in default.iterkeys():
|
||||||
|
try:
|
||||||
|
if section == default_section or \
|
||||||
|
(type(default_section) == types.FunctionType and default_section(section)):
|
||||||
|
match = default_section
|
||||||
|
break
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if match == None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
optiondefaults = default[match]
|
||||||
|
|
||||||
|
if not optiondefaults:
|
||||||
# Output this whole section as a list of raw key/value tuples
|
# Output this whole section as a list of raw key/value tuples
|
||||||
if not filename:
|
if not filename:
|
||||||
self.__dict__[h] = []
|
self.__dict__[section] = []
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
self.__dict__[h] = cfg.items(h)
|
self.__dict__[section] = cfg.items(section)
|
||||||
except ConfigParser.NoSectionError:
|
except ConfigParser.NoSectionError:
|
||||||
self.__dict__[h] = []
|
self.__dict__[section] = []
|
||||||
else:
|
else:
|
||||||
self.__dict__[h] = Config()
|
self.__dict__[section] = Config()
|
||||||
for name, conv, vdefault in v:
|
for name, conv, vdefault in optiondefaults:
|
||||||
|
|
||||||
if not filename:
|
if not filename:
|
||||||
self.__dict__[h].__dict__[name] = vdefault
|
self.__dict__[section].__dict__[name] = vdefault
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
self.__dict__[h].__dict__[name] = conv(cfg.get(h, name))
|
self.__dict__[section].__dict__[name] = conv(cfg.get(section, name))
|
||||||
except (ValueError, ConfigParser.NoSectionError, ConfigParser.NoOptionError):
|
except (ValueError, ConfigParser.NoSectionError, ConfigParser.NoOptionError):
|
||||||
self.__dict__[h].__dict__[name] = vdefault
|
self.__dict__[section].__dict__[name] = vdefault
|
||||||
|
|
||||||
def x2bool(s):
|
def x2bool(s):
|
||||||
"""Helper function to convert strings from the config to bool"""
|
"""Helper function to convert strings from the config to bool"""
|
||||||
|
@@ -33,6 +33,7 @@ import unittest
|
|||||||
from config import Config, x2bool
|
from config import Config, x2bool
|
||||||
from tempfile import mkstemp
|
from tempfile import mkstemp
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
def create_file(content = None):
|
def create_file(content = None):
|
||||||
"""
|
"""
|
||||||
@@ -53,12 +54,18 @@ domination = True
|
|||||||
somestr = Blabla
|
somestr = Blabla
|
||||||
somenum = 10
|
somenum = 10
|
||||||
testfallbacknum = asdas
|
testfallbacknum = asdas
|
||||||
|
[Server_10]
|
||||||
|
value = False
|
||||||
|
[Server_9]
|
||||||
|
[Server_2]
|
||||||
|
value = True
|
||||||
"""
|
"""
|
||||||
|
|
||||||
cfg_default = {'world':(('domination', x2bool, False),
|
cfg_default = {'world':(('domination', x2bool, False),
|
||||||
('somestr', str, "fail"),
|
('somestr', str, "fail"),
|
||||||
('somenum', int, 0),
|
('somenum', int, 0),
|
||||||
('somenumtest', int, 1)),
|
('somenumtest', int, 1)),
|
||||||
|
(lambda x: re.match("Server_\d+",x)):(('value', x2bool, True),),
|
||||||
'somethingelse':(('bla', str, "test"),)}
|
'somethingelse':(('bla', str, "test"),)}
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@@ -102,6 +109,9 @@ testfallbacknum = asdas
|
|||||||
assert(cfg.world.somenum == 10)
|
assert(cfg.world.somenum == 10)
|
||||||
self.assertRaises(AttributeError, getattr, cfg.world, "testfallbacknum")
|
self.assertRaises(AttributeError, getattr, cfg.world, "testfallbacknum")
|
||||||
assert(cfg.somethingelse.bla == "test")
|
assert(cfg.somethingelse.bla == "test")
|
||||||
|
assert(cfg.Server_10.value == False)
|
||||||
|
assert(cfg.Server_2.value == True)
|
||||||
|
assert(cfg.Server_9.value == True)
|
||||||
finally:
|
finally:
|
||||||
os.remove(path)
|
os.remove(path)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user