Changeset 799 for vanHelsing/trunk/src/bcfg.py
- Timestamp:
- Oct 15, 2009, 6:37:56 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vanHelsing/trunk/src/bcfg.py
r798 r799 11 11 RESOURCE_TYPES = ('dird', 'console', 'filed', 'stored') 12 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 } 13 rxp_item = re.compile('^\\s*(\\w[\\w ]*\\w+)\\s*\\=(.*)') # xxx = xxx matchen 14 rxp_item2 = re.compile('^\\s*(\\w[\\w ]*\\w+)\\s*\\=\\s*{(.*)}\\s*') # match key = { a = 1; b= 2} 15 rxp_openbrace = re.compile('^\\s*(\\w[\\w ]*\\w+)\\s*\\=?\\s*\\{\\s*') # match xxx { 16 rxp_closebrace = re.compile('[^{]*}.*') # match } 16 17 rxp_comment = re.compile('^\s*#.*') 17 18 … … 26 27 self.filename = filename 27 28 self.resources = resource.Resource(level=0) 29 self.resources.name="<root>" 28 30 29 31 def read(self): 30 print "reading %s" % self.filename 31 self.parse() 32 print self.resources 32 self.parse(self.resources,open(self.filename,"r"),0) 33 #print self.resources 33 34 34 35 35 def write(self): 36 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()) 37 38 def getResourceById(self,theid): 39 return self.resources.getById(theid) 40 41 def getResourceByName(self,name): 42 return self.resources.getByName(name) 43 44 def getRoot(self): 45 return self.resources 46 47 def parse(self,curRes,f,level): 48 #print "START",curRes.name 49 50 while True: 51 line=f.readline() 52 if not line: 53 break 54 55 inlinecomment=None 56 57 commentStart=line.find("#") 58 if commentStart!=-1: 59 inlinecomment=line[commentStart:] 60 line=line[:commentStart].strip() 61 #curRes.add_comment(inlinecomment) 62 63 if rxp_closebrace.match(line): 64 #print "closebraceline" 65 break 66 67 item2 = rxp_item2.match(line) 68 if item2: 69 #print "item2" 70 name = item2.group(1) 71 value = item2.group(2) 72 #print "item:",name,value 73 newRes=resource.Resource(level+1) 74 newRes.name=name 75 newRes.value="{"+value+"}" 76 curRes.add_item(newRes) 55 77 continue 56 print braceLevel 78 57 79 openbraceline = rxp_openbrace.match(line) 58 closebraceline = rxp_closebrace.match(line) 80 if openbraceline: 81 #print "openbraceline" 82 resname = openbraceline.group(1) 83 try: 84 resClass = getattr(resource,resname); 85 except: 86 resClass = resource.Resource 87 88 newRes=resClass(level+1) 89 newRes.name=resname 90 curRes.add_item(newRes); 91 self.parse(newRes,f,level+1); 92 93 continue 94 59 95 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 96 if item: 97 name = item.group(1) 98 value = item.group(2) 99 #print "item:",name,value 100 newRes=resource.Resource(level+1) 101 newRes.name=name 102 newRes.value=value 103 curRes.add_item(newRes) 104 continue 67 105 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 106 #print "END",curRes.name 82 107 83 108 class DirdConfig(Config): … … 97 122 if __name__ == "__main__": 98 123 99 dirdcfg = DirdConfig(" ../conf/bacula-dir.conf")124 dirdcfg = DirdConfig("test.conf") 100 125 dirdcfg.read() 101 126 #print dirdcfg.getResourceById(174) 127 jobs=dirdcfg.getResourceByName("Job") 128 for j in jobs: 129 print j.getByName("Name")[0].value 130 d=dirdcfg.getResourceByName("ip")[1] 131 print d 132 133 job=resource.Job(1,"Job") 134 job.add("Name",'"test2"') 135 job.add("Client",'"test2"') 136 job.add("JobDefs",'"testdefs"') 137 job.add("FileSet",'"Full Set"') 138 139 root=dirdcfg.getRoot() 140 root.add_item(job) 141 142 print root 102 143 sys.exit(0)
Note:
See TracChangeset
for help on using the changeset viewer.