Changeset 695 for branches


Ignore:
Timestamp:
03/21/13 12:31:54 (11 years ago)
Author:
ramonb
Message:
  • updated proof of concept GangliaConfigParser?
  • added dictionary creation of config
  • fix to comment checking
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/0.4/parse_ganglia.py

    r693 r695  
    11#!/usr/bin/env python
    22
    3 import shlex, sys
     3import shlex, sys, pprint
    44from glob import glob
    55
     
    99
    1010        self.conf_lijst   = [ ]
     11        self.conf_dict    = { }
    1112        self.filename     = filename
    1213        self.file_pointer = file( filename, 'r' )
     
    1516
    1617        self.parse()
     18
    1719
    1820    def __del__( self ):
     
    4345
    4446        t = 'bogus'
    45         c= False
    46         i= False
     47        c = False
     48        i = False
    4749
    4850        while t != self.lexx.eof:
     
    5052            t = self.lexx.get_token()
    5153
    52             if t == '/*':
    53                 c = True
    54                 print 'comment start'
    55                 print 'skipping: %s' %t
    56                 continue
    57 
    58             if t == '*/':
    59                 c = False
    60                 print 'skipping: %s' %t
    61                 print 'comment end'
    62                 continue
     54            if len( t ) >= 2:
     55
     56                if len( t ) >= 4:
     57
     58                    if t[:2] == '/*' and t[-2:] == '*/':
     59
     60                        print 'comment line'
     61                        print 'skipping: %s' %t
     62                        continue
     63
     64                if t == '/*' or t[:2] == '/*':
     65                    c = True
     66                    print 'comment start'
     67                    print 'skipping: %s' %t
     68                    continue
     69
     70                if t == '*/' or t[-2:] == '*/':
     71                    c = False
     72                    print 'skipping: %s' %t
     73                    print 'comment end'
     74                    continue
    6375
    6476            if c:
     
    100112        return self.conf_lijst
    101113
     114    def confListToDict( self, parent_list=None ):
     115
     116        new_dict = { }
     117        count    = 0
     118        skip     = 0
     119
     120        if not parent_list:
     121            parent_list = self.conf_lijst
     122
     123        print 'entering confListToDict(): (parent) list size %s' %len(parent_list)
     124
     125        for n, c in enumerate( parent_list ):
     126
     127            count = count + 1
     128
     129            print 'CL: n %d c %s' %(n, c)
     130
     131            if skip > 0:
     132
     133                #print '- skipped'
     134                skip = skip - 1
     135                continue
     136
     137            if (n+1) <= (len( parent_list )-1):
     138
     139                if parent_list[(n+1)] == '{':
     140
     141                    if not new_dict.has_key( c ):
     142                        new_dict[ c ] = [ ]
     143
     144                    (temp_new_dict, skip) = self.confListToDict( parent_list[(n+2):] )
     145                    new_dict[ c ].append( temp_new_dict )
     146
     147                if parent_list[(n+1)] == '=' and (n+2) <= (len( parent_list )-1):
     148
     149                    if not new_dict.has_key( c ):
     150                        new_dict[ c ] = [ ]
     151
     152                    new_dict[ c ].append( parent_list[ (n+2) ] )
     153
     154                    skip = 2
     155
     156                if parent_list[n] == '}':
     157
     158                    #print 'parent_list = %s' %parent_list
     159                    print 'leaving confListToDict(): new dict = %s' %new_dict
     160                    return (new_dict, count)
     161
     162    def getConfDict( self ):
     163
     164        return self.conf_dict
     165
     166    def makeConfDict( self ):
     167
     168        new_dict = { }
     169        skip     = 0
     170
     171        print 'entering makeConfDict()'
     172
     173        for n, c in enumerate( self.conf_lijst ):
     174
     175            print 'M: n %d c %s' %(n, c)
     176
     177            if skip > 0:
     178
     179                print '- skipped'
     180                skip = skip - 1
     181                continue
     182
     183            if (n+1) <= (len( self.conf_lijst )-1):
     184
     185                if self.conf_lijst[(n+1)] == '{':
     186
     187                    if not new_dict.has_key( c ):
     188                        new_dict[ c ] = [ ]
     189
     190                    ( temp_new_dict, skip ) = self.confListToDict( self.conf_lijst[(n+2):] )
     191                    new_dict[ c ].append( temp_new_dict )
     192
     193                if self.conf_lijst[(n+1)] == '=' and (n+2) <= (len( self.conf_lijst )-1):
     194
     195                    if not new_dict.has_key( c ):
     196                        new_dict[ c ] = [ ]
     197
     198                    new_dict[ c ].append( self.conf_lijst[ (n+2) ] )
     199
     200                    skip = 2
     201
     202        self.conf_dict = new_dict
     203        print 'leaving makeConfDict(): conf dict size %d' %len( self.conf_dict )
     204
    102205GMOND_LOCATION = '/etc/ganglia/gmond.conf'
    103206
    104207g = GangliaConfigParser( GMOND_LOCATION )
    105208
    106 print g.getConfLijst()
     209pprint.pprint( g.getConfLijst(), width=1 )
     210
     211g.makeConfDict()
     212
     213pprint.pprint( g.getConfDict(), width=1 )
    107214
    108215print 'exiting..'
Note: See TracChangeset for help on using the changeset viewer.