7dfa04767a652cd2ed1e3c38a9fca0a789170fcb
[confparser-old] / basicvalidator.py
1 # -*- coding: iso-8859-1 -*-
2
3 #
4 # Base module to check structure validity of a configuration file.
5 #
6
7 class Error( Exception ) : pass
8
9 def error( what ) :
10
11         raise Error( what )
12
13 class Validator :
14
15         def __init__( self , name ) :
16
17                 self.name = name
18
19         def descend( self , item ) :
20
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 ) )
24
25         def check( self , values ) :
26
27                 # Check node values.
28                 if values :
29                         error( 'Unexpected values %r for %r.' % ( values , self.name ) )
30
31         def valid( self ) :
32
33                 pass
34
35 class MixinNonEmpty :
36
37         children = 0
38
39         def valid( self ) :
40
41                 if not self.children :
42                         error( 'Empty block for %r.' % self.name )
43
44 #-----------------------------------------------------------------------------
45
46 def checkConf( confNode , rootClass ) :
47
48         checkConf.lastNode = confNode
49
50         def _checkConf( confNode , syntaxNode ) :
51
52                 checkConf.lastNode = confNode
53                 name , values , contents , meta = confNode
54                 r = syntaxNode.descend( name )
55                 r.check( values )
56                 for item in contents :
57                         _checkConf( item , r )
58                 r.valid()
59
60         name , values , contents , info = confNode
61         root = rootClass( '__root__' )
62         try :
63                 for item in contents :
64                         _checkConf( item , root )
65         except Error , e :
66                 meta = checkConf.lastNode[ 3 ]
67                 raise Error( 'at line %d, column %d, %s' 
68                         % ( meta[ 0 ] , meta[ 1 ] , str( e ) ) )