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

Last change on this file since 798 was 798, checked in by hmueller, on Oct 14, 2009 at 4:03:57 PM

parser in class gepackt

  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-python
File size: 2.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*(?P<Name>[\S]+)\s*=\s*(?P<Value>.*)$') # xxx = xxx matchen
14rxp_openbrace = re.compile('\s*(?P<Name>[\S ]+)=?\s+{.*') # match xxx {
15rxp_closebrace = re.compile('.*}.*') # match }
16rxp_comment = re.compile('^\s*#.*')
17
18
19
20#'dird', 'console', 'filed' or 'stored'
21class Config(object):
22 """Class for bacula configuration access"""
23
24 def __init__(self, resource_type, filename=""):
25 self.resource_type = resource_type
26 self.filename = filename
27 self.resources = resource.Resource(level=0)
28
29 def read(self):
30 print "reading %s" % self.filename
31 self.parse()
32 print self.resources
33
34
35 def write(self):
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())
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
83class DirdConfig(Config):
84
85 def __init__(self, filename=""):
86 Config.__init__(self, 'dird', filename)
87
88class ConsoleConfig(Config):
89 pass
90
91class FiledConfig(Config):
92 pass
93
94class StoredConfig(Config):
95 pass
96
97if __name__ == "__main__":
98
99 dirdcfg = DirdConfig("../conf/bacula-dir.conf")
100 dirdcfg.read()
101
102 sys.exit(0)
Note: See TracBrowser for help on using the repository browser.