1 | #!/usr/bin/python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | """Classes and functions for configuration file handling
|
---|
4 | """
|
---|
5 |
|
---|
6 | import sys
|
---|
7 | import re
|
---|
8 | import resource
|
---|
9 | import directive
|
---|
10 |
|
---|
11 | RESOURCE_TYPES = ('dird', 'console', 'filed', 'stored')
|
---|
12 |
|
---|
13 | rxp_item = re.compile('\s*(?P<Name>[\S]+)\s*=\s*(?P<Value>.*)$') # xxx = xxx matchen
|
---|
14 | rxp_openbrace = re.compile('\s*(?P<Name>[\S ]+)=?\s+{.*') # match xxx {
|
---|
15 | rxp_closebrace = re.compile('.*}.*') # match }
|
---|
16 | rxp_comment = re.compile('^\s*#.*')
|
---|
17 |
|
---|
18 |
|
---|
19 |
|
---|
20 | #'dird', 'console', 'filed' or 'stored'
|
---|
21 | class Config(object):
|
---|
22 | """Class for bacula configuration access"""
|
---|
23 |
|
---|
24 | def __init__(self, resource_type, filename=""):
|
---|
25 | self.resource_type = resource_type
|
---|
26 | self.filename = filename
|
---|
27 | self.resources = resource.Resource(level=0)
|
---|
28 |
|
---|
29 | def read(self):
|
---|
30 | print "reading %s" % self.filename
|
---|
31 | self.parse()
|
---|
32 | print self.resources
|
---|
33 |
|
---|
34 |
|
---|
35 | def write(self):
|
---|
36 | pass
|
---|
37 |
|
---|
38 | def parse(self):
|
---|
39 | multiRes = set(['Run','File','Ip']) # entries, which can be multiple times in a ressource
|
---|
40 | f = open(self.filename)
|
---|
41 | dirRes=[] # Liste der Director Ressourcen
|
---|
42 | curRes = self.resources
|
---|
43 | braceLevel = 0;
|
---|
44 | tmpRes = {}
|
---|
45 | treeindex=range(10)
|
---|
46 | treeindex[0]=curRes
|
---|
47 |
|
---|
48 | print resource.directives
|
---|
49 |
|
---|
50 | for line in f:
|
---|
51 | #print braceLevel,line
|
---|
52 | commentline = rxp_comment.match(line)
|
---|
53 | if commentline:
|
---|
54 | curRes.add_comment(line.strip())
|
---|
55 | continue
|
---|
56 | print braceLevel
|
---|
57 | openbraceline = rxp_openbrace.match(line)
|
---|
58 | closebraceline = rxp_closebrace.match(line)
|
---|
59 | item = rxp_item.match(line)
|
---|
60 |
|
---|
61 | if openbraceline:
|
---|
62 | braceLevel += 1
|
---|
63 | resname = openbraceline.group("Name")
|
---|
64 |
|
---|
65 | if not resname in resource.directives:
|
---|
66 | raise "Unknown Resource: %s" % resname
|
---|
67 |
|
---|
68 | print resname
|
---|
69 | newRes = eval("resource.%s(braceLevel)" % resname)
|
---|
70 | print newRes
|
---|
71 | curRes.add_item(newRes)
|
---|
72 | treeindex[braceLevel]=curRes;
|
---|
73 | curRes = newRes
|
---|
74 | elif closebraceline:
|
---|
75 | braceLevel -= 1
|
---|
76 | curRes = treeindex[braceLevel]
|
---|
77 | elif item:
|
---|
78 | name = item.group("Name")
|
---|
79 | value = item.group("Value")
|
---|
80 | curRes.add_item(directive.Item(name, value))
|
---|
81 |
|
---|
82 |
|
---|
83 | class DirdConfig(Config):
|
---|
84 |
|
---|
85 | def __init__(self, filename=""):
|
---|
86 | Config.__init__(self, 'dird', filename)
|
---|
87 |
|
---|
88 | class ConsoleConfig(Config):
|
---|
89 | pass
|
---|
90 |
|
---|
91 | class FiledConfig(Config):
|
---|
92 | pass
|
---|
93 |
|
---|
94 | class StoredConfig(Config):
|
---|
95 | pass
|
---|
96 |
|
---|
97 | if __name__ == "__main__":
|
---|
98 |
|
---|
99 | dirdcfg = DirdConfig("../conf/bacula-dir.conf")
|
---|
100 | dirdcfg.read()
|
---|
101 |
|
---|
102 | sys.exit(0)
|
---|