source: branches/0.4/parse_ganglia.py @ 695

Last change on this file since 695 was 695, checked in by ramonb, 11 years ago
  • updated proof of concept GangliaConfigParser?
  • added dictionary creation of config
  • fix to comment checking
  • Property svn:executable set to *
File size: 5.4 KB
Line 
1#!/usr/bin/env python
2
3import shlex, sys, pprint
4from glob import glob
5
6class GangliaConfigParser:
7
8    def __init__( self, filename ):
9
10        self.conf_lijst   = [ ]
11        self.conf_dict    = { }
12        self.filename     = filename
13        self.file_pointer = file( filename, 'r' )
14        self.lexx         = shlex.shlex( self.file_pointer )
15        self.lexx.whitespace_split = True
16
17        self.parse()
18
19
20    def __del__( self ):
21
22        self.file_pointer.close()
23        del self.lexx
24        del self.conf_lijst
25
26    def removeQuotes( self, value ):
27
28        clean_value = value
29        clean_value = clean_value.replace( "'", "" )
30        clean_value = clean_value.replace( '"', '' )
31        clean_value = clean_value.strip()
32   
33        return clean_value
34
35    def removeBraces( self, value ):
36
37        clean_value = value
38        clean_value = clean_value.replace( "(", "" )
39        clean_value = clean_value.replace( ')', '' )
40        clean_value = clean_value.strip()
41   
42        return clean_value
43
44    def parse( self ):
45
46        t = 'bogus'
47        c = False
48        i = False
49
50        while t != self.lexx.eof:
51            #print 'get token'
52            t = self.lexx.get_token()
53
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
75
76            if c:
77                print 'skipping: %s' %t
78                continue
79
80            if t == 'include':
81                i = True
82                print 'include start'
83                print 'skipping: %s' %t
84                continue
85
86            if i:
87
88                print 'include start: %s' %t
89
90                t2 = self.removeQuotes( t )
91                t2 = self.removeBraces( t )
92
93                for in_file in glob( self.removeQuotes(t2) ):
94
95                    print 'including file: %s' %in_file
96                    parse_infile = GangliaConfigParser( in_file )
97
98                    self.conf_lijst = self.conf_lijst + parse_infile.getConfLijst()
99
100                    del parse_infile
101
102                i = False
103                print 'include end'
104                print 'skipping: %s' %t
105                continue
106
107            print 'keep: %s' %t
108            self.conf_lijst.append( t )
109
110    def getConfLijst( self ):
111
112        return self.conf_lijst
113
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
205GMOND_LOCATION = '/etc/ganglia/gmond.conf'
206
207g = GangliaConfigParser( GMOND_LOCATION )
208
209pprint.pprint( g.getConfLijst(), width=1 )
210
211g.makeConfDict()
212
213pprint.pprint( g.getConfDict(), width=1 )
214
215print 'exiting..'
216sys.exit(0)
Note: See TracBrowser for help on using the repository browser.