source: vanHelsing/trunk/src/bcfg.py@ 791

Last change on this file since 791 was 775, checked in by hmueller, on Jul 6, 2009 at 3:21:47 PM
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-python
File size: 6.1 KB
RevLine 
[775]1# -*- coding: utf-8 -*-
2"""Classes and functions for configuration file handling
3"""
4
5import sys
6import re
7
8RESOURCE_TYPES = ('dird', 'console', 'filed', 'stored')
9
10#'dird', 'console', 'filed' or 'stored'
11class 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
24class DirdConfig(Config):
25 pass
26
27class ConsoleConfig(Config):
28 pass
29
30class FiledConfig(Config):
31 pass
32
33class StoredConfig(Config):
34 pass
35
36class 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
104mybaculaRessource = baculaRessource("myRessource",{'name':'bacula-dir', 'Test':'test'})
105
106
107print mybaculaRessource
108
109multiRes = set(['Run','File','Ip']) # entries, which can be multiple times in a ressource
110
111f = open("bacula-dir.conf",'r')
112
113#p = re.compile('\s*([\S ]+)\s*=\s*(\S+)\s*') # xxx = xxx matchen
114p = re.compile('\s*([\S]+)\s*=\s*(.*)$') # xxx = xxx matchen
115#openbrace = re.compile('\s*([\S ]+)\s*{.*') # match xxx {
116openbrace = re.compile('\s*([\S ]+)=?\s+{.*') # match xxx {
117
118closebrace = re.compile('.*}.*') # match }
119comment = re.compile('^\s*#.*')
120
121#myRes = {} ; # RessourcenDict erzeugen
122
123dirRes=[] # Liste der Director Ressourcen
124
125
126
127braceLevel = 0;
128#print p
129tmpRes = {}
130treeindex=range(10)
131
132for 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
182myNewResDict={}
183
184#mybaculaRessource = baculaRessource("myRessource",{'name':'bacula-dir', 'Test':'test'})
185#print mybaculaRessource
186
187
188for 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
Note: See TracBrowser for help on using the repository browser.