<s> import os <EOL> import re <EOL> import sys <EOL> class Validator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self , value ) : <EOL> raise NotImplementedError ( ) <EOL> def format ( self , value ) : <EOL> raise NotImplementedError ( ) <EOL> class Boolean ( Validator ) : <EOL> """<STR_LIT>""" <EOL> truth_values = { <EOL> '<STR_LIT:1>' : True , '<STR_LIT:0>' : False , <EOL> '<STR_LIT:t>' : True , '<STR_LIT:f>' : False , <EOL> '<STR_LIT:true>' : True , '<STR_LIT:false>' : False , <EOL> '<STR_LIT:y>' : True , '<STR_LIT:n>' : False , <EOL> '<STR_LIT:yes>' : True , '<STR_LIT>' : False <EOL> } <EOL> def __call__ ( self , value ) : <EOL> if not ( value is None or isinstance ( value , bool ) ) : <EOL> value = str ( value ) . lower ( ) <EOL> if value not in Boolean . truth_values : <EOL> raise ValueError ( '<STR_LIT>' % value ) <EOL> value = Boolean . truth_values [ value ] <EOL> return value <EOL> class Fieldname ( Validator ) : <EOL> """<STR_LIT>""" <EOL> pattern = re . compile ( r'''<STR_LIT>''' ) <EOL> def __call__ ( self , value ) : <EOL> value = str ( value ) <EOL> if Fieldname . pattern . match ( value ) is None : <EOL> raise ValueError ( '<STR_LIT>' % value ) <EOL> return value <EOL> class File ( Validator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mode = '<STR_LIT:r>' , buffering = - <NUM_LIT:1> ) : <EOL> self . mode = mode <EOL> self . buffering = buffering <EOL> def __call__ ( self , value ) : <EOL> if value is not None : <EOL> try : <EOL> path = str ( value ) <EOL> if not os . path . isabs ( path ) : <EOL> path = os . path . join ( File . _var_run_splunk , path ) <EOL> value = open ( path , self . mode , self . buffering ) <EOL> except IOError as e : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> % ( value , self . mode , self . buffering , e ) ) <EOL> return value <EOL> def format ( self , value ) : <EOL> return value . name <EOL> _var_run_splunk = os . path . join ( <EOL> os . environ [ '<STR_LIT>' ] , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> class Integer ( Validator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , minimum = - sys . maxint - <NUM_LIT:1> , maximum = sys . maxint ) : <EOL> self . minimum = minimum <EOL> self . maximum = maximum <EOL> def __call__ ( self , value ) : <EOL> if value is not None : <EOL> value = int ( value ) <EOL> if not ( self . minimum <= value <= self . maximum ) : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> % ( self . minimum , self . maximum , value ) ) <EOL> return value <EOL> class Duration ( Validator ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self , value ) : <EOL> if value is None : <EOL> return None <EOL> try : <EOL> p = value . split ( '<STR_LIT::>' , <NUM_LIT:2> ) <EOL> _60 = Duration . _60 <EOL> _unsigned = Duration . _unsigned <EOL> if len ( p ) == <NUM_LIT:1> : <EOL> result = _unsigned ( p [ <NUM_LIT:0> ] ) <EOL> if len ( p ) == <NUM_LIT:2> : <EOL> result = <NUM_LIT> * _unsigned ( p [ <NUM_LIT:0> ] ) + _60 ( p [ <NUM_LIT:1> ] ) <EOL> if len ( p ) == <NUM_LIT:3> : <EOL> result = <NUM_LIT> * _unsigned ( p [ <NUM_LIT:0> ] ) + <NUM_LIT> * _60 ( p [ <NUM_LIT:1> ] ) + _60 ( p [ <NUM_LIT:2> ] ) <EOL> except ValueError : <EOL> raise ValueError ( "<STR_LIT>" , value ) <EOL> return result <EOL> def format ( self , value ) : <EOL> value = int ( value ) <EOL> s = value % <NUM_LIT> <EOL> m = value / <NUM_LIT> % <NUM_LIT> <EOL> h = value / ( <NUM_LIT> * <NUM_LIT> ) <EOL> return '<STR_LIT>' % ( h , m , s ) <EOL> _60 = Integer ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> _unsigned = Integer ( <NUM_LIT:0> ) <EOL> class OptionName ( Validator ) : <EOL> """<STR_LIT>""" <EOL> pattern = re . compile ( r'''<STR_LIT>''' ) <EOL> def __call__ ( self , value ) : <EOL> value = str ( value ) <EOL> if OptionName . pattern . match ( value ) is None : <EOL> raise ValueError ( '<STR_LIT>' % value ) <EOL> return value <EOL> class RegularExpression ( Validator ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self , value ) : <EOL> value = str ( value ) <EOL> try : <EOL> value = re . compile ( value ) <EOL> except re . error as e : <EOL> raise ValueError ( '<STR_LIT>' % ( str ( e ) . capitalize ( ) , value ) ) <EOL> return value <EOL> def format ( self , value ) : <EOL> return value . pattern <EOL> class Set ( Validator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args ) : <EOL> self . membership = args <EOL> def __call__ ( self , value ) : <EOL> if value is not None : <EOL> value = str ( value ) <EOL> if value not in self . membership : <EOL> raise ValueError ( '<STR_LIT>' % value ) <EOL> return value </s>
