~ubuntu-branches/ubuntu/maverick/rapache/maverick

« back to all changes in this revision

Viewing changes to RapacheCore/ApacheConf.py

  • Committer: Bazaar Package Importer
  • Author(s): Emanuele Gentili
  • Date: 2008-10-15 15:44:27 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20081015154427-sc86e7urpacfwppr
Tags: 0.7-0ubuntu1
* rapache version bump (LP: #283770)
* debian/patches:
 + removed 01_surf_this_button.patch, fixed in bzr branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
"""
36
36
 
37
37
import re
38
 
from RapacheCore.Observer import PollyObserver
39
 
from RapacheCore.Observer import Observable
 
38
from Observer import PollyObserver
 
39
from Observer import Observable
40
40
 
41
41
class Parser (Observable):
42
42
    filename = None
54
54
        self.observer.register(self)
55
55
        
56
56
    def load(self, filename ):
 
57
        """Loads a configuration file in the parser"""
57
58
        self.filename = filename
58
59
        file = open ( filename, 'r' )
59
60
        self.content = file.readlines()
60
61
        file.close()
61
62
        return self.content
62
63
    
 
64
    def dump_values (self ):
 
65
        """Dumps every directive/value into a key/value dictionary. Newer 
 
66
        keys override older ones"""
 
67
        dict = {}
 
68
        for line in self.content:            
 
69
            key = self.parser.get_directive( line )
 
70
            if key:                
 
71
                try:
 
72
                    value = self.parser.get_value(line)
 
73
                    dict[ key ] = value
 
74
                except :                    
 
75
                    dict[ key ] = "#ERROR"
 
76
        return dict
 
77
    
63
78
    def get_value(self, name):
64
79
        line = self.get_directive(name)    
65
80
        if line == None: return None
76
91
            self.set_line( idx, line )
77
92
    def remove_value(self, name):
78
93
        idx = self._get_last_directive_idx(name)
79
 
        print "removing line:",idx
80
94
        if idx: 
81
95
            self.remove_line(idx)
82
96
            return True
115
129
        value = self.get_value(name)
116
130
        if value == None: return []
117
131
        options = self.parser.parse_options( value )
118
 
        print options
119
132
        return options
120
133
    def add_option (self, name, option ):    
121
134
        line = self.get_directive(name)
207
220
        if idx >= 0: idx = idx + self.min
208
221
        return self.father.set_line( idx, line )
209
222
    def insert_line (self, idx, line ):
210
 
        print "===========> INSERTING"
211
223
        if idx >= 0: idx = idx + self.min
212
224
        line = "\t"+line.lstrip() #ident
213
225
        return self.father.insert_line( idx, line )
225
237
    """Utility class. Contains methods to parse and manipulate apache conf
226
238
    directives"""
227
239
    def tokenize (self, line ):
228
 
        basic_regexp = '^(\s*)([A-Z0-9]+)(\s+)(.*)'  
 
240
        basic_regexp = '^(\s*)([A-Z0-9]+)((\s+)(.*))?'  
229
241
        result = re.match( basic_regexp, line, re.IGNORECASE )        
230
242
        if ( result == None ): return False
231
 
        return list( result.groups() )
 
243
        result_list = list( result.groups() )
 
244
        #we need to strip the outer parentesys around (\s+)(.*)
 
245
        del( result_list[2] )        
 
246
        return result_list
232
247
    def value_unescape(self, value):
233
248
        #value should have no precedig or trailing spaces
 
249
        if value == None: return None
234
250
        if value == "" : return value
235
251
        char = value[0]
236
252
        if char == '"' or char == "'":
250
266
    #TODO: doesn't hanlde single quotes at all :(
251
267
    def parse_options ( self, s ):
252
268
        """parse a value into a list of multiple options"""
 
269
        if s == None or s == False: return []
253
270
        s = s.rstrip()
254
 
        s = s.replace ( '\"', '"' )
 
271
        s = s.replace ( '\\"', '"' )
255
272
        result = '';    
256
 
        tokens = s.split( '"' )
 
273
        tokens = s.split( '"' )        
257
274
        for k, v in enumerate( tokens ):
258
275
            # replace spaces in every odd token
259
276
            if ( k & 1 ) == 1 : tokens[k] = v.replace( ' ', ' ' )
273
290
        for k,o in enumerate( options ):
274
291
            if ( option == o ): del options[ k ] 
275
292
        
276
 
        return self.change_value( line, " ".join( options ) )
 
293
        return self.change_raw_value( line, " ".join( options ) )
277
294
    def has_option (self, line, option):
278
295
        options = self.parse_options( line )
279
296
        for o in options: 
282
299
    
283
300
    def add_option ( self, line, option ):
284
301
        options = self.get_value( line );
285
 
        if options == False : options = ""   
 
302
        if options == False or options == None : options = ""   
286
303
        options = self.parse_options( options);
287
304
        found = False;
288
305
        for k,o in enumerate( options ):
291
308
        return self.change_raw_value( line, " ".join( options ))
292
309
 
293
310
    def value_escape ( self, value ):
 
311
        if not value: return ''
294
312
        if ( value.find(' ') != -1 ):
295
313
            value = '"'+value.replace( '"', '\\"' )+'"'
296
314
        return value;
299
317
        tokens = self.tokenize( line )
300
318
        if ( tokens == False ): return False;
301
319
        value = tokens.pop()
302
 
        value = value.strip()
303
 
        value = self.value_unescape( value )
304
 
        
 
320
        if isinstance( value, str ):
 
321
            value = value.strip()
 
322
            value = self.value_unescape( value )
305
323
        return value
306
 
 
 
324
    def get_comment (self, line ):
 
325
        line = line.lstrip()
 
326
        if not line.startswith('#'): return False
 
327
        return line.lstrip('#')
 
328
    def get_indentation (self, line ):
 
329
        tokens = self.tokenize( line );        
 
330
        if ( tokens == False ): return False
 
331
        return tokens[ 0 ]
307
332
    def get_directive ( self, line ):
308
333
        tokens = self.tokenize( line );        
309
334
        if ( tokens == False ): return False
315
340
 
316
341
    def change_raw_value ( self, line , new_value ):
317
342
        tokens = self.tokenize( line )
 
343
        for idx, tok in enumerate( tokens ):
 
344
            if tok is None: tokens[idx] = ''            
318
345
        tokens[2] = tokens[2].replace( "\n", '' ) #separator shuoldn't contain newlines
319
346
        if tokens[2] == '': tokens[2] = ' ' #at least as space as separator
320
347
        line = tokens[0]+tokens[1]+tokens[2]+new_value