Changeset 799 for vanHelsing/trunk/src
- Timestamp:
- Oct 15, 2009, 6:37:56 PM (15 years ago)
- Location:
- vanHelsing/trunk/src
- Files:
-
- 3 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) -
vanHelsing/trunk/src/directive.py
r775 r799 6 6 @author: hmueller 7 7 ''' 8 9 from resource import Resource 8 10 9 11 PrettyNames = {} … … 30 32 # 31 33 32 class Item( object):34 class Item(Resource): 33 35 def __init__(self, name, default=None, type=None, required=False): 34 36 self.name = name … … 50 52 return self.name < other.name 51 53 52 def __str__(self):53 if PrettyNames.has_key(self.name):54 return '%s = "%s"' % (self.prettyName(), str(self.value))55 else:56 return '%s = "%s" # Warning: No pretty name' % (57 self.name, str(self.value))58 54 def prettyName(self): 59 55 return PrettyNames.get(self.name, self.name) -
vanHelsing/trunk/src/resource.py
r798 r799 6 6 ''' 7 7 8 9 8 10 class Resource(object): 9 DIRECTIVE="" 11 DIRECTIVE="Resource" 12 serial=0 10 13 11 def __init__(self, level=0): 14 def __init__(self, level=0,name=None): 15 Resource.serial=Resource.serial+1 12 16 self.items = [] 13 17 self.comments = [] 14 18 self._recurselevel = level 15 19 self.name = name 20 self.value = None 21 self.id=Resource.serial 22 16 23 def __str__(self): 17 24 s = "\n".join(self.comments) 18 s += self.DIRECTIVE + "{\n" 25 indent=" "*self._recurselevel 26 s += indent+"# id:"+str(self.id)+"\n" 27 28 if len(self.items)==0: 29 return indent+self.name+" = "+self.value+"\n"; 30 31 s += indent+self.name + " {\n" 19 32 for d in self.items: 20 s += " " + str(d) + "\n"21 s += "}\n"33 s += str(d) 34 s += indent+"}\n" 22 35 return s 23 36 … … 29 42 def add_item(self, item): 30 43 self.items.append(item) 44 45 def add(self,key,value): 46 newRes=Resource() 47 newRes.name=key 48 newRes.value=value 49 newRes._recurselevel=self._recurselevel+1 50 self.items.append(newRes) 51 52 def getById(self,theid): 53 if self.id==theid: 54 return self 55 56 for r in self.items: 57 result=r.getById(theid) 58 if result: 59 return result 60 return None 61 62 def getByName(self,name): 63 result=[] 64 if self.name==name: 65 result.append(self) 66 for i in self.items: 67 r=i.getByName(name) 68 if len(r)!=0: 69 for a in r: 70 result.append(a) 71 return result 72 31 73 32 74 class Director(Resource):
Note:
See TracChangeset
for help on using the changeset viewer.