1 from ConfigParser import ConfigParser |
1 from ConfigParser import ConfigParser |
|
2 import signal |
|
3 from functools import partial |
|
4 |
|
5 from validate import vInteger |
|
6 from error import ValidateException |
2 |
7 |
3 class Config(ConfigParser): |
8 class Config(ConfigParser): |
4 def __init__(self): |
9 def __init__(self): |
5 ConfigParser.__init__(self) |
10 ConfigParser.__init__(self) |
6 self.reloadList=[] |
11 self.reloadList=[] |
7 |
12 |
8 def reload(self): |
13 def read(self,files): |
9 for f in self.reloadlist: |
14 from offer import providers |
|
15 ConfigParser.read(self, files) |
|
16 for s in self.sections(): |
|
17 if s == "main": |
|
18 opts=main |
|
19 else: |
|
20 opts=providers[self.get(s,'typ')].options |
|
21 |
|
22 for o in opts: |
|
23 try: |
|
24 opts[o].validate(self.get(s,o),o) |
|
25 except ValidateException: |
|
26 if opts[o].must: |
|
27 raise |
|
28 |
|
29 def reload_(self): |
|
30 for f in self.reloadList: |
10 f() |
31 f() |
11 |
32 |
12 def registerReload(self, func): |
33 def registerReload(self, func): |
13 self.reloadList.append(func) |
34 self.reloadList.append(func) |
14 |
35 |
|
36 class Option(): |
|
37 def __init__(self, validate, long="", help="", must=False, default=None): |
|
38 self.validate = validate |
|
39 self.long=long |
|
40 self.help = help |
|
41 self.must = must |
|
42 self.default = default |
|
43 |
|
44 def readConfig(): |
|
45 config.read(confFiles) |
|
46 config.reload() |
|
47 |
|
48 def registerSignal(): |
|
49 '''register readConfig to SIGUSR2''' |
|
50 def rC(signal, frame): |
|
51 readConfig() |
|
52 |
|
53 signal.signal(signal.SIGUSR2,rC) |
|
54 |
15 config=Config() |
55 config=Config() |
|
56 confFiles=["iro.conf", "~/iro.conf","/etc/iro/iro.conf"] |
|
57 main={"hostname":Option(lambda x,y:x,long="Hostname under that twisted is running",must=True), |
|
58 "port":Option(partial(vInteger,minv=0),long="Port under that twisted is running",must=True), |
|
59 } |