<s> from __future__ import ( absolute_import , unicode_literals ) <EOL> """<STR_LIT>""" <EOL> from builtins import object <EOL> from . . helper import json <EOL> from . . exception import SijaxError <EOL> from types import GeneratorType <EOL> from functools import partial <EOL> class BaseResponse ( object ) : <EOL> """<STR_LIT>""" <EOL> COMMAND_ALERT = '<STR_LIT>' <EOL> COMMAND_HTML = '<STR_LIT:html>' <EOL> COMMAND_SCRIPT = '<STR_LIT>' <EOL> COMMAND_ATTR = '<STR_LIT>' <EOL> COMMAND_CSS = '<STR_LIT>' <EOL> COMMAND_REMOVE = '<STR_LIT>' <EOL> COMMAND_CALL = '<STR_LIT>' <EOL> JSON_AS_ASCII = False <EOL> JSON_SEPARATORS = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) <EOL> JSON_INDENT = None <EOL> JSON_SORT_KEYS = False <EOL> def __init__ ( self , sijax_instance , request_args ) : <EOL> """<STR_LIT>""" <EOL> self . _commands = [ ] <EOL> self . _sijax = sijax_instance <EOL> self . _request_args = request_args <EOL> self . dumps = partial ( json . dumps , ensure_ascii = self . JSON_AS_ASCII , <EOL> separators = self . JSON_SEPARATORS , indent = self . JSON_INDENT , <EOL> sort_keys = self . JSON_SORT_KEYS ) <EOL> def _get_request_args ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _request_args <EOL> def _add_command ( self , cmd_type , params = None ) : <EOL> """<STR_LIT>""" <EOL> if params is None : <EOL> params = { } <EOL> params [ '<STR_LIT:type>' ] = cmd_type <EOL> self . _commands . append ( params ) <EOL> return self <EOL> def clear_commands ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _commands = [ ] <EOL> return self <EOL> def alert ( self , message ) : <EOL> """<STR_LIT>""" <EOL> params = { self . __class__ . COMMAND_ALERT : message } <EOL> return self . _add_command ( self . __class__ . COMMAND_ALERT , params ) <EOL> def _html ( self , selector , html , set_type ) : <EOL> params = { '<STR_LIT>' : selector , '<STR_LIT:html>' : html , '<STR_LIT>' : set_type } <EOL> return self . _add_command ( self . __class__ . COMMAND_HTML , params ) <EOL> def html ( self , selector , html ) : <EOL> """<STR_LIT>""" <EOL> return self . _html ( selector , html , '<STR_LIT:replace>' ) <EOL> def html_append ( self , selector , html ) : <EOL> """<STR_LIT>""" <EOL> return self . _html ( selector , html , "<STR_LIT>" ) <EOL> def html_prepend ( self , selector , html ) : <EOL> """<STR_LIT>""" <EOL> return self . _html ( selector , html , '<STR_LIT>' ) <EOL> def script ( self , js ) : <EOL> """<STR_LIT>""" <EOL> params = { self . __class__ . COMMAND_SCRIPT : js } <EOL> return self . _add_command ( self . __class__ . COMMAND_SCRIPT , params ) <EOL> def css ( self , selector , property_name , value ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : selector , '<STR_LIT:key>' : property_name , '<STR_LIT:value>' : value } <EOL> return self . _add_command ( self . __class__ . COMMAND_CSS , params ) <EOL> def _attr ( self , selector , property_name , value , set_type ) : <EOL> params = { <EOL> '<STR_LIT>' : selector , '<STR_LIT:key>' : property_name , <EOL> '<STR_LIT:value>' : value , '<STR_LIT>' : set_type <EOL> } <EOL> return self . _add_command ( self . __class__ . COMMAND_ATTR , params ) <EOL> def attr ( self , selector , property_name , value ) : <EOL> """<STR_LIT>""" <EOL> return self . _attr ( selector , property_name , value , '<STR_LIT:replace>' ) <EOL> def attr_append ( self , selector , property_name , value ) : <EOL> """<STR_LIT>""" <EOL> return self . _attr ( selector , property_name , value , '<STR_LIT>' ) <EOL> def attr_prepend ( self , selector , property_name , value ) : <EOL> """<STR_LIT>""" <EOL> return self . _attr ( selector , property_name , value , '<STR_LIT>' ) <EOL> def remove ( self , selector ) : <EOL> """<STR_LIT>""" <EOL> params = { self . __class__ . COMMAND_REMOVE : selector } <EOL> return self . _add_command ( self . __class__ . COMMAND_REMOVE , params ) <EOL> def redirect ( self , uri ) : <EOL> """<STR_LIT>""" <EOL> return self . script ( '<STR_LIT>' % self . dumps ( uri ) ) <EOL> def call ( self , js_func_name , func_params = None ) : <EOL> """<STR_LIT>""" <EOL> if func_params is None : <EOL> func_params = [ ] <EOL> if ( not isinstance ( func_params , list ) and <EOL> not isinstance ( func_params , tuple ) ) : <EOL> raise SijaxError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> params = { <EOL> self . __class__ . COMMAND_CALL : js_func_name , <EOL> '<STR_LIT>' : func_params <EOL> } <EOL> return self . _add_command ( self . __class__ . COMMAND_CALL , params ) <EOL> def _get_json ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . dumps ( self . _commands ) <EOL> def _perform_handler_call ( self , callback , args ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return callback ( self , * args ) <EOL> except TypeError : <EOL> import sys , traceback <EOL> stack_entries = traceback . extract_tb ( sys . exc_info ( ) [ <NUM_LIT:2> ] ) <EOL> if len ( stack_entries ) != <NUM_LIT:1> : <EOL> raise <EOL> evt_invalid_call = self . _sijax . __class__ . EVENT_INVALID_CALL <EOL> return self . _sijax . get_event ( evt_invalid_call ) ( self , callback ) <EOL> def _process_callback ( self , callback , args ) : <EOL> """<STR_LIT>""" <EOL> response = self . _perform_handler_call ( callback , args ) <EOL> if isinstance ( response , GeneratorType ) : <EOL> raise SijaxError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def _process_call_chain ( self , call_chain ) : <EOL> """<STR_LIT>""" <EOL> for callback , args in call_chain : <EOL> self . _process_callback ( callback , args ) <EOL> return self . _get_json ( ) </s>
