<s> from __future__ import absolute_import <EOL> import abc <EOL> import json <EOL> import functools <EOL> import pprint <EOL> import six <EOL> from splash import defaults <EOL> from splash . browser_tab import BrowserTab <EOL> from splash . exceptions import RenderError <EOL> def stop_on_error ( meth ) : <EOL> @ functools . wraps ( meth ) <EOL> def stop_on_error_wrapper ( self , * args , ** kwargs ) : <EOL> try : <EOL> return meth ( self , * args , ** kwargs ) <EOL> except Exception as e : <EOL> self . return_error ( e ) <EOL> return stop_on_error_wrapper <EOL> class RenderScript ( six . with_metaclass ( abc . ABCMeta , object ) ) : <EOL> """<STR_LIT>""" <EOL> default_min_log_level = <NUM_LIT:2> <EOL> def __init__ ( self , network_manager , splash_proxy_factory , render_options , verbosity ) : <EOL> self . tab = BrowserTab ( <EOL> network_manager = network_manager , <EOL> splash_proxy_factory = splash_proxy_factory , <EOL> verbosity = verbosity , <EOL> render_options = render_options , <EOL> ) <EOL> self . render_options = render_options <EOL> self . verbosity = verbosity <EOL> self . deferred = self . tab . deferred <EOL> @ abc . abstractmethod <EOL> def start ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def log ( self , text , min_level = None ) : <EOL> if min_level is None : <EOL> min_level = self . default_min_log_level <EOL> self . tab . logger . log ( text , min_level = min_level ) <EOL> def return_result ( self , result ) : <EOL> self . tab . return_result ( result ) <EOL> def return_error ( self , error = None ) : <EOL> self . tab . return_error ( error ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . tab . close ( ) <EOL> class DefaultRenderScript ( RenderScript ) : <EOL> """<STR_LIT>""" <EOL> def start ( self , url , baseurl = None , wait = None , viewport = None , <EOL> js_source = None , js_profile = None , images = None , console = False , <EOL> headers = None , http_method = '<STR_LIT:GET>' , body = None , <EOL> render_all = False , resource_timeout = None ) : <EOL> self . url = url <EOL> self . wait_time = defaults . WAIT_TIME if wait is None else wait <EOL> self . js_source = js_source <EOL> self . js_profile = js_profile <EOL> self . console = console <EOL> self . viewport = defaults . VIEWPORT_SIZE if viewport is None else viewport <EOL> self . render_all = render_all or viewport == '<STR_LIT>' <EOL> if resource_timeout : <EOL> self . tab . set_resource_timeout ( resource_timeout ) <EOL> if images is not None : <EOL> self . tab . set_images_enabled ( images ) <EOL> if self . viewport != '<STR_LIT>' : <EOL> self . tab . set_viewport ( self . viewport ) <EOL> self . tab . go ( <EOL> url = url , <EOL> callback = self . on_goto_load_finished , <EOL> errback = self . on_goto_load_error , <EOL> baseurl = baseurl , <EOL> http_method = http_method , <EOL> body = body , <EOL> headers = headers , <EOL> ) <EOL> @ abc . abstractmethod <EOL> def get_result ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def on_goto_load_finished ( self ) : <EOL> if self . wait_time == <NUM_LIT:0> : <EOL> self . log ( "<STR_LIT>" ) <EOL> self . _loadFinishedOK ( ) <EOL> else : <EOL> time_ms = int ( self . wait_time * <NUM_LIT:1000> ) <EOL> self . log ( "<STR_LIT>" % time_ms ) <EOL> self . tab . wait ( <EOL> time_ms = time_ms , <EOL> callback = self . _loadFinishedOK , <EOL> onerror = self . on_goto_load_error , <EOL> ) <EOL> def on_goto_load_error ( self , error_info ) : <EOL> ex = RenderError ( { <EOL> '<STR_LIT:type>' : error_info . type , <EOL> '<STR_LIT:code>' : error_info . code , <EOL> '<STR_LIT:text>' : error_info . text , <EOL> '<STR_LIT:url>' : error_info . url <EOL> } ) <EOL> self . return_error ( ex ) <EOL> @ stop_on_error <EOL> def _loadFinishedOK ( self ) : <EOL> self . log ( "<STR_LIT>" ) <EOL> if self . tab . _closing : <EOL> self . log ( "<STR_LIT>" , min_level = <NUM_LIT:3> ) <EOL> return <EOL> self . tab . stop_loading ( ) <EOL> self . tab . store_har_timing ( "<STR_LIT>" ) <EOL> self . _prepare_render ( ) <EOL> self . return_result ( self . get_result ( ) ) <EOL> def _runjs ( self , js_source , js_profile ) : <EOL> js_output , js_console_output = None , None <EOL> if js_source or js_profile : <EOL> if self . console : <EOL> self . tab . _jsconsole_enable ( ) <EOL> if js_profile : <EOL> self . tab . run_js_files ( js_profile , handle_errors = False ) <EOL> if js_source : <EOL> js_output = self . tab . evaljs ( js_source , handle_errors = False ) <EOL> if self . console : <EOL> js_console_output = self . tab . _jsconsole_messages ( ) <EOL> self . tab . store_har_timing ( '<STR_LIT>' ) <EOL> return js_output , js_console_output <EOL> def _prepare_render ( self ) : <EOL> if self . viewport == '<STR_LIT>' : <EOL> self . tab . set_viewport ( self . viewport ) <EOL> self . js_output , self . js_console_output = self . _runjs ( self . js_source , self . js_profile ) <EOL> class HtmlRender ( DefaultRenderScript ) : <EOL> def get_result ( self ) : <EOL> return self . tab . html ( ) <EOL> class ImageRender ( DefaultRenderScript ) : <EOL> def start ( self , ** kwargs ) : <EOL> self . width = kwargs . pop ( '<STR_LIT:width>' ) <EOL> self . height = kwargs . pop ( '<STR_LIT>' ) <EOL> self . scale_method = kwargs . pop ( '<STR_LIT>' ) <EOL> return super ( ImageRender , self ) . start ( ** kwargs ) <EOL> class PngRender ( ImageRender ) : <EOL> def get_result ( self ) : <EOL> return self . tab . png ( self . width , self . height , <EOL> render_all = self . render_all , <EOL> scale_method = self . scale_method ) <EOL> class JpegRender ( ImageRender ) : <EOL> def start ( self , ** kwargs ) : <EOL> self . quality = kwargs . pop ( '<STR_LIT>' ) <EOL> return super ( JpegRender , self ) . start ( ** kwargs ) <EOL> def get_result ( self ) : <EOL> return self . tab . jpeg ( <EOL> self . width , self . height , render_all = self . render_all , <EOL> scale_method = self . scale_method , quality = self . quality ) <EOL> class JsonRender ( JpegRender ) : <EOL> def start ( self , ** kwargs ) : <EOL> self . include = { <EOL> inc : kwargs . pop ( inc ) <EOL> for inc in [ '<STR_LIT:html>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> } <EOL> self . include [ '<STR_LIT>' ] = kwargs . get ( '<STR_LIT>' ) <EOL> super ( JsonRender , self ) . start ( ** kwargs ) <EOL> def get_result ( self ) : <EOL> res = { } <EOL> if self . include [ '<STR_LIT>' ] : <EOL> res [ '<STR_LIT>' ] = self . tab . png ( self . width , self . height , b64 = True , <EOL> render_all = self . render_all , <EOL> scale_method = self . scale_method ) <EOL> if self . include [ '<STR_LIT>' ] : <EOL> res [ '<STR_LIT>' ] = self . tab . jpeg ( self . width , self . height , b64 = True , <EOL> render_all = self . render_all , <EOL> scale_method = self . scale_method , <EOL> quality = self . quality ) <EOL> if self . include [ '<STR_LIT>' ] and self . js_output : <EOL> res [ '<STR_LIT>' ] = self . js_output <EOL> if self . include [ '<STR_LIT>' ] and self . js_console_output : <EOL> res [ '<STR_LIT>' ] = self . js_console_output <EOL> res . update ( self . tab . iframes_info ( <EOL> children = self . include [ '<STR_LIT>' ] , <EOL> html = self . include [ '<STR_LIT:html>' ] , <EOL> ) ) <EOL> if self . include [ '<STR_LIT>' ] : <EOL> res [ '<STR_LIT>' ] = self . tab . history ( ) <EOL> if self . include [ '<STR_LIT>' ] : <EOL> res [ '<STR_LIT>' ] = self . tab . har ( ) <EOL> return res <EOL> class HarRender ( DefaultRenderScript ) : <EOL> def get_result ( self ) : <EOL> return json . dumps ( self . tab . har ( ) ) </s>
