<s> """<STR_LIT>""" <EOL> from __future__ import generators <EOL> import inspect <EOL> class View ( object ) : <EOL> """<STR_LIT>""" <EOL> _viewcache = { } <EOL> __view__ = ( ) <EOL> def __new__ ( rootclass , obj , * args , ** kwds ) : <EOL> self = object . __new__ ( rootclass ) <EOL> self . __obj__ = obj <EOL> self . __rootclass__ = rootclass <EOL> key = self . __viewkey__ ( ) <EOL> try : <EOL> self . __class__ = self . _viewcache [ key ] <EOL> except KeyError : <EOL> self . __class__ = self . _selectsubclass ( key ) <EOL> return self <EOL> def __getattr__ ( self , attr ) : <EOL> return getattr ( self . __obj__ , attr ) <EOL> def __viewkey__ ( self ) : <EOL> return self . __obj__ . __class__ <EOL> def __matchkey__ ( self , key , subclasses ) : <EOL> if inspect . isclass ( key ) : <EOL> keys = inspect . getmro ( key ) <EOL> else : <EOL> keys = [ key ] <EOL> for key in keys : <EOL> result = [ C for C in subclasses if key in C . __view__ ] <EOL> if result : <EOL> return result <EOL> return [ ] <EOL> def _selectsubclass ( self , key ) : <EOL> subclasses = list ( enumsubclasses ( self . __rootclass__ ) ) <EOL> for C in subclasses : <EOL> if not isinstance ( C . __view__ , tuple ) : <EOL> C . __view__ = ( C . __view__ , ) <EOL> choices = self . __matchkey__ ( key , subclasses ) <EOL> if not choices : <EOL> return self . __rootclass__ <EOL> elif len ( choices ) == <NUM_LIT:1> : <EOL> return choices [ <NUM_LIT:0> ] <EOL> else : <EOL> return type ( '<STR_LIT:?>' , tuple ( choices ) , { } ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __rootclass__ . __name__ , self . __obj__ ) <EOL> def enumsubclasses ( cls ) : <EOL> for subcls in cls . __subclasses__ ( ) : <EOL> for subsubclass in enumsubclasses ( subcls ) : <EOL> yield subsubclass <EOL> yield cls </s>
