1 # -*- coding: iso-8859-1 -*-
4 # Base module to check structure validity of a configuration file.
7 class Error( Exception ) : pass
15 def __init__( self , name ) :
19 def descend( self , item ) :
21 # Return a validator for the contents
22 # of the node 'item', or throw an exception.
23 error( 'Invalid keyword `%r\' in %r.' % ( item , self.name ) )
25 def check( self , values ) :
29 error( 'Unexpected values %r for %r.' % ( values , self.name ) )
41 if not self.children :
42 error( 'Empty block for %r.' % self.name )
44 #-----------------------------------------------------------------------------
46 def checkConf( confNode , rootClass ) :
48 checkConf.lastNode = confNode
50 def _checkConf( confNode , syntaxNode ) :
52 checkConf.lastNode = confNode
53 name , values , contents , meta = confNode
54 r = syntaxNode.descend( name )
56 for item in contents :
57 _checkConf( item , r )
60 name , values , contents , info = confNode
61 root = rootClass( '__root__' )
63 for item in contents :
64 _checkConf( item , root )
66 meta = checkConf.lastNode[ 3 ]
67 raise Error( 'at line %d, column %d, %s'
68 % ( meta[ 0 ] , meta[ 1 ] , str( e ) ) )