[798] | 1 | #!/usr/bin/python
|
---|
[775] | 2 | # -*- coding: utf-8 -*-
|
---|
| 3 | """Classes and functions for configuration file handling
|
---|
| 4 | """
|
---|
| 5 |
|
---|
| 6 | import sys
|
---|
| 7 | import re
|
---|
[798] | 8 | import resource
|
---|
| 9 | import directive
|
---|
[775] | 10 |
|
---|
| 11 | RESOURCE_TYPES = ('dird', 'console', 'filed', 'stored')
|
---|
| 12 |
|
---|
[799] | 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 }
|
---|
[798] | 17 | rxp_comment = re.compile('^\s*#.*')
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 |
|
---|
[775] | 21 | #'dird', 'console', 'filed' or 'stored'
|
---|
[798] | 22 | class 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
|
---|
[812] | 28 | self.resources = resource.Root(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 |
|
---|
[812] | 35 | def write(self, filename=None):
|
---|
| 36 | if(filename==None):
|
---|
| 37 | filename=self.filename
|
---|
| 38 | f=open(filename,"w")
|
---|
| 39 | f.write(str(self.getRoot()))
|
---|
| 40 | f.close()
|
---|
[799] | 41 |
|
---|
| 42 | def getResourceById(self,theid):
|
---|
| 43 | return self.resources.getById(theid)
|
---|
| 44 |
|
---|
| 45 | def getResourceByName(self,name):
|
---|
| 46 | return self.resources.getByName(name)
|
---|
| 47 |
|
---|
| 48 | def getRoot(self):
|
---|
| 49 | return self.resources
|
---|
| 50 |
|
---|
| 51 | def parse(self,curRes,f,level):
|
---|
| 52 | #print "START",curRes.name
|
---|
| 53 |
|
---|
| 54 | while True:
|
---|
| 55 | line=f.readline()
|
---|
| 56 | if not line:
|
---|
| 57 | break
|
---|
| 58 |
|
---|
| 59 | inlinecomment=None
|
---|
| 60 |
|
---|
| 61 | commentStart=line.find("#")
|
---|
| 62 | if commentStart!=-1:
|
---|
| 63 | inlinecomment=line[commentStart:]
|
---|
| 64 | line=line[:commentStart].strip()
|
---|
| 65 | #curRes.add_comment(inlinecomment)
|
---|
| 66 |
|
---|
[812] | 67 | if len(line)>0 and line[0]=='@':
|
---|
| 68 | includepath=line[1:].strip()
|
---|
| 69 | print "include " +includepath
|
---|
| 70 | self.parse(self.resources,open(includepath,"r"),0)
|
---|
| 71 |
|
---|
[799] | 72 | if rxp_closebrace.match(line):
|
---|
| 73 | #print "closebraceline"
|
---|
| 74 | break
|
---|
| 75 |
|
---|
| 76 | item2 = rxp_item2.match(line)
|
---|
| 77 | if item2:
|
---|
| 78 | #print "item2"
|
---|
| 79 | name = item2.group(1)
|
---|
| 80 | value = item2.group(2)
|
---|
| 81 | #print "item:",name,value
|
---|
| 82 | newRes=resource.Resource(level+1)
|
---|
| 83 | newRes.name=name
|
---|
| 84 | newRes.value="{"+value+"}"
|
---|
| 85 | curRes.add_item(newRes)
|
---|
[798] | 86 | continue
|
---|
[799] | 87 |
|
---|
[798] | 88 | openbraceline = rxp_openbrace.match(line)
|
---|
| 89 | if openbraceline:
|
---|
[799] | 90 | #print "openbraceline"
|
---|
| 91 | resname = openbraceline.group(1)
|
---|
| 92 | try:
|
---|
| 93 | resClass = getattr(resource,resname);
|
---|
| 94 | except:
|
---|
| 95 | resClass = resource.Resource
|
---|
[798] | 96 |
|
---|
[799] | 97 | newRes=resClass(level+1)
|
---|
| 98 | newRes.name=resname
|
---|
| 99 | curRes.add_item(newRes);
|
---|
| 100 | self.parse(newRes,f,level+1);
|
---|
| 101 |
|
---|
| 102 | continue
|
---|
| 103 |
|
---|
| 104 | item = rxp_item.match(line)
|
---|
| 105 | if item:
|
---|
| 106 | name = item.group(1)
|
---|
[812] | 107 | value = item.group(2).strip()
|
---|
[799] | 108 | #print "item:",name,value
|
---|
| 109 | newRes=resource.Resource(level+1)
|
---|
| 110 | newRes.name=name
|
---|
| 111 | newRes.value=value
|
---|
[798] | 112 | curRes.add_item(newRes)
|
---|
[799] | 113 | continue
|
---|
| 114 |
|
---|
| 115 | #print "END",curRes.name
|
---|
[798] | 116 |
|
---|
[775] | 117 | class DirdConfig(Config):
|
---|
[798] | 118 |
|
---|
| 119 | def __init__(self, filename=""):
|
---|
| 120 | Config.__init__(self, 'dird', filename)
|
---|
| 121 |
|
---|
[775] | 122 | class ConsoleConfig(Config):
|
---|
| 123 | pass
|
---|
| 124 |
|
---|
| 125 | class FiledConfig(Config):
|
---|
| 126 | pass
|
---|
| 127 |
|
---|
| 128 | class StoredConfig(Config):
|
---|
| 129 | pass
|
---|
| 130 |
|
---|
[812] | 131 | def createClient(clientname,clientaddr,catalog,password):
|
---|
| 132 | newclient=resource.Client(1,"Client")
|
---|
| 133 | newclient.add("Name",clientname)
|
---|
| 134 | newclient.add("Address",clientaddr)
|
---|
| 135 | #newclient.add("FDPort",fdport)
|
---|
| 136 | newclient.add("Catalog",catalog)
|
---|
| 137 | newclient.add("Password",password)
|
---|
| 138 | return newclient
|
---|
| 139 |
|
---|
[798] | 140 | if __name__ == "__main__":
|
---|
[775] | 141 |
|
---|
[799] | 142 | dirdcfg = DirdConfig("test.conf")
|
---|
[798] | 143 | dirdcfg.read()
|
---|
[799] | 144 |
|
---|
| 145 | job=resource.Job(1,"Job")
|
---|
| 146 | job.add("Name",'"test2"')
|
---|
| 147 | job.add("Client",'"test2"')
|
---|
| 148 | job.add("JobDefs",'"testdefs"')
|
---|
| 149 | job.add("FileSet",'"Full Set"')
|
---|
| 150 |
|
---|
| 151 | root=dirdcfg.getRoot()
|
---|
| 152 | root.add_item(job)
|
---|
| 153 |
|
---|
[812] | 154 | newclient=createClient("testclient-fd","testclient","MyCatalog","testpassword")
|
---|
| 155 | root.add_item(newclient)
|
---|
| 156 |
|
---|
| 157 | dirdcfg.write("test.conf.out")
|
---|
[798] | 158 | sys.exit(0)
|
---|