Changeset 798 for vanHelsing/trunk


Ignore:
Timestamp:
Oct 14, 2009, 4:03:57 PM (15 years ago)
Author:
hmueller
Message:

parser in class gepackt

Location:
vanHelsing/trunk/src
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • vanHelsing/trunk/src/bcfg.py

    r775 r798  
     1#!/usr/bin/python
    12# -*- coding: utf-8 -*-
    23"""Classes and functions for configuration file handling
     
    56import sys
    67import re
     8import resource
     9import directive
    710
    811RESOURCE_TYPES = ('dird', 'console', 'filed', 'stored')
    912
     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
    1020#'dird', 'console', 'filed' or 'stored'
    11 class Config(Object):
     21class Config(object):
    1222    """Class for bacula configuration access"""
    1323   
     
    1525        self.resource_type = resource_type
    1626        self.filename = filename
     27        self.resources = resource.Resource(level=0)
    1728
    1829    def read(self):
    19         pass
     30        print "reading %s" % self.filename
     31        self.parse()
     32        print self.resources
     33       
    2034   
    2135    def write(self):
    2236        pass
    2337   
     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   
    2483class DirdConfig(Config):
    25     pass
    26 
     84   
     85    def __init__(self, filename=""):
     86        Config.__init__(self, 'dird', filename)
     87       
    2788class ConsoleConfig(Config):
    2889    pass
     
    3495    pass
    3596
    36 class baculaRessource:
    37     """ generic bacula support Type     
    38     """
    39     name = None;
    40     resourceDirectives = {}
    41     mandatoryDirectives = {} # which directives must be configured?
    42      
    43     def printdict(self,mydict,indent=''):
    44         ressource = 'unknown'
    45         #indent += '  '       
    46         if type(mydict) == dict :   
    47             if '_ressourceName' in mydict:
    48                 v = mydict['_ressourceName']
    49                 ressource =  v
    50                 print indent + v + " {"
    51                 indent += '  '
    52            
    53             for p,v in mydict.iteritems():
    54                 #print "v: " ,  v
    55                 if type(v) == str:                          # leaf
    56                     if p == '_ressourceName':
    57                         continue
    58                     print indent + p + " = " +  v
    59                    
    60                 elif type(v) == dict :
    61                     #print indent + p + " {"
    62                     #ressource = p
    63                     self.printdict(v,indent)
    64                    
    65                 elif type(v) ==  list :
    66                     #ressource = "list"
    67                     for i in v:
    68                         print indent + p + " = " + i
    69                        
    70         if type(mydict) == list :
    71             for i in mydict:
    72          
    73                 if i == '_ressourceName':
    74                     Name = i['Name']
    75                     print indent + Name
    76                     self.printdict(i,indent)
    77                 else:                                    # leaf
    78                     print indent + i
    79 
    80         if type(mydict) == str:
    81             print indent + "direct" + mydict
    82         indent = indent[:-2]   
    83         print indent + "} # " + ressource
     97if __name__ == "__main__":
    8498   
    85 
     99    dirdcfg = DirdConfig("../conf/bacula-dir.conf")
     100    dirdcfg.read()
    86101   
    87     def __init__(self, name, resourceDirectives):
    88         #resourceDirectives = {'name':'bacula-dir', 'Test':'test'}  # the configured directives
    89         self.name = name
    90         self.resourceDirectives =  resourceDirectives
    91 
    92        
    93        
    94     def __str__(self):                      # overload print function
    95         self.printdict(self.resourceDirectives,"")
    96         s  = ''
    97         #s = self.name + ' {\n'
    98         #for directive,value in self.resourceDirectives.iteritems() :
    99         #    s += ' ' +directive + ' = ' + value + '\n'
    100         #s += '}\n'
    101         return s
    102    
    103 
    104 mybaculaRessource = baculaRessource("myRessource",{'name':'bacula-dir', 'Test':'test'})
    105 
    106 
    107 print  mybaculaRessource
    108 
    109 multiRes = set(['Run','File','Ip']) # entries, which can be multiple times in a ressource
    110 
    111 f = open("bacula-dir.conf",'r')
    112 
    113 #p = re.compile('\s*([\S ]+)\s*=\s*(\S+)\s*') # xxx = xxx matchen
    114 p = re.compile('\s*([\S]+)\s*=\s*(.*)$') # xxx = xxx matchen
    115 #openbrace = re.compile('\s*([\S ]+)\s*{.*')    # match xxx {
    116 openbrace = re.compile('\s*([\S ]+)=?\s+{.*')    # match xxx {
    117 
    118 closebrace = re.compile('.*}.*')               # match }
    119 comment = re.compile('^\s*#.*')
    120 
    121 #myRes = {} ; # RessourcenDict erzeugen
    122 
    123 dirRes=[] # Liste der Director Ressourcen
    124 
    125 
    126 
    127 braceLevel = 0;
    128 #print p
    129 tmpRes = {}
    130 treeindex=range(10)
    131 
    132 for line in f:
    133         #print braceLevel,line
    134         commentline = comment.match(line)
    135         if commentline:
    136             #print "found commentline: " + commentline.group(0)
    137             continue
    138         #print braceLevel
    139         openbraceline = openbrace.match(line)
    140         closebraceline = closebrace.match(line)
    141         m = p.match(line)
    142        
    143         if openbraceline:
    144             #print "found openbraceline: " + openbraceline.group(0)
    145             braceLevel += 1
    146             if braceLevel == 1: # First level
    147                 fatherRes = {}
    148                 fatherRes['_ressourceName'] = openbraceline.group(1)
    149                 dirRes.append(fatherRes)
    150                 treeindex[braceLevel] = fatherRes
    151             else:
    152                 sonRes = {}; #
    153                 sonRes['_ressourceName'] = openbraceline.group(1)
    154                 fatherRes[openbraceline.group(1)] = sonRes;
    155                 #print "creating new sonres for", openbraceline.group(1),"in" , fatherRes['_ressourceName']
    156                 #treeindex[braceLevel]=fatherRes;
    157                 #tmpRes = fatherRes;
    158                 fatherRes = sonRes;
    159                 treeindex[braceLevel]=fatherRes;
    160         elif closebraceline:
    161             braceLevel -= 1
    162             fatherRes = treeindex[braceLevel]
    163            
    164         elif m:
    165                 if m.group(1) in multiRes:                 # create a new list for multiple Entries
    166                     try:
    167                         fatherRes[m.group(1)].append(m.group(2))
    168                     except:
    169                     #if  ( fatherRes[m.group(1)] != None):           # add a listentry if not already there
    170                         multilist = []
    171     asd                    multilist.append(m.group(2))
    172                         fatherRes[m.group(1)] = multilist
    173                 else:
    174                     try:
    175                         if fatherRes[m.group(1)]:
    176                             print "Warning: overwriting "+ m.group(1)
    177                     except:
    178                         fatherRes[m.group(1)] = m.group(2)
    179                 #print braceLevel,m.group(2)
    180 
    181 
    182 myNewResDict={}
    183 
    184 #mybaculaRessource = baculaRessource("myRessource",{'name':'bacula-dir', 'Test':'test'})
    185 #print  mybaculaRessource
    186 
    187 
    188 for tmpdict in dirRes:
    189     #print tmpdict['_ressourceName']
    190    
    191     try:
    192         myNewResDict[tmpdict['_ressourceName']].append(tmpdict)
    193     except:
    194         myNewResDict[tmpdict['_ressourceName']]=[]
    195         myNewResDict[tmpdict['_ressourceName']].append(tmpdict)
    196     myBacRes = baculaRessource(tmpdict['_ressourceName'],tmpdict)
    197     print myBacRes
    198 
    199 
     102    sys.exit(0)
  • vanHelsing/trunk/src/resource.py

    r775 r798  
    77
    88class Resource(object):
     9    DIRECTIVE=""
    910
    10     def __init__(self):
     11    def __init__(self, level=0):
    1112        self.items = []
    1213        self.comments = []
     14        self._recurselevel = level
    1315   
    1416    def __str__(self):
    1517        s = "\n".join(self.comments)
    16         s += self.__name__ + "{\n"
     18        s += self.DIRECTIVE + "{\n"
    1719        for d in self.items:
    1820            s += "  " + str(d) + "\n"
     
    2426            comment = "# " + comment
    2527        self.comments.append(comment)
     28       
     29    def add_item(self, item):
     30        self.items.append(item)
    2631
    2732class Director(Resource):
     33    DIRECTIVE="Director"
    2834    pass
    2935
     
    5460
    5561class Messages(Resource):
    56     DIRECTIVE=""
     62    DIRECTIVE="Messages"
    5763
    5864class Console(Resource):
    59     DIRECTIVE=""
     65    DIRECTIVE="Console"
    6066
    6167class Counter(Resource):
    62     DIRECTIVE=""
     68    DIRECTIVE="Counter"
    6369
    6470class Device(Resource):
    65     DIRECTIVE=""
     71    DIRECTIVE="Device"
    6672
    6773class Autochanger(Resource):
    68     DIRECTIVE=""
     74    DIRECTIVE="Autochanger"
     75
     76__cls = locals().copy()
     77
     78directives = [v.DIRECTIVE for k,v in __cls.iteritems()
     79              if k != "Resource" and getattr(v, "DIRECTIVE", None)]
Note: See TracChangeset for help on using the changeset viewer.