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

Last change on this file since 808 was 799, checked in by slederer, on Oct 15, 2009 at 6:37:56 PM

Änderungen am Parser, so dass er funktioniert

  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-python
File size: 3.8 KB
RevLine 
[798]1#!/usr/bin/python
[775]2# -*- coding: utf-8 -*-
3"""Classes and functions for configuration file handling
4"""
5
6import sys
7import re
[798]8import resource
9import directive
[775]10
11RESOURCE_TYPES = ('dird', 'console', 'filed', 'stored')
12
[799]13rxp_item = re.compile('^\\s*(\\w[\\w ]*\\w+)\\s*\\=(.*)') # xxx = xxx matchen
14rxp_item2 = re.compile('^\\s*(\\w[\\w ]*\\w+)\\s*\\=\\s*{(.*)}\\s*') # match key = { a = 1; b= 2}
15rxp_openbrace = re.compile('^\\s*(\\w[\\w ]*\\w+)\\s*\\=?\\s*\\{\\s*') # match xxx {
16rxp_closebrace = re.compile('[^{]*}.*') # match }
[798]17rxp_comment = re.compile('^\s*#.*')
18
19
20
[775]21#'dird', 'console', 'filed' or 'stored'
[798]22class Config(object):
[775]23 """Class for bacula configuration access"""
24
25 def __init__(self, resource_type, filename=""):
26 self.resource_type = resource_type
27 self.filename = filename
[798]28 self.resources = resource.Resource(level=0)
[799]29 self.resources.name="<root>"
[775]30
31 def read(self):
[799]32 self.parse(self.resources,open(self.filename,"r"),0)
33 #print self.resources
[798]34
[775]35 def write(self):
36 pass
[799]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)
[798]77 continue
[799]78
[798]79 openbraceline = rxp_openbrace.match(line)
80 if openbraceline:
[799]81 #print "openbraceline"
82 resname = openbraceline.group(1)
83 try:
84 resClass = getattr(resource,resname);
85 except:
86 resClass = resource.Resource
[798]87
[799]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
95 item = rxp_item.match(line)
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
[798]103 curRes.add_item(newRes)
[799]104 continue
105
106 #print "END",curRes.name
[798]107
[775]108class DirdConfig(Config):
[798]109
110 def __init__(self, filename=""):
111 Config.__init__(self, 'dird', filename)
112
[775]113class ConsoleConfig(Config):
114 pass
115
116class FiledConfig(Config):
117 pass
118
119class StoredConfig(Config):
120 pass
121
[798]122if __name__ == "__main__":
[775]123
[799]124 dirdcfg = DirdConfig("test.conf")
[798]125 dirdcfg.read()
[799]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
[798]143 sys.exit(0)
Note: See TracBrowser for help on using the repository browser.