<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import logging <EOL> import pprint <EOL> import time <EOL> from tornado . escape import url_escape , xhtml_escape <EOL> from viewfinder . backend . base . util import ConvertToString <EOL> from viewfinder . backend . db . contact import Contact <EOL> from viewfinder . backend . db . db_client import DBKey <EOL> from viewfinder . backend . db . follower import Follower <EOL> from viewfinder . backend . db . schema import UnpackLocation , UnpackPlacemark <EOL> from viewfinder . backend . db . episode import Episode <EOL> from viewfinder . backend . db . photo import Photo <EOL> from viewfinder . backend . db . viewpoint import Viewpoint <EOL> class FmtDefault ( object ) : <EOL> def __init__ ( self , table ) : <EOL> self . _table = table <EOL> def FormatItemAttributes ( self , item ) : <EOL> """<STR_LIT>""" <EOL> attributes = self . _FormatAllAttributes ( item ) <EOL> rows = [ pretty for _ , _ , _ , pretty in attributes ] <EOL> return rows <EOL> def FormatItemAttributesForView ( self , item ) : <EOL> """<STR_LIT>""" <EOL> attributes = self . _FormatAllAttributes ( item ) <EOL> rows = [ ( name , key , pretty ) for name , key , _ , pretty in attributes ] <EOL> rows . extend ( self . _GetExtraViewFields ( item ) ) <EOL> return rows <EOL> def _GetExtraViewFields ( self , item ) : <EOL> """<STR_LIT>""" <EOL> return [ ] <EOL> @ staticmethod <EOL> def _Escape ( val ) : <EOL> return url_escape ( ConvertToString ( val ) ) <EOL> @ staticmethod <EOL> def _XEscape ( val ) : <EOL> return xhtml_escape ( ConvertToString ( val ) ) <EOL> @ staticmethod <EOL> def _HashQueryLink ( table , key , name = None ) : <EOL> return '<STR_LIT>' % ( FmtDefault . _Escape ( table ) , FmtDefault . _Escape ( key ) , FmtDefault . _XEscape ( name if name is not None else key ) ) <EOL> @ staticmethod <EOL> def _SortQueryLink ( table , hash_key , sort_key , name = None ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( FmtDefault . _Escape ( table ) , FmtDefault . _Escape ( hash_key ) , FmtDefault . _Escape ( sort_key ) , <EOL> FmtDefault . _XEscape ( name if name is not None else '<STR_LIT>' % ( hash_key , sort_key ) ) ) <EOL> @ staticmethod <EOL> def _EpisodeLink ( vp , name = None ) : <EOL> return FmtDefault . _HashQueryLink ( '<STR_LIT>' , vp , name ) <EOL> @ staticmethod <EOL> def _PhotoLink ( vp , name = None ) : <EOL> return FmtDefault . _HashQueryLink ( '<STR_LIT>' , vp , name ) <EOL> @ staticmethod <EOL> def _UserLink ( vp , name = None ) : <EOL> return FmtDefault . _HashQueryLink ( '<STR_LIT>' , vp , name ) <EOL> @ staticmethod <EOL> def _ViewpointLink ( vp , name = None ) : <EOL> return FmtDefault . _HashQueryLink ( '<STR_LIT>' , vp , name ) <EOL> def _FormatAllAttributes ( self , item ) : <EOL> """<STR_LIT>""" <EOL> attrs = [ ] <EOL> for name in self . _table . GetColumnNames ( ) : <EOL> c = self . _table . GetColumn ( name ) <EOL> value = item . get ( c . key , None ) <EOL> pretty = self . _FormatAttribute ( name , value ) if value is not None else '<STR_LIT:->' <EOL> attrs . append ( ( name , c . key , value , pretty ) ) <EOL> return attrs <EOL> def _FormatAttribute ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> if name == '<STR_LIT>' or name == '<STR_LIT>' : <EOL> did , ( vid , sid ) = Viewpoint . DeconstructViewpointId ( value ) <EOL> pretty = '<STR_LIT>' % ( value , did , vid ) <EOL> return FmtDefault . _ViewpointLink ( value , pretty ) <EOL> elif name == '<STR_LIT>' or name == '<STR_LIT>' : <EOL> return self . _UserLink ( value ) <EOL> elif name == '<STR_LIT>' or name == '<STR_LIT>' : <EOL> ts , did , ( eid , sid ) = Episode . DeconstructEpisodeId ( value ) <EOL> pretty = '<STR_LIT>' % ( value , did , eid ) <EOL> return self . _EpisodeLink ( value , pretty ) <EOL> elif name == '<STR_LIT>' or name == '<STR_LIT>' : <EOL> ts , did , ( pid , sid ) = Photo . DeconstructPhotoId ( value ) <EOL> pretty = '<STR_LIT>' % ( value , did , pid ) <EOL> return self . _PhotoLink ( value , pretty ) <EOL> elif name == '<STR_LIT>' or name == '<STR_LIT>' or name == '<STR_LIT>' or name == '<STR_LIT>' : <EOL> return self . _FormatTimestamp ( value ) <EOL> elif name == '<STR_LIT:location>' : <EOL> return self . _XEscape ( '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' % ( k , v ) for k , v in UnpackLocation ( value ) . _asdict ( ) . items ( ) ] ) ) <EOL> elif name == '<STR_LIT>' : <EOL> return self . _XEscape ( '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' % ( k , v ) for k , v in UnpackPlacemark ( value ) . _asdict ( ) . items ( ) ] ) ) <EOL> else : <EOL> return self . _XEscape ( '<STR_LIT:%s>' % value ) <EOL> def _FormatTimestamp ( self , timestamp ) : <EOL> """<STR_LIT>""" <EOL> return self . _XEscape ( time . asctime ( time . gmtime ( timestamp ) ) ) <EOL> def _GetQueryURL ( self , table , hash_key ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( self . _Escape ( table ) , self . _Escape ( repr ( hash_key ) ) ) <EOL> class FmtAccounting ( FmtDefault ) : <EOL> _names = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> def _FormatAttribute ( self , name , value ) : <EOL> if name == '<STR_LIT>' : <EOL> split = value . split ( '<STR_LIT::>' ) <EOL> prefix = split [ <NUM_LIT:0> ] <EOL> prefix_name = self . _names [ prefix ] <EOL> if prefix == '<STR_LIT>' : <EOL> return '<STR_LIT>' % ( self . _XEscape ( prefix_name ) , self . _ViewpointLink ( split [ <NUM_LIT:1> ] ) ) <EOL> elif prefix == '<STR_LIT>' : <EOL> return '<STR_LIT>' % ( self . _XEscape ( prefix_name ) , self . _UserLink ( split [ <NUM_LIT:1> ] ) ) <EOL> elif name == '<STR_LIT>' : <EOL> split = value . split ( '<STR_LIT::>' ) <EOL> prefix = split [ <NUM_LIT:0> ] <EOL> prefix_name = self . _names [ prefix ] <EOL> if len ( split ) == <NUM_LIT:1> : <EOL> return prefix_name <EOL> elif prefix == '<STR_LIT>' or prefix == '<STR_LIT>' : <EOL> return '<STR_LIT>' % ( self . _XEscape ( prefix_name ) , self . _UserLink ( split [ <NUM_LIT:1> ] ) ) <EOL> return FmtDefault . _FormatAttribute ( self , name , value ) <EOL> class FmtEpisode ( FmtDefault ) : <EOL> def _GetExtraViewFields ( self , item ) : <EOL> ep_id = item . get ( '<STR_LIT>' ) <EOL> extras = [ ] <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , '<STR_LIT>' % ep_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , ep_id , '<STR_LIT>' ) ) <EOL> return [ ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' . join ( extras ) ) ] <EOL> class FmtIdentity ( FmtDefault ) : <EOL> def _GetExtraViewFields ( self , item ) : <EOL> id_id = item . get ( '<STR_LIT>' ) <EOL> extras = [ ] <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , '<STR_LIT>' % id_id , '<STR_LIT>' ) ) <EOL> return [ ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' . join ( extras ) ) ] <EOL> class FmtIndex ( FmtDefault ) : <EOL> def _FormatAllAttributes ( self , item ) : <EOL> """<STR_LIT>""" <EOL> attrs = [ ] <EOL> term = item . get ( '<STR_LIT:t>' , None ) <EOL> key = item . get ( '<STR_LIT:k>' , None ) <EOL> data = item . get ( '<STR_LIT:d>' , None ) <EOL> split = term . split ( '<STR_LIT::>' ) <EOL> table = split [ <NUM_LIT:0> ] <EOL> key_pretty = key <EOL> if table == '<STR_LIT>' : <EOL> db_key = Contact . _ParseIndexKey ( key ) <EOL> key_pretty = self . _SortQueryLink ( '<STR_LIT>' , db_key . hash_key , db_key . range_key ) <EOL> elif table == '<STR_LIT>' : <EOL> key_pretty = self . _EpisodeLink ( key ) <EOL> elif table == '<STR_LIT>' : <EOL> db_key = Follower . _ParseIndexKey ( key ) <EOL> key_pretty = self . _SortQueryLink ( '<STR_LIT>' , db_key . hash_key , db_key . range_key ) <EOL> elif table == '<STR_LIT:id>' : <EOL> key_pretty = self . _HashQueryLink ( '<STR_LIT>' , key ) <EOL> elif table == '<STR_LIT>' : <EOL> key_pretty = self . _ViewpointLink ( key ) <EOL> attrs . append ( ( '<STR_LIT>' , '<STR_LIT:t>' , term , term ) ) <EOL> attrs . append ( ( '<STR_LIT:key>' , '<STR_LIT:k>' , key , key_pretty ) ) <EOL> attrs . append ( ( '<STR_LIT:data>' , '<STR_LIT:t>' , data , data ) ) <EOL> attrs . append ( ( '<STR_LIT>' , '<STR_LIT>' , data , data ) ) <EOL> return attrs <EOL> class FmtLock ( FmtDefault ) : <EOL> def _FormatAttribute ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> if name == '<STR_LIT>' : <EOL> if value < time . time ( ) : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return self . _FormatTimestamp ( value ) <EOL> else : <EOL> return FmtDefault . _FormatAttribute ( self , name , value ) <EOL> class FmtOperation ( FmtDefault ) : <EOL> def _FormatAttribute ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> if name in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return '<STR_LIT>' % self . _XEscape ( value ) <EOL> elif name == '<STR_LIT>' : <EOL> if value < time . time ( ) : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return self . _FormatTimestamp ( value ) <EOL> else : <EOL> return FmtDefault . _FormatAttribute ( self , name , value ) <EOL> class FmtUser ( FmtDefault ) : <EOL> def _GetExtraViewFields ( self , item ) : <EOL> user_id = item . get ( '<STR_LIT>' ) <EOL> extras = [ ] <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , '<STR_LIT>' % user_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , user_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , user_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , '<STR_LIT>' % user_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , user_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , user_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , user_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , '<STR_LIT>' % user_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , user_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , '<STR_LIT>' % user_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , user_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , '<STR_LIT>' % user_id , '<STR_LIT>' ) ) <EOL> return [ ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' . join ( extras ) ) ] <EOL> class FmtViewpoint ( FmtDefault ) : <EOL> def _GetExtraViewFields ( self , item ) : <EOL> vp_id = item . get ( '<STR_LIT>' ) <EOL> extras = [ ] <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , '<STR_LIT>' % vp_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , vp_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , vp_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , '<STR_LIT>' % vp_id , '<STR_LIT>' ) ) <EOL> extras . append ( self . _HashQueryLink ( '<STR_LIT>' , '<STR_LIT>' % vp_id , '<STR_LIT>' ) ) <EOL> return [ ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' . join ( extras ) ) ] </s>
