diff -r d0fcb1cde990 -r 0d134b173cb1 iro/config.py --- a/iro/config.py Fri Mar 30 11:49:13 2012 +0200 +++ b/iro/config.py Fri Mar 30 15:55:44 2012 +0200 @@ -3,6 +3,7 @@ from ConfigParser import ConfigParser import signal from functools import partial +from collections import OrderedDict from validate import vInteger from error import NeededOption @@ -60,29 +61,26 @@ If one option is valid, the attribute is created with the value of the validate function. """ - def __init__(self, name): + def __init__(self, name, options=None): """ :param string name: section name. + :param `collections.OrderedDict` options: Orderd Dict of the configuration options (see :attr:`options`) """ self.name = name - self.options={ - "port":Option(partial(vInteger,minv=0),long="Port under that twisted is running",must=True), - "dburl":Option(lambda x,y:x,long="Connection URL to database",must=True), - } - """Options dict for Options used in configuration file (see :class:`iro.config.Option`). Ordering of configuration fields are done by :attr:`order`. + + self.options = options + """Options :class:`collections.OrderedDict` for Options used in configuration file (see :class:`iro.config.Option`). Ordering of configuration fields are done by :attr:`order`. Sample:: - {"port":Option(partial(vInteger,minv=0),long="Port under that twisted is running",must=True), - "dburl":Option(lambda x,y:x,long="Connection URL to database",must=True) - } + OrderedDict([ + ("dburl",Option(lambda x,y:x,long="Connection URL to database",must=True)), + ("port",Option(partial(vInteger,minv=0),long="Port under that twisted is running",must=True)), + ]) A child class typically use update to add more options. """ - - self.order = ["dburl","port"] - """ A list for ordering the options dict (:attr:`options`). """ - + self._init = True """indecates, if the config is loaded with config values.""" @@ -140,7 +138,7 @@ :return: a list of lines """ ret=[] - for o in self.order: + for o in self.options: opt=self.options[o] if opt.long: ret.append("# "+opt.long) @@ -163,7 +161,7 @@ if main._init: main.load(configParser.items("main")) else: - m = Config("main") + m = Config("main", main_options) m.load(configParser.items("main")) if not main.same(m): raise Exception("Main options can't be reloaded, please restart your Application.") @@ -186,5 +184,12 @@ confFiles=["iro.conf", "~/iro.conf","/etc/iro/iro.conf"] """Configfile list """ -main = Config("main") +main_options = OrderedDict([ + ("dburl",Option(lambda x,y:x,long="Connection URL to database",must=True)), + ("port",Option(partial(vInteger,minv=0),long="Port under that twisted is running",must=True)), + ]) + +"options for main section" + +main = Config("main", main_options) """Main config options"""