1 # -*- coding: iso-8859-1 -*-
5 # Copyright (C) 2005 Frédéric Jolliton <frederic@jolliton.com>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 # Base module to check structure validity of a configuration file.
26 class Error( Exception ) : pass
34 def __init__( self , name ) :
38 def descend( self , item ) :
40 '''Return a validator for the contents
41 of the node 'item', or throw an exception.'''
43 error( 'Invalid keyword `%r\' in %r.' % ( item , self.name ) )
45 def check( self , values ) :
47 '''Check node values.'''
50 error( 'Unexpected values %r for %r.' % ( values , self.name ) )
54 '''Called once node contents is valided.'''
58 def validate( node , validator ) :
60 '''Validate tree from node 'node' with validator 'validator'.'''
62 validate.lastNode = node
64 def _checkConf( node , syntaxNode ) :
66 validate.lastNode = node
67 name , values , contents , meta = node
68 r = syntaxNode.descend( name )
70 for item in contents :
71 _checkConf( item , r )
74 name , values , contents , meta = node
75 root = validator( '__root__' )
77 for item in contents :
78 _checkConf( item , root )
80 meta = validate.lastNode[ 3 ]
81 raise Error( 'at line %d, column %d, %s'
82 % ( meta[ 0 ] , meta[ 1 ] , str( e ) ) )