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.
42 error( 'Invalid keyword `%r\' in %r.' % ( item , self.name ) )
44 def check( self , values ) :
48 error( 'Unexpected values %r for %r.' % ( values , self.name ) )
60 if not self.children :
61 error( 'Empty block for %r.' % self.name )
63 #-----------------------------------------------------------------------------
65 def checkConf( confNode , rootClass ) :
67 checkConf.lastNode = confNode
69 def _checkConf( confNode , syntaxNode ) :
71 checkConf.lastNode = confNode
72 name , values , contents , meta = confNode
73 r = syntaxNode.descend( name )
75 for item in contents :
76 _checkConf( item , r )
79 name , values , contents , info = confNode
80 root = rootClass( '__root__' )
82 for item in contents :
83 _checkConf( item , root )
85 meta = checkConf.lastNode[ 3 ]
86 raise Error( 'at line %d, column %d, %s'
87 % ( meta[ 0 ] , meta[ 1 ] , str( e ) ) )