Changeset 798
- Timestamp:
- Oct 14, 2009, 4:03:57 PM (15 years ago)
- Location:
- vanHelsing/trunk/src
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
vanHelsing/trunk/src/bcfg.py
r775 r798 1 #!/usr/bin/python 1 2 # -*- coding: utf-8 -*- 2 3 """Classes and functions for configuration file handling … … 5 6 import sys 6 7 import re 8 import resource 9 import directive 7 10 8 11 RESOURCE_TYPES = ('dird', 'console', 'filed', 'stored') 9 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 10 20 #'dird', 'console', 'filed' or 'stored' 11 class Config( Object):21 class Config(object): 12 22 """Class for bacula configuration access""" 13 23 … … 15 25 self.resource_type = resource_type 16 26 self.filename = filename 27 self.resources = resource.Resource(level=0) 17 28 18 29 def read(self): 19 pass 30 print "reading %s" % self.filename 31 self.parse() 32 print self.resources 33 20 34 21 35 def write(self): 22 36 pass 23 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 24 83 class DirdConfig(Config): 25 pass 26 84 85 def __init__(self, filename=""): 86 Config.__init__(self, 'dird', filename) 87 27 88 class ConsoleConfig(Config): 28 89 pass … … 34 95 pass 35 96 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 97 if __name__ == "__main__": 84 98 85 99 dirdcfg = DirdConfig("../conf/bacula-dir.conf") 100 dirdcfg.read() 86 101 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 102 sys.exit(0) -
vanHelsing/trunk/src/resource.py
r775 r798 7 7 8 8 class Resource(object): 9 DIRECTIVE="" 9 10 10 def __init__(self ):11 def __init__(self, level=0): 11 12 self.items = [] 12 13 self.comments = [] 14 self._recurselevel = level 13 15 14 16 def __str__(self): 15 17 s = "\n".join(self.comments) 16 s += self. __name__+ "{\n"18 s += self.DIRECTIVE + "{\n" 17 19 for d in self.items: 18 20 s += " " + str(d) + "\n" … … 24 26 comment = "# " + comment 25 27 self.comments.append(comment) 28 29 def add_item(self, item): 30 self.items.append(item) 26 31 27 32 class Director(Resource): 33 DIRECTIVE="Director" 28 34 pass 29 35 … … 54 60 55 61 class Messages(Resource): 56 DIRECTIVE=" "62 DIRECTIVE="Messages" 57 63 58 64 class Console(Resource): 59 DIRECTIVE=" "65 DIRECTIVE="Console" 60 66 61 67 class Counter(Resource): 62 DIRECTIVE=" "68 DIRECTIVE="Counter" 63 69 64 70 class Device(Resource): 65 DIRECTIVE=" "71 DIRECTIVE="Device" 66 72 67 73 class Autochanger(Resource): 68 DIRECTIVE="" 74 DIRECTIVE="Autochanger" 75 76 __cls = locals().copy() 77 78 directives = [v.DIRECTIVE for k,v in __cls.iteritems() 79 if k != "Resource" and getattr(v, "DIRECTIVE", None)]
Note:
See TracChangeset
for help on using the changeset viewer.