1 from validator import *
4 # Configuration parser check *syntax*.
5 # Validator check *structure*, and eventually some values.
8 #-----------------------------------------------------------------------------
10 class Keyword( Validator ) : pass
12 class Header( Validator ) :
14 allowedMatches = [ 'is' , 'contains' , 'match' ]
16 def check( self , values ) :
18 if len( values ) != 3 :
19 error( 'header HEADER-NAME MATCH-TYPE MATCH-ARGUMENT ;' )
20 elif values[ 1 ] not in self.allowedMatches :
21 error( 'Allowed matches type in header rule are: %r' % self.allowedMatches )
23 #-----------------------------------------------------------------------------
25 class Logical( Validator , MixinNonEmpty ) :
27 def descend( self , item ) :
30 return ruleValidator( item )
32 class Reject( Validator , MixinNonEmpty ) :
34 def descend( self , item ) :
37 return ruleValidator( item )
39 def check( self , values ) :
41 if len( values ) != 1 :
42 error( 'reject CODE { .. }' )
44 class Folder( Validator , MixinNonEmpty ) :
46 def descend( self , item ) :
49 return ruleValidator( item )
51 def check( self , values ) :
53 if len( values ) != 1 :
54 error( 'folder FOLDER-NAME { .. }' )
56 #-----------------------------------------------------------------------------
58 def ruleValidator( item ) :
60 if item in [ 'broken' , 'infected' , 'spam' ] :
61 return Keyword( item )
62 elif item in [ 'or' , 'and' , 'not' ] :
63 return Logical( item )
64 elif item == 'header' :
67 error( 'Invalid keyword `%r\'.' % item )
69 #-----------------------------------------------------------------------------
71 class Root( Validator ) :
73 def descend( self , item ) :
77 elif item == 'folder' :
80 error( 'Invalid keyword `%r\'.' % item )
82 def values( self , values ) :
84 raise Exception( 'Internal error' )
86 #-----------------------------------------------------------------------------
88 def checkConf( confNode ) :
90 checkConf.lastNode = confNode
92 def _checkConf( confNode , syntaxNode ) :
94 checkConf.lastNode = confNode
95 name , values , contents , meta = confNode
96 r = syntaxNode.descend( name )
98 for item in contents :
99 _checkConf( item , r )
102 name , values , contents , info = confNode
103 root = Root( '__root__' )
105 for item in contents :
106 _checkConf( item , root )
107 except ValidatorError , e :
108 return e , checkConf.lastNode
110 def checkFile( filename ) :
114 doc = open( filename ).read()
115 doc = confparser.parse( doc )
117 print '%s:' % filename ,
121 msg = 'at line %s, column %s' % ( meta[ 0 ] , meta[ 1 ] )
123 msg = 'in file %s, ' % meta[ 2 ] + msg
124 print '%s: %s' % ( msg , exception )