1 | # -*- coding: utf-8 -*-
|
---|
2 | """Classes and functions for configuration file handling
|
---|
3 | """
|
---|
4 |
|
---|
5 | import sys
|
---|
6 | import re
|
---|
7 |
|
---|
8 | RESOURCE_TYPES = ('dird', 'console', 'filed', 'stored')
|
---|
9 |
|
---|
10 | #'dird', 'console', 'filed' or 'stored'
|
---|
11 | class Config(Object):
|
---|
12 | """Class for bacula configuration access"""
|
---|
13 |
|
---|
14 | def __init__(self, resource_type, filename=""):
|
---|
15 | self.resource_type = resource_type
|
---|
16 | self.filename = filename
|
---|
17 |
|
---|
18 | def read(self):
|
---|
19 | pass
|
---|
20 |
|
---|
21 | def write(self):
|
---|
22 | pass
|
---|
23 |
|
---|
24 | class DirdConfig(Config):
|
---|
25 | pass
|
---|
26 |
|
---|
27 | class ConsoleConfig(Config):
|
---|
28 | pass
|
---|
29 |
|
---|
30 | class FiledConfig(Config):
|
---|
31 | pass
|
---|
32 |
|
---|
33 | class StoredConfig(Config):
|
---|
34 | pass
|
---|
35 |
|
---|
36 | class baculaRessource:
|
---|
37 | """ generic bacula support Type
|
---|
38 | """
|
---|
39 | name = None;
|
---|
40 | resourceDirectives = {}
|
---|
41 | mandatoryDirectives = {} # which directives must be configured?
|
---|
42 |
|
---|
43 | def printdict(self,mydict,indent=''):
|
---|
44 | ressource = 'unknown'
|
---|
45 | #indent += ' '
|
---|
46 | if type(mydict) == dict :
|
---|
47 | if '_ressourceName' in mydict:
|
---|
48 | v = mydict['_ressourceName']
|
---|
49 | ressource = v
|
---|
50 | print indent + v + " {"
|
---|
51 | indent += ' '
|
---|
52 |
|
---|
53 | for p,v in mydict.iteritems():
|
---|
54 | #print "v: " , v
|
---|
55 | if type(v) == str: # leaf
|
---|
56 | if p == '_ressourceName':
|
---|
57 | continue
|
---|
58 | print indent + p + " = " + v
|
---|
59 |
|
---|
60 | elif type(v) == dict :
|
---|
61 | #print indent + p + " {"
|
---|
62 | #ressource = p
|
---|
63 | self.printdict(v,indent)
|
---|
64 |
|
---|
65 | elif type(v) == list :
|
---|
66 | #ressource = "list"
|
---|
67 | for i in v:
|
---|
68 | print indent + p + " = " + i
|
---|
69 |
|
---|
70 | if type(mydict) == list :
|
---|
71 | for i in mydict:
|
---|
72 |
|
---|
73 | if i == '_ressourceName':
|
---|
74 | Name = i['Name']
|
---|
75 | print indent + Name
|
---|
76 | self.printdict(i,indent)
|
---|
77 | else: # leaf
|
---|
78 | print indent + i
|
---|
79 |
|
---|
80 | if type(mydict) == str:
|
---|
81 | print indent + "direct" + mydict
|
---|
82 | indent = indent[:-2]
|
---|
83 | print indent + "} # " + ressource
|
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
87 | def __init__(self, name, resourceDirectives):
|
---|
88 | #resourceDirectives = {'name':'bacula-dir', 'Test':'test'} # the configured directives
|
---|
89 | self.name = name
|
---|
90 | self.resourceDirectives = resourceDirectives
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 | def __str__(self): # overload print function
|
---|
95 | self.printdict(self.resourceDirectives,"")
|
---|
96 | s = ''
|
---|
97 | #s = self.name + ' {\n'
|
---|
98 | #for directive,value in self.resourceDirectives.iteritems() :
|
---|
99 | # s += ' ' +directive + ' = ' + value + '\n'
|
---|
100 | #s += '}\n'
|
---|
101 | return s
|
---|
102 |
|
---|
103 |
|
---|
104 | mybaculaRessource = baculaRessource("myRessource",{'name':'bacula-dir', 'Test':'test'})
|
---|
105 |
|
---|
106 |
|
---|
107 | print mybaculaRessource
|
---|
108 |
|
---|
109 | multiRes = set(['Run','File','Ip']) # entries, which can be multiple times in a ressource
|
---|
110 |
|
---|
111 | f = open("bacula-dir.conf",'r')
|
---|
112 |
|
---|
113 | #p = re.compile('\s*([\S ]+)\s*=\s*(\S+)\s*') # xxx = xxx matchen
|
---|
114 | p = re.compile('\s*([\S]+)\s*=\s*(.*)$') # xxx = xxx matchen
|
---|
115 | #openbrace = re.compile('\s*([\S ]+)\s*{.*') # match xxx {
|
---|
116 | openbrace = re.compile('\s*([\S ]+)=?\s+{.*') # match xxx {
|
---|
117 |
|
---|
118 | closebrace = re.compile('.*}.*') # match }
|
---|
119 | comment = re.compile('^\s*#.*')
|
---|
120 |
|
---|
121 | #myRes = {} ; # RessourcenDict erzeugen
|
---|
122 |
|
---|
123 | dirRes=[] # Liste der Director Ressourcen
|
---|
124 |
|
---|
125 |
|
---|
126 |
|
---|
127 | braceLevel = 0;
|
---|
128 | #print p
|
---|
129 | tmpRes = {}
|
---|
130 | treeindex=range(10)
|
---|
131 |
|
---|
132 | for line in f:
|
---|
133 | #print braceLevel,line
|
---|
134 | commentline = comment.match(line)
|
---|
135 | if commentline:
|
---|
136 | #print "found commentline: " + commentline.group(0)
|
---|
137 | continue
|
---|
138 | #print braceLevel
|
---|
139 | openbraceline = openbrace.match(line)
|
---|
140 | closebraceline = closebrace.match(line)
|
---|
141 | m = p.match(line)
|
---|
142 |
|
---|
143 | if openbraceline:
|
---|
144 | #print "found openbraceline: " + openbraceline.group(0)
|
---|
145 | braceLevel += 1
|
---|
146 | if braceLevel == 1: # First level
|
---|
147 | fatherRes = {}
|
---|
148 | fatherRes['_ressourceName'] = openbraceline.group(1)
|
---|
149 | dirRes.append(fatherRes)
|
---|
150 | treeindex[braceLevel] = fatherRes
|
---|
151 | else:
|
---|
152 | sonRes = {}; #
|
---|
153 | sonRes['_ressourceName'] = openbraceline.group(1)
|
---|
154 | fatherRes[openbraceline.group(1)] = sonRes;
|
---|
155 | #print "creating new sonres for", openbraceline.group(1),"in" , fatherRes['_ressourceName']
|
---|
156 | #treeindex[braceLevel]=fatherRes;
|
---|
157 | #tmpRes = fatherRes;
|
---|
158 | fatherRes = sonRes;
|
---|
159 | treeindex[braceLevel]=fatherRes;
|
---|
160 | elif closebraceline:
|
---|
161 | braceLevel -= 1
|
---|
162 | fatherRes = treeindex[braceLevel]
|
---|
163 |
|
---|
164 | elif m:
|
---|
165 | if m.group(1) in multiRes: # create a new list for multiple Entries
|
---|
166 | try:
|
---|
167 | fatherRes[m.group(1)].append(m.group(2))
|
---|
168 | except:
|
---|
169 | #if ( fatherRes[m.group(1)] != None): # add a listentry if not already there
|
---|
170 | multilist = []
|
---|
171 | asd multilist.append(m.group(2))
|
---|
172 | fatherRes[m.group(1)] = multilist
|
---|
173 | else:
|
---|
174 | try:
|
---|
175 | if fatherRes[m.group(1)]:
|
---|
176 | print "Warning: overwriting "+ m.group(1)
|
---|
177 | except:
|
---|
178 | fatherRes[m.group(1)] = m.group(2)
|
---|
179 | #print braceLevel,m.group(2)
|
---|
180 |
|
---|
181 |
|
---|
182 | myNewResDict={}
|
---|
183 |
|
---|
184 | #mybaculaRessource = baculaRessource("myRessource",{'name':'bacula-dir', 'Test':'test'})
|
---|
185 | #print mybaculaRessource
|
---|
186 |
|
---|
187 |
|
---|
188 | for tmpdict in dirRes:
|
---|
189 | #print tmpdict['_ressourceName']
|
---|
190 |
|
---|
191 | try:
|
---|
192 | myNewResDict[tmpdict['_ressourceName']].append(tmpdict)
|
---|
193 | except:
|
---|
194 | myNewResDict[tmpdict['_ressourceName']]=[]
|
---|
195 | myNewResDict[tmpdict['_ressourceName']].append(tmpdict)
|
---|
196 | myBacRes = baculaRessource(tmpdict['_ressourceName'],tmpdict)
|
---|
197 | print myBacRes
|
---|
198 |
|
---|
199 |
|
---|