<s> from types import FunctionType <EOL> import tornado . web <EOL> from tornado . util import unicode_type <EOL> try : <EOL> import urlparse <EOL> except ImportError : <EOL> import urllib . parse as urlparse <EOL> try : <EOL> from urllib import urlencode <EOL> except ImportError : <EOL> from urllib . parse import urlencode <EOL> def decorate_all ( decorator_list ) : <EOL> def is_method_need_to_decorate ( func_name , func_obj , check_param ) : <EOL> """<STR_LIT>""" <EOL> methods = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> return ( func_name in methods and <EOL> isinstance ( func_obj , FunctionType ) and <EOL> getattr ( func_obj , check_param , True ) ) <EOL> """<STR_LIT>""" <EOL> class DecorateAll ( type ) : <EOL> def __new__ ( cls , name , bases , dct ) : <EOL> for func_name , func_obj in dct . items ( ) : <EOL> for item in decorator_list : <EOL> decorator , check_param = item <EOL> if is_method_need_to_decorate ( func_name , func_obj , check_param ) : <EOL> dct [ func_name ] = decorator ( dct [ func_name ] ) <EOL> return super ( DecorateAll , cls ) . __new__ ( cls , name , bases , dct ) <EOL> def __setattr__ ( self , func_name , func_obj ) : <EOL> for item in decorator_list : <EOL> decorator , check_param = item <EOL> if is_method_need_to_decorate ( func_name , func_obj , check_param ) : <EOL> func_obj = decorator ( func_obj ) <EOL> super ( DecorateAll , self ) . __setattr__ ( func_name , func_obj ) <EOL> return DecorateAll <EOL> def make_list ( val ) : <EOL> if isinstance ( val , list ) : <EOL> return val <EOL> else : <EOL> return [ val ] <EOL> def real_ip ( request ) : <EOL> return ( request . headers . get ( '<STR_LIT>' , None ) or request . headers . get ( '<STR_LIT>' , None ) or <EOL> request . remote_ip or '<STR_LIT:127.0.0.1>' ) <EOL> HTTPError = tornado . web . HTTPError <EOL> ITERABLE = ( set , frozenset , list , tuple ) <EOL> def update_url ( url , update_args = None , remove_args = None ) : <EOL> scheme , sep , url_new = url . partition ( '<STR_LIT>' ) <EOL> if len ( scheme ) == len ( url ) : <EOL> scheme = '<STR_LIT>' <EOL> else : <EOL> url = '<STR_LIT>' + url_new <EOL> url_split = urlparse . urlsplit ( url ) <EOL> query_dict = urlparse . parse_qs ( url_split . query , keep_blank_values = True ) <EOL> if update_args : <EOL> query_dict . update ( update_args ) <EOL> if remove_args : <EOL> query_dict = dict ( [ ( k , query_dict . get ( k ) ) for k in query_dict if k not in remove_args ] ) <EOL> query = make_qs ( query_dict ) <EOL> return urlparse . urlunsplit ( ( scheme , url_split . netloc , url_split . path , query , url_split . fragment ) ) <EOL> def make_qs ( query_args ) : <EOL> def _encode ( s ) : <EOL> if isinstance ( s , unicode_type ) : <EOL> return s . encode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> return s <EOL> kv_pairs = [ ] <EOL> for key , val in query_args . items ( ) : <EOL> if val is not None : <EOL> encoded_key = _encode ( key ) <EOL> if isinstance ( val , ITERABLE ) : <EOL> for v in val : <EOL> kv_pairs . append ( ( encoded_key , _encode ( v ) ) ) <EOL> else : <EOL> kv_pairs . append ( ( encoded_key , _encode ( val ) ) ) <EOL> qs = urlencode ( kv_pairs , doseq = True ) <EOL> return qs </s>
