1 from twisted.python import log |
1 from twisted.python import log |
2 |
2 |
3 from ConfigParser import ConfigParser |
3 from ConfigParser import ConfigParser |
4 import signal |
4 import signal |
5 from functools import partial |
5 from functools import partial |
|
6 from collections import OrderedDict |
6 |
7 |
7 from validate import vInteger |
8 from validate import vInteger |
8 from error import NeededOption |
9 from error import NeededOption |
9 |
10 |
10 class MyConfigParser(ConfigParser): |
11 class MyConfigParser(ConfigParser): |
58 class Config: |
59 class Config: |
59 """Base class for all classes, that uses option from configfile. |
60 """Base class for all classes, that uses option from configfile. |
60 |
61 |
61 If one option is valid, the attribute is created with the value of the validate function. |
62 If one option is valid, the attribute is created with the value of the validate function. |
62 """ |
63 """ |
63 def __init__(self, name): |
64 def __init__(self, name, options=None): |
64 """ |
65 """ |
65 :param string name: section name. |
66 :param string name: section name. |
|
67 :param `collections.OrderedDict` options: Orderd Dict of the configuration options (see :attr:`options`) |
66 """ |
68 """ |
67 self.name = name |
69 self.name = name |
68 self.options={ |
70 |
69 "port":Option(partial(vInteger,minv=0),long="Port under that twisted is running",must=True), |
71 self.options = options |
70 "dburl":Option(lambda x,y:x,long="Connection URL to database",must=True), |
72 """Options :class:`collections.OrderedDict` for Options used in configuration file (see :class:`iro.config.Option`). Ordering of configuration fields are done by :attr:`order`. |
71 } |
|
72 """Options dict for Options used in configuration file (see :class:`iro.config.Option`). Ordering of configuration fields are done by :attr:`order`. |
|
73 |
73 |
74 Sample:: |
74 Sample:: |
75 |
75 |
76 {"port":Option(partial(vInteger,minv=0),long="Port under that twisted is running",must=True), |
76 OrderedDict([ |
77 "dburl":Option(lambda x,y:x,long="Connection URL to database",must=True) |
77 ("dburl",Option(lambda x,y:x,long="Connection URL to database",must=True)), |
78 } |
78 ("port",Option(partial(vInteger,minv=0),long="Port under that twisted is running",must=True)), |
|
79 ]) |
79 |
80 |
80 A child class typically use update to add more options. |
81 A child class typically use update to add more options. |
81 """ |
82 """ |
82 |
83 |
83 self.order = ["dburl","port"] |
|
84 """ A list for ordering the options dict (:attr:`options`). """ |
|
85 |
|
86 self._init = True |
84 self._init = True |
87 """indecates, if the config is loaded with config values.""" |
85 """indecates, if the config is loaded with config values.""" |
88 |
86 |
89 |
87 |
90 def _read(self, cfg, write=False): |
88 def _read(self, cfg, write=False): |
161 configParser.read(confFiles) |
159 configParser.read(confFiles) |
162 configParser.reload_() |
160 configParser.reload_() |
163 if main._init: |
161 if main._init: |
164 main.load(configParser.items("main")) |
162 main.load(configParser.items("main")) |
165 else: |
163 else: |
166 m = Config("main") |
164 m = Config("main", main_options) |
167 m.load(configParser.items("main")) |
165 m.load(configParser.items("main")) |
168 if not main.same(m): |
166 if not main.same(m): |
169 raise Exception("Main options can't be reloaded, please restart your Application.") |
167 raise Exception("Main options can't be reloaded, please restart your Application.") |
170 |
168 |
171 def init(): |
169 def init(): |
184 """configParser to get configuration.""" |
182 """configParser to get configuration.""" |
185 |
183 |
186 confFiles=["iro.conf", "~/iro.conf","/etc/iro/iro.conf"] |
184 confFiles=["iro.conf", "~/iro.conf","/etc/iro/iro.conf"] |
187 """Configfile list """ |
185 """Configfile list """ |
188 |
186 |
189 main = Config("main") |
187 main_options = OrderedDict([ |
|
188 ("dburl",Option(lambda x,y:x,long="Connection URL to database",must=True)), |
|
189 ("port",Option(partial(vInteger,minv=0),long="Port under that twisted is running",must=True)), |
|
190 ]) |
|
191 |
|
192 "options for main section" |
|
193 |
|
194 main = Config("main", main_options) |
190 """Main config options""" |
195 """Main config options""" |