<s> import calendar <EOL> from airy . core . conf import settings <EOL> from airy . core . exceptions import Http404 <EOL> from airy . core . files . uploadedfile import SimpleUploadedFile <EOL> from airy . core . reportbug import report_on_fail <EOL> from airy . utils . encoding import smart_unicode <EOL> from airy . utils . functional import curry <EOL> from tornado . web import * <EOL> from tornado . escape import * <EOL> from tornadio2 import TornadioRouter , SocketConnection , event <EOL> from urlparse import urlparse , parse_qs <EOL> from urllib2 import unquote <EOL> import logging <EOL> import base64 <EOL> class ConnectionSet ( set ) : <EOL> """<STR_LIT>""" <EOL> def _get_filter_condition ( self , item , key , value ) : <EOL> try : <EOL> key , tester = key . split ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> tester = None <EOL> if tester : <EOL> if callable ( getattr ( getattr ( item , key ) , tester ) ) : <EOL> condition = getattr ( getattr ( item , key ) , tester ) ( value ) <EOL> else : <EOL> condition = getattr ( getattr ( item , key ) , tester ) == value <EOL> else : <EOL> condition = getattr ( item , key ) == value <EOL> return condition <EOL> def filter ( self , ** kwargs ) : <EOL> filtered_set = ConnectionSet ( self ) <EOL> for item in self : <EOL> for key , value in kwargs . iteritems ( ) : <EOL> condition = self . _get_filter_condition ( item , key , value ) <EOL> if not condition : <EOL> filtered_set = filtered_set - set ( [ item ] ) <EOL> break <EOL> return filtered_set <EOL> def exclude ( self , * args , ** kwargs ) : <EOL> if len ( args ) : <EOL> for conn in args : <EOL> if isinstance ( conn , AiryHandler ) : <EOL> kwargs [ '<STR_LIT>' ] = conn . connection . session . session_id <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] = conn . session . session_id <EOL> filtered_set = ConnectionSet ( self ) <EOL> for item in self : <EOL> for key , value in kwargs . iteritems ( ) : <EOL> condition = self . _get_filter_condition ( item , key , value ) <EOL> if condition : <EOL> filtered_set = filtered_set - set ( [ item ] ) <EOL> break <EOL> return filtered_set <EOL> @ property <EOL> def ui ( self ) : <EOL> return UIProxySet ( self ) <EOL> def __getattr__ ( self , name ) : <EOL> def filtered_func ( conn_set , name , * args , ** kwargs ) : <EOL> for item in conn_set : <EOL> getattr ( item , name ) ( * args , ** kwargs ) <EOL> return conn_set <EOL> return curry ( filtered_func , self , name ) <EOL> class UIProxy ( object ) : <EOL> """<STR_LIT>""" <EOL> _data = '<STR_LIT>' <EOL> def __init__ ( self , connection ) : <EOL> self . connection = connection <EOL> def execute ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . connection . emit ( '<STR_LIT>' , data ) <EOL> self . _data = '<STR_LIT>' <EOL> return self <EOL> def redirect ( self , url ) : <EOL> """<STR_LIT>""" <EOL> return self . execute ( '<STR_LIT>' % url ) <EOL> def insert ( self , target , data = None ) : <EOL> """<STR_LIT>""" <EOL> data = data or self . _data <EOL> return self . execute ( '<STR_LIT>' % ( target , json_encode ( data ) ) ) <EOL> def after ( self , target , data = None ) : <EOL> """<STR_LIT>""" <EOL> data = data or self . _data <EOL> return self . execute ( '<STR_LIT>' % ( target , json_encode ( data ) ) ) <EOL> def append ( self , target , data = None ) : <EOL> """<STR_LIT>""" <EOL> data = data or self . _data <EOL> return self . execute ( '<STR_LIT>' % ( target , json_encode ( data ) ) ) <EOL> def prepend ( self , target , data = None ) : <EOL> """<STR_LIT>""" <EOL> data = data or self . _data <EOL> return self . execute ( '<STR_LIT>' % ( target , json_encode ( data ) ) ) <EOL> def remove ( self , target ) : <EOL> """<STR_LIT>""" <EOL> return self . execute ( '<STR_LIT>' % target ) <EOL> def set_title ( self , text ) : <EOL> """<STR_LIT>""" <EOL> return self . execute ( '<STR_LIT>' % text ) <EOL> def set_meta_description ( self , text ) : <EOL> """<STR_LIT>""" <EOL> return self . execute ( '<STR_LIT>' % text ) <EOL> def render_string ( self , template_name , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . connection . render_string ( template_name , ** kwargs ) <EOL> def render ( self , target , template_name , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> html = self . render_string ( template_name , ** kwargs ) <EOL> self . insert ( target , html ) <EOL> return self <EOL> def html ( self , template_name , ** kwargs ) : <EOL> self . _data = self . connection . render_string ( template_name , ** kwargs ) <EOL> return self <EOL> class UIProxySet ( object ) : <EOL> def __init__ ( self , connections ) : <EOL> self . connections = connections <EOL> def __getattr__ ( self , name ) : <EOL> def filtered_func ( ui_set , name , * args , ** kwargs ) : <EOL> for item in ui_set . connections : <EOL> getattr ( item . ui , name ) ( * args , ** kwargs ) <EOL> return ui_set <EOL> return curry ( filtered_func , self , name ) <EOL> class AirySite ( object ) : <EOL> """<STR_LIT>""" <EOL> connections = ConnectionSet ( ) <EOL> application = None <EOL> loader = None <EOL> def resolve_url ( self , url , connection , arguments = { } ) : <EOL> handler = None <EOL> args = [ ] <EOL> kwargs = { } <EOL> for pattern , handlers in self . application . handlers : <EOL> for spec in handlers : <EOL> if issubclass ( spec . handler_class , AiryHandler ) : <EOL> match = spec . regex . match ( url ) <EOL> if match : <EOL> if spec . regex . groups : <EOL> def unquote ( s ) : <EOL> if s is None : return s <EOL> return escape . url_unescape ( s , encoding = None ) <EOL> if spec . regex . groupindex : <EOL> kwargs = dict ( <EOL> ( k , unquote ( v ) ) <EOL> for ( k , v ) in match . groupdict ( ) . iteritems ( ) ) <EOL> else : <EOL> args = [ unquote ( s ) for s in match . groups ( ) ] <EOL> handler = spec . handler_class ( self , connection , arguments , ** spec . kwargs ) <EOL> break <EOL> return handler , args , kwargs <EOL> site = AirySite ( ) <EOL> class AiryRequestHandler ( RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def _flatten_arguments ( self , arguments ) : <EOL> data = { } <EOL> for key , value in arguments . items ( ) : <EOL> if len ( value ) > <NUM_LIT:1> : <EOL> data [ key ] = value <EOL> else : <EOL> data [ key ] = value [ <NUM_LIT:0> ] <EOL> return data <EOL> def get_flat_arguments ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _flatten_arguments ( self . request . arguments ) <EOL> def get_current_user ( self ) : <EOL> """<STR_LIT>""" <EOL> for backend_name in getattr ( settings , '<STR_LIT>' , [ ] ) : <EOL> backend = __import__ ( backend_name , fromlist = [ backend_name ] ) <EOL> user = backend . get_current_user ( self ) <EOL> if user : <EOL> return user <EOL> return None <EOL> def get_cookie ( self , name , default = None ) : <EOL> """<STR_LIT>""" <EOL> if self . cookies is not None and name in self . cookies : <EOL> return unquote ( self . cookies [ name ] . value ) <EOL> return default <EOL> def render_string ( self , template_name , ** kwargs ) : <EOL> context_processors = getattr ( settings , '<STR_LIT>' , [ ] ) <EOL> template_args = { '<STR_LIT>' : self . reverse_url , "<STR_LIT>" : utf8 } <EOL> for processor_path in context_processors : <EOL> path , name = processor_path . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> processor_module = __import__ ( path , fromlist = path ) <EOL> processor = getattr ( processor_module , name ) <EOL> template_args . update ( processor ( self , ** kwargs ) ) <EOL> template_args . update ( kwargs ) <EOL> return super ( AiryRequestHandler , self ) . render_string ( template_name , ** template_args ) <EOL> def is_robot ( self , * args , ** kwargs ) : <EOL> robots = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> robots . extend ( args ) <EOL> for robot in robots : <EOL> if robot . lower ( ) in self . request . headers [ '<STR_LIT>' ] . lower ( ) : <EOL> return True <EOL> return False <EOL> class AiryHandler ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , site , connection , arguments = { } , ** kwargs ) : <EOL> self . site = site <EOL> self . application = self . site . application <EOL> self . connection = connection <EOL> self . connections = self . site . connections <EOL> self . arguments = { } <EOL> self . files = { } <EOL> for k , values in arguments . iteritems ( ) : <EOL> is_file_content = False <EOL> for v in values : <EOL> if isinstance ( v , dict ) and '<STR_LIT:name>' in v and '<STR_LIT:content>' in v : <EOL> try : <EOL> mimetype , body = v [ '<STR_LIT:content>' ] . split ( '<STR_LIT:;>' , <NUM_LIT:1> ) <EOL> encoding , file_content = body . split ( '<STR_LIT:U+002C>' , <NUM_LIT:1> ) <EOL> if encoding == '<STR_LIT>' : <EOL> file_content = base64 . b64decode ( file_content ) <EOL> else : <EOL> raise RuntimeError ( '<STR_LIT>' % ( encoding , k ) ) <EOL> self . files [ k ] = SimpleUploadedFile ( v [ '<STR_LIT:name>' ] , file_content , mimetype ) <EOL> is_file_content = True <EOL> except Exception , e : <EOL> logging . error ( "<STR_LIT>" % k ) <EOL> continue <EOL> if not is_file_content : <EOL> self . arguments [ k ] = values <EOL> for k , v in kwargs . iteritems ( ) : <EOL> setattr ( self , k , v ) <EOL> _ARG_DEFAULT = [ ] <EOL> def get_argument ( self , name , default = _ARG_DEFAULT , strip = True ) : <EOL> """<STR_LIT>""" <EOL> args = self . get_arguments ( name , strip = strip ) <EOL> if not args : <EOL> if default is self . _ARG_DEFAULT : <EOL> raise HTTPError ( <NUM_LIT> , "<STR_LIT>" % name ) <EOL> return default <EOL> return args [ - <NUM_LIT:1> ] <EOL> def get_arguments ( self , name , strip = True ) : <EOL> """<STR_LIT>""" <EOL> values = [ ] <EOL> for v in self . arguments . get ( name , [ ] ) : <EOL> v = self . decode_argument ( v , name = name ) <EOL> if isinstance ( v , unicode ) : <EOL> v = re . sub ( r"<STR_LIT>" , "<STR_LIT:U+0020>" , v ) <EOL> if strip and ( isinstance ( v , unicode ) or isinstance ( v , str ) ) : <EOL> v = v . strip ( ) <EOL> values . append ( v ) <EOL> return values <EOL> def _flatten_arguments ( self , arguments ) : <EOL> data = { } <EOL> for key , value in arguments . items ( ) : <EOL> if isinstance ( value , list ) : <EOL> if len ( value ) == <NUM_LIT:0> : <EOL> data [ key ] = None <EOL> elif len ( value ) == <NUM_LIT:1> : <EOL> data [ key ] = value [ <NUM_LIT:0> ] <EOL> else : <EOL> data [ key ] = value <EOL> else : <EOL> data [ key ] = value <EOL> return data <EOL> def get_flat_arguments ( self ) : <EOL> arguments = { } <EOL> for key in self . arguments : <EOL> arguments [ key ] = self . get_arguments ( key ) <EOL> return self . _flatten_arguments ( arguments ) <EOL> def get_files ( self ) : <EOL> return self . files <EOL> def decode_argument ( self , value , name = None ) : <EOL> """<STR_LIT>""" <EOL> return to_unicode ( value ) <EOL> def __getattr__ ( self , item ) : <EOL> return getattr ( self . connection , item ) <EOL> class AiryCoreHandler ( SocketConnection ) : <EOL> "<STR_LIT>" <EOL> state = '<STR_LIT:/>' <EOL> def on_open ( self , info ) : <EOL> self . info = info <EOL> self . site = site <EOL> self . application = site . application <EOL> self . connections = self . site . connections <EOL> self . ui = UIProxy ( self ) <EOL> logging . info ( '<STR_LIT>' % ( self . info . ip , self . info . arguments . get ( '<STR_LIT:t>' , '<STR_LIT>' ) ) ) <EOL> site . connections . add ( self ) <EOL> @ event ( '<STR_LIT>' ) <EOL> def set_state ( self , state ) : <EOL> ( scheme , host , path , arguments ) = self . parse_url ( state ) <EOL> self . state = path <EOL> @ event ( '<STR_LIT>' ) <EOL> @ report_on_fail <EOL> def get ( self , url , * args , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> ( scheme , host , path , arguments ) = self . parse_url ( url ) <EOL> handler , hargs , hkwargs = site . resolve_url ( path , self , arguments = arguments ) <EOL> hargs . extend ( args ) <EOL> hkwargs . update ( kwargs ) <EOL> if handler : <EOL> logging . info ( '<STR_LIT>' % ( path , self . info . ip , <EOL> self . info . arguments . get ( '<STR_LIT:t>' , '<STR_LIT>' ) , <EOL> handler . __class__ . __name__ ) ) <EOL> try : <EOL> return handler . get ( * hargs , ** hkwargs ) <EOL> except Http404 : <EOL> self . process_404 ( * hargs , ** hkwargs ) <EOL> else : <EOL> logging . error ( '<STR_LIT>' % ( path , self . info . ip , self . info . arguments . get ( '<STR_LIT:t>' , '<STR_LIT>' ) ) ) <EOL> self . process_404 ( * hargs , ** hkwargs ) <EOL> @ event ( '<STR_LIT>' ) <EOL> @ report_on_fail <EOL> def post ( self , url , * args , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> ( scheme , host , path , arguments ) = self . parse_url ( url ) <EOL> args = list ( args ) <EOL> try : <EOL> data = args . pop ( <NUM_LIT:0> ) <EOL> for k , v in dict ( data ) . items ( ) : <EOL> if not isinstance ( v , list ) : <EOL> data [ k ] = [ v ] <EOL> except : <EOL> data = { } <EOL> arguments . update ( data ) <EOL> handler , hargs , hkwargs = site . resolve_url ( path , self , arguments = arguments ) <EOL> hargs . extend ( args ) <EOL> hkwargs . update ( kwargs ) <EOL> if handler : <EOL> logging . info ( '<STR_LIT>' % ( <EOL> path , self . info . ip , self . info . arguments . get ( '<STR_LIT:t>' , '<STR_LIT>' ) , handler . __class__ . __name__ ) <EOL> ) <EOL> try : <EOL> return handler . post ( * hargs , ** hkwargs ) <EOL> except Http404 : <EOL> self . process_404 ( * hargs , ** hkwargs ) <EOL> else : <EOL> logging . error ( '<STR_LIT>' % ( path , self . info . ip , self . info . arguments . get ( '<STR_LIT:t>' , '<STR_LIT>' ) ) ) <EOL> self . process_404 ( * hargs , ** hkwargs ) <EOL> def on_close ( self ) : <EOL> logging . info ( '<STR_LIT>' % ( self . info . ip , self . info . arguments . get ( '<STR_LIT:t>' , '<STR_LIT>' ) ) ) <EOL> self . site . connections . remove ( self ) <EOL> @ property <EOL> def cookies ( self ) : <EOL> return self . info . cookies <EOL> def get_cookie ( self , name , default = None ) : <EOL> """<STR_LIT>""" <EOL> if self . info . cookies is not None and name in self . info . cookies : <EOL> return unquote ( self . info . cookies [ name ] . value ) <EOL> return default <EOL> def set_cookie ( self , name , value , domain = None , expires = None , path = "<STR_LIT:/>" , <EOL> expires_days = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> name = escape . native_str ( name ) <EOL> value = escape . native_str ( value ) <EOL> if re . search ( r"<STR_LIT>" , name + value ) : <EOL> raise ValueError ( "<STR_LIT>" % ( name , value ) ) <EOL> options = { } <EOL> if domain : <EOL> options [ "<STR_LIT>" ] = domain <EOL> if expires_days is not None and not expires : <EOL> expires = datetime . datetime . utcnow ( ) + datetime . timedelta ( <EOL> days = expires_days ) <EOL> if expires : <EOL> options [ "<STR_LIT>" ] = calendar . timegm ( expires . utctimetuple ( ) ) <EOL> if path : <EOL> options [ "<STR_LIT:path>" ] = path <EOL> for k , v in kwargs . iteritems ( ) : <EOL> if k == '<STR_LIT>' : k = '<STR_LIT>' <EOL> options [ k ] = v <EOL> self . info . cookies [ name ] = value <EOL> self . ui . execute ( '<STR_LIT>' % ( name , value , json_encode ( options ) ) ) <EOL> def clear_cookie ( self , name , path = "<STR_LIT:/>" , domain = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> del self . cookies [ name ] <EOL> except KeyError : <EOL> pass <EOL> self . ui . execute ( '<STR_LIT>' % name ) <EOL> def clear_all_cookies ( self ) : <EOL> """<STR_LIT>""" <EOL> for name in self . info . cookies . iterkeys ( ) : <EOL> self . clear_cookie ( name ) <EOL> def set_secure_cookie ( self , name , value , expires_days = <NUM_LIT:30> , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . set_cookie ( name , self . create_signed_value ( name , value ) , <EOL> expires_days = expires_days , ** kwargs ) <EOL> def create_signed_value ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> self . require_setting ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return create_signed_value ( self . application . settings [ "<STR_LIT>" ] , <EOL> name , value ) <EOL> def get_secure_cookie ( self , name , value = None , max_age_days = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> self . require_setting ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if value is None : value = self . get_cookie ( name ) <EOL> return decode_signed_value ( self . application . settings [ "<STR_LIT>" ] , <EOL> name , value , max_age_days = max_age_days ) <EOL> _ARG_DEFAULT = [ ] <EOL> def get_argument ( self , name , default = _ARG_DEFAULT , strip = True ) : <EOL> """<STR_LIT>""" <EOL> args = self . get_arguments ( name , strip = strip ) <EOL> if not args : <EOL> if default is self . _ARG_DEFAULT : <EOL> raise HTTPError ( <NUM_LIT> , "<STR_LIT>" % name ) <EOL> return default <EOL> return args [ - <NUM_LIT:1> ] <EOL> def get_arguments ( self , name , strip = True ) : <EOL> """<STR_LIT>""" <EOL> values = [ ] <EOL> for v in self . info . arguments . get ( name , [ ] ) : <EOL> v = self . decode_argument ( v , name = name ) <EOL> if isinstance ( v , unicode ) : <EOL> v = re . sub ( r"<STR_LIT>" , "<STR_LIT:U+0020>" , v ) <EOL> if strip and ( isinstance ( v , unicode ) or isinstance ( v , str ) ) : <EOL> v = v . strip ( ) <EOL> values . append ( v ) <EOL> return values <EOL> def _flatten_arguments ( self , arguments ) : <EOL> data = { } <EOL> for key , value in arguments . items ( ) : <EOL> if isinstance ( value , list ) : <EOL> if len ( value ) == <NUM_LIT:0> : <EOL> data [ key ] = None <EOL> elif len ( value ) == <NUM_LIT:1> : <EOL> data [ key ] = value [ <NUM_LIT:0> ] <EOL> else : <EOL> data [ key ] = value <EOL> else : <EOL> data [ key ] = value <EOL> return data <EOL> def get_flat_arguments ( self ) : <EOL> arguments = { } <EOL> for key in self . info . arguments : <EOL> arguments [ key ] = self . get_arguments ( key ) <EOL> return self . _flatten_arguments ( arguments ) <EOL> def get_files ( self ) : <EOL> return self . files <EOL> def decode_argument ( self , value , name = None ) : <EOL> """<STR_LIT>""" <EOL> return to_unicode ( value ) <EOL> @ property <EOL> def current_user ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . _current_user = self . get_current_user ( ) <EOL> return self . _current_user <EOL> def get_current_user ( self ) : <EOL> for backend_name in getattr ( settings , '<STR_LIT>' , [ ] ) : <EOL> backend = __import__ ( backend_name , fromlist = [ backend_name ] ) <EOL> user = backend . get_current_user ( self ) <EOL> if user : <EOL> return user <EOL> return None <EOL> def require_setting ( self , name , feature = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if not self . site . application . settings . get ( name ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( name , feature ) ) <EOL> def get_login_url ( self ) : <EOL> """<STR_LIT>""" <EOL> self . require_setting ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return self . site . application . settings [ "<STR_LIT>" ] <EOL> def process_404 ( self , * args , ** kwargs ) : <EOL> try : <EOL> handler_path = settings . page_not_found_handler <EOL> path , name = handler_path . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> handler_module = __import__ ( path , fromlist = path ) <EOL> handler_class = getattr ( handler_module , name ) <EOL> handler_class ( site , self ) . get ( ) <EOL> except AttributeError : <EOL> logging . error ( '<STR_LIT>' % ( kwargs . get ( '<STR_LIT:url>' , '<STR_LIT>' ) , ) ) <EOL> def parse_url ( self , url ) : <EOL> "<STR_LIT>" <EOL> parsed = urlparse ( url ) <EOL> arguments = parse_qs ( parsed . query ) <EOL> return parsed . scheme , parsed . netloc , parsed . path , arguments <EOL> def reverse_url ( self , name , * args ) : <EOL> """<STR_LIT>""" <EOL> return self . application . reverse_url ( name , * args ) <EOL> def render_string ( self , template_name , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> context_processors = getattr ( settings , '<STR_LIT>' , [ ] ) <EOL> template_args = { '<STR_LIT>' : self . reverse_url } <EOL> for processor_path in context_processors : <EOL> path , name = processor_path . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> processor_module = __import__ ( path , fromlist = path ) <EOL> processor = getattr ( processor_module , name ) <EOL> template_args . update ( processor ( self , ** kwargs ) ) <EOL> template_args . update ( kwargs ) <EOL> html = self . site . loader . load ( template_name ) . generate ( ** template_args ) <EOL> return html <EOL> def __getattr__ ( self , item ) : <EOL> return getattr ( self . ui , item ) <EOL> class FormProcessor ( AiryRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def check_xsrf_cookie ( self ) : <EOL> pass <EOL> def post ( self , * args , ** kwargs ) : <EOL> args = self . get_flat_arguments ( ) <EOL> args [ '<STR_LIT>' ] = { } <EOL> for file in self . request . files : <EOL> if len ( self . request . files [ file ] ) > <NUM_LIT:1> : <EOL> for content in self . request . files [ file ] : <EOL> args [ '<STR_LIT>' ] [ file ] = { <EOL> '<STR_LIT:content>' : '<STR_LIT>' % ( content [ '<STR_LIT>' ] , '<STR_LIT>' , base64 . b64encode ( content [ '<STR_LIT:body>' ] ) ) , <EOL> '<STR_LIT:name>' : smart_unicode ( content [ '<STR_LIT:filename>' ] ) <EOL> } <EOL> else : <EOL> content = self . request . files [ file ] [ <NUM_LIT:0> ] <EOL> args [ '<STR_LIT>' ] [ file ] = { <EOL> '<STR_LIT:content>' : '<STR_LIT>' % ( content [ '<STR_LIT>' ] , '<STR_LIT>' , base64 . b64encode ( content [ '<STR_LIT:body>' ] ) ) , <EOL> '<STR_LIT:name>' : smart_unicode ( content [ '<STR_LIT:filename>' ] ) <EOL> } <EOL> self . write ( json_encode ( args ) ) <EOL> self . finish ( ) <EOL> def utf8 ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , _UTF8_TYPES ) : <EOL> return value <EOL> assert isinstance ( value , unicode ) <EOL> return smart_unicode ( value , encoding = "<STR_LIT:utf-8>" , errors = "<STR_LIT:ignore>" ) <EOL> _UTF8_TYPES = ( bytes , type ( None ) ) </s>
