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

Last change on this file since 804 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
Line 
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3"""Classes and functions for configuration file handling
4"""
5
6import sys
7import re
8import resource
9import directive
10
11RESOURCE_TYPES = ('dird', 'console', 'filed', 'stored')
12
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 }
17rxp_comment = re.compile('^\s*#.*')
18
19
20
21#'dird', 'console', 'filed' or 'stored'
22class Config(object):
23 """Class for bacula configuration access"""
24
25 def __init__(self, resource_type, filename=""):
26 self.resource_type = resource_type
27 self.filename = filename
28 self.resources = resource.Resource(level=0)
29 self.resources.name="<root>"
30
31 def read(self):
32 self.parse(self.resources,open(self.filename,"r"),0)
33 #print self.resources
34
35 def write(self):
36 pass
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)
77 continue
78
79 openbraceline = rxp_openbrace.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
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
103 curRes.add_item(newRes)
104 continue
105
106 #print "END",curRes.name
107
108class DirdConfig(Config):
109
110 def __init__(self, filename=""):
111 Config.__init__(self, 'dird', filename)
112
113class ConsoleConfig(Config):
114 pass
115
116class FiledConfig(Config):
117 pass
118
119class StoredConfig(Config):
120 pass
121
122if __name__ == "__main__":
123
124 dirdcfg = DirdConfig("test.conf")
125 dirdcfg.read()
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
143 sys.exit(0)
Note: See TracBrowser for help on using the repository browser.