<s> import logging <EOL> log = logging . getLogger ( __name__ ) <EOL> import io <EOL> import struct <EOL> from record import Record <EOL> from message import Message <EOL> from uri_record import UriRecord <EOL> from text_record import TextRecord <EOL> actions = ( '<STR_LIT:default>' , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> class SmartPosterRecord ( Record ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , uri , title = { } , icons = { } , action = '<STR_LIT:default>' , <EOL> resource_size = None , resource_type = None ) : <EOL> super ( SmartPosterRecord , self ) . __init__ ( '<STR_LIT>' ) <EOL> self . _title = dict ( ) <EOL> self . title = title <EOL> self . icons = icons <EOL> self . action = action <EOL> self . resource_size = resource_size <EOL> self . resource_type = resource_type <EOL> if isinstance ( uri , Record ) : <EOL> record = uri <EOL> if record . type == self . type : <EOL> self . name = record . name <EOL> self . data = record . data <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> self . uri = uri <EOL> @ property <EOL> def data ( self ) : <EOL> message = Message ( UriRecord ( self . _uri ) ) <EOL> for lang , text in self . title . iteritems ( ) : <EOL> message . append ( TextRecord ( text = text , language = lang ) ) <EOL> for image_type , image_data in self . icons . iteritems ( ) : <EOL> message . append ( Record ( "<STR_LIT>" + image_type , data = image_data ) ) <EOL> if self . _action >= <NUM_LIT:0> : <EOL> message . append ( Record ( "<STR_LIT>" , data = chr ( self . _action ) ) ) <EOL> if self . _res_size : <EOL> size = struct . pack ( '<STR_LIT>' , self . _res_size ) <EOL> message . append ( Record ( "<STR_LIT>" , data = size ) ) <EOL> return str ( message ) <EOL> @ data . setter <EOL> def data ( self , string ) : <EOL> log . debug ( "<STR_LIT>" + repr ( string ) ) <EOL> if len ( string ) > <NUM_LIT:0> : <EOL> f = io . BytesIO ( string ) <EOL> while f . tell ( ) < len ( string ) : <EOL> record = Record ( data = f ) <EOL> if record . type == "<STR_LIT>" : <EOL> self . uri = UriRecord ( record ) . uri <EOL> elif record . type == "<STR_LIT>" : <EOL> record = TextRecord ( record ) <EOL> self . title [ record . language ] = record . text <EOL> elif record . type == "<STR_LIT>" : <EOL> self . _action = ord ( record . data ) <EOL> elif record . type == "<STR_LIT>" : <EOL> self . _res_size = struct . unpack ( '<STR_LIT>' , record . data ) <EOL> elif record . type == "<STR_LIT>" : <EOL> self . _res_type = record . data <EOL> elif record . type . startswith ( "<STR_LIT>" ) : <EOL> image_type = record . type . replace ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . icons [ image_type ] = record . data <EOL> else : <EOL> log . error ( "<STR_LIT>" ) <EOL> @ property <EOL> def uri ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _uri <EOL> @ uri . setter <EOL> def uri ( self , value ) : <EOL> try : <EOL> self . _uri = value . encode ( "<STR_LIT:ascii>" ) <EOL> except UnicodeDecodeError : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> except AttributeError : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> @ property <EOL> def title ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _title <EOL> @ title . setter <EOL> def title ( self , value ) : <EOL> if isinstance ( value , dict ) : <EOL> self . _title = value <EOL> else : <EOL> self . _title [ "<STR_LIT>" ] = value <EOL> @ property <EOL> def icons ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _icons <EOL> @ icons . setter <EOL> def icons ( self , value ) : <EOL> if not isinstance ( value , dict ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> self . _icons = value <EOL> @ property <EOL> def action ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return actions [ self . _action + <NUM_LIT:1> ] <EOL> except IndexError : <EOL> return str ( self . _action ) <EOL> @ action . setter <EOL> def action ( self , value ) : <EOL> try : <EOL> self . _action = actions . index ( value ) - <NUM_LIT:1> <EOL> except ValueError : <EOL> raise ValueError ( "<STR_LIT>" + repr ( actions ) ) <EOL> @ property <EOL> def resource_size ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _res_size <EOL> @ resource_size . setter <EOL> def resource_size ( self , value ) : <EOL> if value is not None : <EOL> value = int ( value ) <EOL> if value < <NUM_LIT:0> or value > <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . _res_size = value <EOL> @ property <EOL> def resource_type ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _res_type <EOL> @ resource_type . setter <EOL> def resource_type ( self , value ) : <EOL> self . _res_type = value <EOL> def pretty ( self , indent = <NUM_LIT:0> ) : <EOL> lines = list ( ) <EOL> lines . append ( ( "<STR_LIT>" , self . uri ) ) <EOL> if self . name : <EOL> lines . append ( ( "<STR_LIT>" , repr ( self . name ) ) ) <EOL> if self . resource_type : <EOL> lines . append ( ( "<STR_LIT>" , self . resource_type ) ) <EOL> if self . resource_size : <EOL> lines . append ( ( "<STR_LIT>" , str ( self . resource_size ) ) ) <EOL> for lang in sorted ( self . title ) : <EOL> lines . append ( ( "<STR_LIT>" % lang , self . title [ lang ] ) ) <EOL> for icon in sorted ( self . icons ) : <EOL> info = "<STR_LIT>" . format ( <EOL> repr ( self . icons [ icon ] [ : <NUM_LIT:10> ] ) . strip ( "<STR_LIT:'>" ) , <EOL> len ( self . icons [ icon ] ) ) <EOL> lines . append ( ( "<STR_LIT>" % icon , info ) ) <EOL> lines . append ( ( "<STR_LIT:action>" , self . action ) ) <EOL> indent = indent * '<STR_LIT:U+0020>' <EOL> lwidth = max ( [ len ( line [ <NUM_LIT:0> ] ) for line in lines ] ) <EOL> lines = [ line [ <NUM_LIT:0> ] . ljust ( lwidth ) + "<STR_LIT>" + line [ <NUM_LIT:1> ] for line in lines ] <EOL> return ( "<STR_LIT:\n>" ) . join ( [ indent + line for line in lines ] ) </s>
