<s> import copy <EOL> import uuid <EOL> from blitzdb . fields . base import BaseField <EOL> from blitzdb . fields import CharField <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> import six <EOL> if six . PY3 : <EOL> unicode = str <EOL> class DoesNotExist ( BaseException ) : <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . __class__ . __name__ <EOL> class MultipleDocumentsReturned ( BaseException ) : <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . __class__ . __name__ <EOL> class MetaDocument ( type ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( meta , name , bases , dct ) : <EOL> sanitized_dct = { } <EOL> sanitized_dct [ '<STR_LIT>' ] = { } <EOL> class_type = type . __new__ ( meta , name , bases , dct ) <EOL> fields = { } <EOL> if hasattr ( class_type , '<STR_LIT>' ) : <EOL> fields . update ( class_type . fields ) <EOL> for key , value in dct . items ( ) : <EOL> if isinstance ( value , BaseField ) : <EOL> if value . key : <EOL> field_key = value . key <EOL> else : <EOL> field_key = key <EOL> fields [ field_key ] = value <EOL> delattr ( class_type , key ) <EOL> class_type . fields = fields <EOL> global DoesNotExist , MultipleDocumentsReturned <EOL> class DoesNotExist ( DoesNotExist ) : <EOL> pass <EOL> class MultipleDocumentsReturned ( MultipleDocumentsReturned ) : <EOL> pass <EOL> class_type . DoesNotExist = DoesNotExist <EOL> class_type . MultipleDocumentsReturned = MultipleDocumentsReturned <EOL> if class_type in document_classes : <EOL> document_classes . remove ( class_type ) <EOL> if name == '<STR_LIT>' and bases == ( object , ) : <EOL> pass <EOL> elif not ( hasattr ( class_type . Meta , '<STR_LIT>' ) and class_type . Meta . autoregister == False ) : <EOL> document_classes . append ( class_type ) <EOL> return class_type <EOL> document_classes = [ ] <EOL> class Document ( object ) : <EOL> __metaclass__ = MetaDocument <EOL> """<STR_LIT>""" <EOL> abstract = True <EOL> class Meta : <EOL> PkType = CharField ( length = <NUM_LIT:32> , primary_key = True , indexed = True , nullable = False ) <EOL> primary_key = "<STR_LIT>" <EOL> indexes = { } <EOL> def __init__ ( self , attributes = None , lazy = False , backend = None , autoload = True , db_loader = None ) : <EOL> """<STR_LIT>""" <EOL> if not attributes : <EOL> attributes = { } <EOL> self . _attributes = attributes <EOL> self . _autoload = autoload <EOL> self . _backend = backend <EOL> self . _properties = { } <EOL> self . _db_loader = db_loader <EOL> if not lazy : <EOL> self . _lazy = False <EOL> else : <EOL> self . _lazy = True <EOL> self . _embed = False <EOL> self . initialize ( ) <EOL> def __getitem__ ( self , key ) : <EOL> try : <EOL> lazy = super ( Document , self ) . __getattribute__ ( '<STR_LIT>' ) <EOL> except AttributeError : <EOL> lazy = False <EOL> if lazy : <EOL> if key in self . lazy_attributes : <EOL> return self . lazy_attributes [ key ] <EOL> elif self . _autoload : <EOL> self . revert ( ) <EOL> return self . attributes [ key ] <EOL> @ property <EOL> def lazy ( self ) : <EOL> return self . _lazy <EOL> @ lazy . setter <EOL> def lazy ( self , lazy ) : <EOL> self . _lazy = lazy <EOL> @ property <EOL> def lazy_attributes ( self ) : <EOL> return self . _attributes <EOL> @ property <EOL> def attributes ( self ) : <EOL> if self . _lazy and self . _autoload : <EOL> self . revert ( ) <EOL> return self . _attributes <EOL> @ attributes . setter <EOL> def attributes ( self , value ) : <EOL> self . _attributes = value <EOL> def get ( self , key , default = None ) : <EOL> return self [ key ] if key in self else default <EOL> def has_key ( self , key ) : <EOL> return True if key in self else False <EOL> def keys ( self ) : <EOL> return self . attributes . keys ( ) <EOL> def clear ( self ) : <EOL> self . attributes . clear ( ) <EOL> def values ( self ) : <EOL> return self . attributes . values ( ) <EOL> def items ( self ) : <EOL> return self . attributes . items ( ) <EOL> @ property <EOL> def properties ( self ) : <EOL> return self . _properties <EOL> @ properties . setter <EOL> def properties ( self , value ) : <EOL> self . _properties = value <EOL> def __contains__ ( self , key ) : <EOL> return True if ( key in self . lazy_attributes or key in self . attributes ) else False <EOL> def __iter__ ( self ) : <EOL> for key in self . keys ( ) : <EOL> yield key <EOL> def get_lazy_attribute ( self , key ) : <EOL> return object . __getattribute__ ( self , key ) <EOL> def __getattr__ ( self , key , load_if_lazy = True ) : <EOL> try : <EOL> return super ( Document , self ) . __getattr__ ( key ) <EOL> except AttributeError : <EOL> pass <EOL> try : <EOL> if self . _lazy and self . _autoload : <EOL> self . revert ( ) <EOL> if key in self . _properties : <EOL> return self . _properties [ key ] <EOL> return self . _attributes [ key ] <EOL> except KeyError : <EOL> raise AttributeError ( key ) <EOL> def __setattr__ ( self , key , value ) : <EOL> if key . startswith ( '<STR_LIT:_>' ) or key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return super ( Document , self ) . __setattr__ ( key , value ) <EOL> else : <EOL> self . attributes [ key ] = value <EOL> def __delattr__ ( self , key ) : <EOL> if key . startswith ( '<STR_LIT:_>' ) : <EOL> return super ( Document , self ) . __delattr__ ( key ) <EOL> try : <EOL> del self . attributes [ key ] <EOL> except KeyError : <EOL> raise AttributeError ( key ) <EOL> __setitem__ = __setattr__ <EOL> def __delitem__ ( self , key ) : <EOL> try : <EOL> return self . __delattr__ ( key ) <EOL> except AttributeError : <EOL> raise KeyError ( key ) <EOL> def __copy__ ( self ) : <EOL> d = self . __class__ ( self . attributes . copy ( ) , lazy = self . _lazy , backend = self . _backend ) <EOL> return d <EOL> def __deepcopy__ ( self , memo ) : <EOL> d = self . __class__ ( copy . deepcopy ( self . attributes , memo ) , <EOL> lazy = self . _lazy , <EOL> backend = self . _backend ) <EOL> return d <EOL> def __hash__ ( self ) : <EOL> return id ( self ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __nonzero__ ( self ) : <EOL> if self . pk : <EOL> return True <EOL> return False <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if id ( self ) == id ( other ) : <EOL> return True <EOL> if type ( self ) != type ( other ) : <EOL> return False <EOL> if self . pk != None or other . pk != None : <EOL> if self . pk == other . pk : <EOL> return True <EOL> if self . attributes == other . attributes : <EOL> return True <EOL> return False <EOL> def __unicode__ ( self ) : <EOL> return self . __class__ . __name__ + "<STR_LIT>" . format ( self . get_pk_name ( ) , self . pk , self . _lazy ) <EOL> if six . PY3 : <EOL> __str__ = __unicode__ <EOL> else : <EOL> def __str__ ( self ) : <EOL> return unicode ( self ) . encode ( "<STR_LIT:utf-8>" ) <EOL> def _represent ( self , n = <NUM_LIT:1> ) : <EOL> if n < <NUM_LIT:0> : <EOL> return self . __class__ . __name__ + "<STR_LIT>" <EOL> def truncate_dict ( d , n = n ) : <EOL> if isinstance ( d , dict ) : <EOL> out = { } <EOL> return dict ( [ ( key , truncate_dict ( value , n - <NUM_LIT:1> ) ) for key , value in d . items ( ) ] ) <EOL> elif isinstance ( d , list ) or isinstance ( d , set ) : <EOL> return [ truncate_dict ( v , n - <NUM_LIT:1> ) for v in d ] <EOL> elif isinstance ( d , Document ) : <EOL> return d . _represent ( n - <NUM_LIT:1> ) <EOL> else : <EOL> return d <EOL> return self . __class__ . __name__ + "<STR_LIT:(>" + str ( truncate_dict ( self . _attributes ) ) + "<STR_LIT:)>" <EOL> __repr__ = _represent <EOL> def initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def autogenerate_pk ( self ) : <EOL> """<STR_LIT>""" <EOL> self . pk = uuid . uuid4 ( ) . hex <EOL> @ classmethod <EOL> def get_pk_name ( cls ) : <EOL> return cls . Meta . primary_key if hasattr ( cls . Meta , '<STR_LIT:primary_key>' ) else Document . Meta . primary_key <EOL> @ property <EOL> def pk ( self ) : <EOL> """<STR_LIT>""" <EOL> primary_key = self . get_pk_name ( ) <EOL> if primary_key in self . _attributes : <EOL> return self . _attributes [ primary_key ] <EOL> if self . _lazy and self . _db_loader : <EOL> self . revert ( ) <EOL> return self . pk <EOL> return None <EOL> @ property <EOL> def embed ( self ) : <EOL> return self . _embed <EOL> @ property <EOL> def eager ( self ) : <EOL> return self . load_if_lazy ( ) <EOL> @ pk . setter <EOL> def pk ( self , value ) : <EOL> self . _attributes [ self . get_pk_name ( ) ] = value <EOL> @ property <EOL> def backend ( self ) : <EOL> return self . _backend <EOL> @ backend . setter <EOL> def backend ( self , backend ) : <EOL> self . _backend = backend <EOL> def save ( self , backend = None ) : <EOL> """<STR_LIT>""" <EOL> if not backend : <EOL> if not self . _backend : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> return self . _backend . save ( self ) <EOL> self . _backend = backend <EOL> return backend . save ( self ) <EOL> def delete ( self , backend = None ) : <EOL> """<STR_LIT>""" <EOL> if not backend : <EOL> if not self . _backend : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> return self . _backend . delete ( self ) <EOL> backend . delete ( self ) <EOL> def revert ( self , backend = None ) : <EOL> """<STR_LIT>""" <EOL> self . _lazy = False <EOL> logger . debug ( "<STR_LIT>" % ( self . __class__ . __name__ , str ( self . pk ) ) ) <EOL> if self . _db_loader : <EOL> obj = self . _db_loader ( ) <EOL> else : <EOL> backend = backend or self . _backend <EOL> if not backend : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> if self . pk == None : <EOL> raise self . DoesNotExist ( "<STR_LIT>" ) <EOL> obj = backend . get ( self . __class__ , { self . get_pk_name ( ) : self . pk } ) <EOL> self . _attributes = obj . attributes <EOL> self . initialize ( ) <EOL> def load_if_lazy ( self ) : <EOL> if self . _lazy : <EOL> self . revert ( ) <EOL> return self </s>
