<s> """<STR_LIT>""" <EOL> import re <EOL> import google <EOL> import yaml <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class AttributeDefinitionError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class ValidationError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message , cause = None ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( cause , '<STR_LIT:args>' ) and cause . args : <EOL> Error . __init__ ( self , message , * cause . args ) <EOL> else : <EOL> Error . __init__ ( self , message ) <EOL> self . message = message <EOL> self . cause = cause <EOL> def __str__ ( self ) : <EOL> return str ( self . message ) <EOL> class MissingAttribute ( ValidationError ) : <EOL> """<STR_LIT>""" <EOL> def AsValidator ( validator ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( validator , ( str , unicode ) ) : <EOL> return Regex ( validator , type ( validator ) ) <EOL> if isinstance ( validator , type ) : <EOL> return Type ( validator ) <EOL> if isinstance ( validator , ( list , tuple , set ) ) : <EOL> return Options ( * tuple ( validator ) ) <EOL> if isinstance ( validator , Validator ) : <EOL> return validator <EOL> else : <EOL> raise AttributeDefinitionError ( '<STR_LIT>' % <EOL> str ( validator ) ) <EOL> class Validated ( object ) : <EOL> """<STR_LIT>""" <EOL> ATTRIBUTES = None <EOL> def __init__ ( self , ** attributes ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( self . ATTRIBUTES , dict ) : <EOL> raise AttributeDefinitionError ( <EOL> '<STR_LIT>' <EOL> % self . __class__ ) <EOL> for key in self . ATTRIBUTES . keys ( ) : <EOL> object . __setattr__ ( self , key , self . GetAttribute ( key ) . default ) <EOL> self . Set ( ** attributes ) <EOL> @ classmethod <EOL> def GetAttribute ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return AsValidator ( self . ATTRIBUTES [ key ] ) <EOL> def Set ( self , ** attributes ) : <EOL> """<STR_LIT>""" <EOL> for key , value in attributes . iteritems ( ) : <EOL> if key not in self . ATTRIBUTES : <EOL> raise ValidationError ( '<STR_LIT>' <EOL> % ( self . __class__ , key ) ) <EOL> setattr ( self , key , value ) <EOL> def CheckInitialized ( self ) : <EOL> """<STR_LIT>""" <EOL> for key in self . ATTRIBUTES . iterkeys ( ) : <EOL> try : <EOL> self . GetAttribute ( key ) ( getattr ( self , key ) ) <EOL> except MissingAttribute , e : <EOL> e . message = "<STR_LIT>" % key <EOL> raise e <EOL> def __setattr__ ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> if key in self . ATTRIBUTES : <EOL> value = self . GetAttribute ( key ) ( value ) <EOL> object . __setattr__ ( self , key , value ) <EOL> else : <EOL> raise ValidationError ( '<STR_LIT>' <EOL> % ( self . __class__ , key ) ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return repr ( self ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> values = [ ( attr , getattr ( self , attr ) ) for attr in self . ATTRIBUTES ] <EOL> dent = '<STR_LIT:U+0020>' <EOL> value_list = [ ] <EOL> for attr , value in values : <EOL> value_list . append ( '<STR_LIT>' % ( dent , attr , value ) ) <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , '<STR_LIT:U+0020>' . join ( value_list ) , dent ) <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if type ( self ) != type ( other ) : <EOL> return False <EOL> for key in self . ATTRIBUTES . iterkeys ( ) : <EOL> if getattr ( self , key ) != getattr ( other , key ) : <EOL> return False <EOL> return True <EOL> def __ne__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return not self . __eq__ ( other ) <EOL> def __hash__ ( self ) : <EOL> """<STR_LIT>""" <EOL> result = <NUM_LIT:0> <EOL> for key in self . ATTRIBUTES . iterkeys ( ) : <EOL> value = getattr ( self , key ) <EOL> if isinstance ( value , list ) : <EOL> value = tuple ( value ) <EOL> result = result ^ hash ( key ) ^ hash ( value ) <EOL> return result <EOL> @ staticmethod <EOL> def _ToValue ( validator , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , Validated ) : <EOL> return value . ToDict ( ) <EOL> elif isinstance ( value , ( list , tuple ) ) : <EOL> return [ Validated . _ToValue ( validator , item ) for item in value ] <EOL> else : <EOL> if isinstance ( validator , Validator ) : <EOL> return validator . ToValue ( value ) <EOL> return value <EOL> def ToDict ( self ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> for name , validator in self . ATTRIBUTES . iteritems ( ) : <EOL> value = getattr ( self , name ) <EOL> if not ( isinstance ( validator , Validator ) and value == validator . default ) : <EOL> result [ name ] = Validated . _ToValue ( validator , value ) <EOL> return result <EOL> def ToYAML ( self ) : <EOL> """<STR_LIT>""" <EOL> return yaml . dump ( self . ToDict ( ) , <EOL> default_flow_style = False , <EOL> Dumper = yaml . SafeDumper ) <EOL> class Validator ( object ) : <EOL> """<STR_LIT>""" <EOL> expected_type = object <EOL> def __init__ ( self , default = None ) : <EOL> """<STR_LIT>""" <EOL> self . default = default <EOL> def __call__ ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return self . Validate ( value ) <EOL> def Validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value <EOL> def ToValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value <EOL> class Type ( Validator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , expected_type , convert = True , default = None ) : <EOL> """<STR_LIT>""" <EOL> super ( Type , self ) . __init__ ( default ) <EOL> self . expected_type = expected_type <EOL> self . convert = convert <EOL> def Validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , self . expected_type ) : <EOL> if value is not None and self . convert : <EOL> try : <EOL> return self . expected_type ( value ) <EOL> except ValueError , e : <EOL> raise ValidationError ( '<STR_LIT>' <EOL> % value , <EOL> e ) <EOL> except TypeError , e : <EOL> raise ValidationError ( '<STR_LIT>' <EOL> % ( self . expected_type , value ) ) <EOL> else : <EOL> raise MissingAttribute ( '<STR_LIT>' ) <EOL> else : <EOL> return value <EOL> TYPE_BOOL = Type ( bool ) <EOL> TYPE_INT = Type ( int ) <EOL> TYPE_LONG = Type ( long ) <EOL> TYPE_STR = Type ( str ) <EOL> TYPE_UNICODE = Type ( unicode ) <EOL> TYPE_FLOAT = Type ( float ) <EOL> class Options ( Validator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * options , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:default>' in kw : <EOL> default = kw [ '<STR_LIT:default>' ] <EOL> else : <EOL> default = None <EOL> alias_map = { } <EOL> def AddAlias ( alias , original ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( alias , str ) : <EOL> raise AttributeDefinitionError ( <EOL> '<STR_LIT>' ) <EOL> elif alias in alias_map : <EOL> raise AttributeDefinitionError ( <EOL> "<STR_LIT>" % alias ) <EOL> alias_map [ alias ] = original <EOL> for option in options : <EOL> if isinstance ( option , str ) : <EOL> AddAlias ( option , option ) <EOL> elif isinstance ( option , ( list , tuple ) ) : <EOL> if len ( option ) != <NUM_LIT:2> : <EOL> raise AttributeDefinitionError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> original , aliases = option <EOL> AddAlias ( original , original ) <EOL> if not isinstance ( aliases , ( list , tuple ) ) : <EOL> raise AttributeDefinitionError ( '<STR_LIT>' ) <EOL> for alias in aliases : <EOL> AddAlias ( alias , original ) <EOL> else : <EOL> raise AttributeDefinitionError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> super ( Options , self ) . __init__ ( default ) <EOL> self . options = alias_map <EOL> def Validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> value = str ( value ) <EOL> if value not in self . options : <EOL> raise ValidationError ( '<STR_LIT>' <EOL> % ( value , self . options ) ) <EOL> return self . options [ value ] <EOL> class Optional ( Validator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , validator , default = None ) : <EOL> """<STR_LIT>""" <EOL> self . validator = AsValidator ( validator ) <EOL> self . expected_type = self . validator . expected_type <EOL> self . default = default <EOL> def Validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> return None <EOL> return self . validator ( value ) <EOL> class Regex ( Validator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , regex , string_type = unicode , default = None ) : <EOL> """<STR_LIT>""" <EOL> super ( Regex , self ) . __init__ ( default ) <EOL> if ( not issubclass ( string_type , basestring ) or <EOL> string_type is basestring ) : <EOL> raise AttributeDefinitionError ( <EOL> '<STR_LIT>' % str ( string_type ) ) <EOL> if isinstance ( regex , basestring ) : <EOL> self . re = re . compile ( '<STR_LIT>' % regex ) <EOL> else : <EOL> raise AttributeDefinitionError ( <EOL> '<STR_LIT>' % str ( regex ) ) <EOL> self . expected_type = string_type <EOL> def Validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if issubclass ( self . expected_type , str ) : <EOL> cast_value = TYPE_STR ( value ) <EOL> else : <EOL> cast_value = TYPE_UNICODE ( value ) <EOL> if self . re . match ( cast_value ) is None : <EOL> raise ValidationError ( '<STR_LIT>' <EOL> % ( value , self . re . pattern ) ) <EOL> return cast_value <EOL> class _RegexStrValue ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attribute , value ) : <EOL> """<STR_LIT>""" <EOL> self . __attribute = attribute <EOL> self . __value = value <EOL> self . __regex = None <EOL> def __AsString ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if issubclass ( self . __attribute . expected_type , str ) : <EOL> cast_value = TYPE_STR ( value ) <EOL> else : <EOL> cast_value = TYPE_UNICODE ( value ) <EOL> cast_value = cast_value . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> cast_value = cast_value . replace ( '<STR_LIT:\r>' , '<STR_LIT>' ) <EOL> return cast_value <EOL> def __BuildRegex ( self ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( self . __value , list ) : <EOL> value_list = self . __value <EOL> sequence = True <EOL> else : <EOL> value_list = [ self . __value ] <EOL> sequence = False <EOL> regex_list = [ ] <EOL> for item in value_list : <EOL> regex_list . append ( self . __AsString ( item ) ) <EOL> if sequence : <EOL> return '<STR_LIT:|>' . join ( '<STR_LIT>' % item for item in regex_list ) <EOL> else : <EOL> return regex_list [ <NUM_LIT:0> ] <EOL> def __Compile ( self ) : <EOL> """<STR_LIT>""" <EOL> regex = self . __BuildRegex ( ) <EOL> try : <EOL> return re . compile ( regex ) <EOL> except re . error , e : <EOL> raise ValidationError ( '<STR_LIT>' % ( regex , e ) , e ) <EOL> @ property <EOL> def regex ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __Compile ( ) <EOL> def match ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return re . match ( self . __BuildRegex ( ) , value ) <EOL> def Validate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __Compile ( ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __BuildRegex ( ) <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , _RegexStrValue ) : <EOL> return self . __BuildRegex ( ) == other . __BuildRegex ( ) <EOL> return str ( self ) == other <EOL> def __ne__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return not self . __eq__ ( other ) <EOL> class RegexStr ( Validator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , string_type = unicode , default = None ) : <EOL> """<STR_LIT>""" <EOL> if default is not None : <EOL> default = _RegexStrValue ( self , default ) <EOL> re . compile ( str ( default ) ) <EOL> super ( RegexStr , self ) . __init__ ( default ) <EOL> if ( not issubclass ( string_type , basestring ) or <EOL> string_type is basestring ) : <EOL> raise AttributeDefinitionError ( <EOL> '<STR_LIT>' % str ( string_type ) ) <EOL> self . expected_type = string_type <EOL> def Validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , _RegexStrValue ) : <EOL> return value <EOL> value = _RegexStrValue ( self , value ) <EOL> value . Validate ( ) <EOL> return value <EOL> def ToValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return str ( value ) <EOL> class Range ( Validator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , minimum , maximum , range_type = int , default = None ) : <EOL> """<STR_LIT>""" <EOL> super ( Range , self ) . __init__ ( default ) <EOL> if not isinstance ( minimum , range_type ) : <EOL> raise AttributeDefinitionError ( <EOL> '<STR_LIT>' % <EOL> ( str ( range_type ) , str ( type ( minimum ) ) , str ( minimum ) ) ) <EOL> if not isinstance ( maximum , range_type ) : <EOL> raise AttributeDefinitionError ( <EOL> '<STR_LIT>' % <EOL> ( str ( range_type ) , str ( type ( maximum ) ) , str ( maximum ) ) ) <EOL> self . minimum = minimum <EOL> self . maximum = maximum <EOL> self . expected_type = range_type <EOL> self . _type_validator = Type ( range_type ) <EOL> def Validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> cast_value = self . _type_validator . Validate ( value ) <EOL> if cast_value < self . minimum or cast_value > self . maximum : <EOL> raise ValidationError ( '<STR_LIT>' <EOL> % ( str ( value ) , <EOL> str ( self . minimum ) , <EOL> str ( self . maximum ) ) ) <EOL> return cast_value <EOL> class Repeated ( Validator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , constructor , default = None ) : <EOL> """<STR_LIT>""" <EOL> super ( Repeated , self ) . __init__ ( default ) <EOL> self . constructor = constructor <EOL> self . expected_type = list <EOL> def Validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , list ) : <EOL> raise ValidationError ( '<STR_LIT>' <EOL> '<STR_LIT>' % value ) <EOL> for item in value : <EOL> if isinstance ( self . constructor , Validator ) : <EOL> item = self . constructor . Validate ( item ) <EOL> elif not isinstance ( item , self . constructor ) : <EOL> raise ValidationError ( '<STR_LIT>' <EOL> % ( str ( self . constructor ) , str ( item ) ) ) <EOL> return value </s>
