<s> from FieldPrototype import Field <EOL> import logging <EOL> import copy <EOL> class StringField ( Field ) : <EOL> """<STR_LIT>""" <EOL> base_type = unicode <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . max_length = False <EOL> if '<STR_LIT:max_length>' in kwargs . keys ( ) : <EOL> self . max_length = kwargs [ '<STR_LIT:max_length>' ] <EOL> super ( StringField , self ) . __init__ ( * args , ** kwargs ) <EOL> def validate_max_length ( self ) : <EOL> if self . max_length != <NUM_LIT:0> : <EOL> if len ( self . _value ) > self . max_length : <EOL> raise ValueError ( '<STR_LIT>' % ( self . __class__ . __name__ , self . max_length ) ) <EOL> else : <EOL> return True <EOL> def validate_type ( self ) : <EOL> if isinstance ( self . _value , self . base_type ) : <EOL> return True <EOL> elif type ( self . _value ) == str : <EOL> self . _value = unicode ( self . _value ) <EOL> logging . warning ( '<STR_LIT>' ) <EOL> return True <EOL> else : <EOL> if self . allow_empty : <EOL> if self . _value == None : <EOL> return True <EOL> try : <EOL> logging . warning ( '<STR_LIT>' ) <EOL> self . _value = unicode ( self . _value ) <EOL> except : <EOL> raise TypeError ( "<STR_LIT>" % ( self . __class__ . __name__ , str ( self . base_type ) , str ( type ( self . _value ) ) ) ) <EOL> def validate ( self ) : <EOL> super ( StringField , self ) . validate ( ) <EOL> if self . max_length != <NUM_LIT:0> : <EOL> self . validate_max_length ( ) <EOL> class IntegerField ( Field ) : <EOL> """<STR_LIT>""" <EOL> base_type = int <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . max_value = False <EOL> self . min_value = False <EOL> if '<STR_LIT>' in kwargs . keys ( ) : <EOL> self . max_value = kwargs [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in kwargs . keys ( ) : <EOL> self . min_value = kwargs [ '<STR_LIT>' ] <EOL> super ( IntegerField , self ) . __init__ ( * args , ** kwargs ) <EOL> def validate_max ( self ) : <EOL> if self . max_value : <EOL> if self . _value > self . max_value : <EOL> raise ValueError ( "<STR_LIT>" % ( self . __class__ . __name__ , self . max_value ) ) <EOL> else : <EOL> return True <EOL> else : <EOL> return True <EOL> def validate_min ( self ) : <EOL> if self . min_value : <EOL> if self . _value < self . min_value : <EOL> raise ValueError ( "<STR_LIT>" % ( self . __class__ . __name__ , self . min_value ) ) <EOL> else : <EOL> return True <EOL> else : <EOL> return True <EOL> def validate ( self ) : <EOL> super ( IntegerField , self ) . validate ( ) <EOL> if self . min_value : <EOL> self . validate_min ( ) <EOL> if self . max_value : <EOL> self . validate_max ( ) <EOL> class BooleanField ( Field ) : <EOL> """<STR_LIT>""" <EOL> base_type = bool <EOL> def get_value ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _value is not None : <EOL> return self . _value <EOL> else : <EOL> if self . default : <EOL> return self . default <EOL> else : <EOL> return False <EOL> class FloatField ( IntegerField ) : <EOL> """<STR_LIT>""" <EOL> base_type = float <EOL> def validate_type ( self ) : <EOL> if type ( self . _value ) == self . base_type : <EOL> return True <EOL> elif type ( self . _value ) == int : <EOL> self . _value = float ( self . _value ) <EOL> logging . warning ( '<STR_LIT>' ) <EOL> return True <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % ( self . __class__ . __name__ , str ( self . base_type ) ) ) <EOL> class ListField ( Field ) : <EOL> """<STR_LIT>""" <EOL> base_type = list <EOL> def __getitem__ ( self , item ) : <EOL> return self . _value [ item ] <EOL> def __setitem__ ( self , key , value ) : <EOL> self . _value [ key ] = value <EOL> def __init__ ( self , of = None , * args , ** kwargs ) : <EOL> self . of = of <EOL> super ( ListField , self ) . __init__ ( * args , ** kwargs ) <EOL> def expand_list ( self , list ) : <EOL> ret_list = [ ] <EOL> for item in list : <EOL> if hasattr ( item , '<STR_LIT>' ) : <EOL> if self . of : <EOL> val = item <EOL> else : <EOL> val = item . get_value ( ) <EOL> else : <EOL> val = item <EOL> ret_list . append ( val ) <EOL> return ret_list <EOL> def _setValue ( self , value ) : <EOL> set_list = [ ] <EOL> if self . of : <EOL> if isinstance ( value , list ) : <EOL> for item in value : <EOL> if isinstance ( item , self . of ) : <EOL> set_list . append ( item ) <EOL> else : <EOL> thisObject = self . of ( item ) <EOL> set_list . append ( thisObject ) <EOL> self . _value = set_list <EOL> elif isinstance ( value , self . __class__ ) : <EOL> self . value = value . value <EOL> elif value is None : <EOL> self . _value = None <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> self . _value = value <EOL> def _getValue ( self ) : <EOL> return self . get_value ( ) <EOL> value = property ( _getValue , _setValue ) <EOL> def get_value ( self ) : <EOL> if self . _value : <EOL> return self . expand_list ( self . _value ) <EOL> else : <EOL> return None <EOL> from bson . objectid import ObjectId <EOL> class ObjectIDField ( Field ) : <EOL> """<STR_LIT>""" <EOL> base_type = ObjectId <EOL> def __repr__ ( self ) : <EOL> return str ( self ) <EOL> def __unicode__ ( self ) : <EOL> return unicode ( self . _value ) <EOL> def __str__ ( self ) : <EOL> return str ( self . _value ) <EOL> from datetime import datetime <EOL> class DateTimeField ( Field ) : <EOL> """<STR_LIT>""" <EOL> base_type = datetime </s>
