<s> from gi . repository import Gtk , WebKit <EOL> import urlparse <EOL> import os <EOL> import tempfile <EOL> import mimetypes <EOL> import codecs <EOL> import re <EOL> from bs4 import BeautifulSoup <EOL> Gtk . init ( '<STR_LIT>' ) <EOL> class App ( object ) : <EOL> """<STR_LIT>""" <EOL> url_pattern = dict ( ) <EOL> debug = False <EOL> def __init__ ( self , module_path = None ) : <EOL> app_dir = os . path . abspath ( os . path . dirname ( module_path ) ) <EOL> window = Gtk . Window ( ) <EOL> window . set_title ( '<STR_LIT>' ) <EOL> webkit_web_view = WebKit . WebView ( ) <EOL> settings = webkit_web_view . get_settings ( ) <EOL> settings . set_property ( '<STR_LIT>' , True ) <EOL> settings . set_property ( '<STR_LIT>' , True ) <EOL> settings . set_property ( '<STR_LIT>' , '<STR_LIT:utf-8>' ) <EOL> window . set_default_size ( <NUM_LIT> , <NUM_LIT> ) <EOL> scrollWindow = Gtk . ScrolledWindow ( ) <EOL> scrollWindow . add ( webkit_web_view ) <EOL> window . add ( scrollWindow ) <EOL> window . connect ( '<STR_LIT>' , Gtk . main_quit ) <EOL> webkit_web_view . connect ( <EOL> '<STR_LIT>' , <EOL> self . on_notify_load_status ) <EOL> webkit_web_view . connect ( <EOL> '<STR_LIT>' , <EOL> self . on_web_view_resource_request_starting ) <EOL> webkit_web_view . connect ( <EOL> '<STR_LIT>' , <EOL> self . on_web_view_resource_response_received ) <EOL> webkit_web_view . connect ( <EOL> '<STR_LIT>' , <EOL> self . on_web_view_resource_load_finished ) <EOL> webkit_web_view . connect ( <EOL> '<STR_LIT>' , <EOL> self . on_navigation_policy_decision_requested ) <EOL> webkit_main_frame = webkit_web_view . get_main_frame ( ) <EOL> webkit_main_frame . connect ( <EOL> '<STR_LIT>' , <EOL> self . on_web_frame_resource_request_starting ) <EOL> webkit_main_frame . connect ( <EOL> '<STR_LIT>' , <EOL> self . on_web_frame_resource_response_received ) <EOL> webkit_main_frame . connect ( <EOL> '<STR_LIT>' , <EOL> self . on_web_frame_resource_load_finished ) <EOL> webkit_main_frame . connect ( <EOL> '<STR_LIT>' , <EOL> self . on_web_frame_resource_load_failed ) <EOL> window . show_all ( ) <EOL> self . window = window <EOL> self . webkit_web_view = webkit_web_view <EOL> self . webkit_main_frame = webkit_main_frame <EOL> self . app_dir = app_dir <EOL> def _url_map_to_function ( self , url ) : <EOL> match_list = list ( ) <EOL> for pattern in self . url_pattern : <EOL> m = re . match ( pattern , url ) <EOL> if m : <EOL> match_list . append ( m ) <EOL> if len ( match_list ) > <NUM_LIT:1> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> return None <EOL> try : <EOL> m = match_list [ <NUM_LIT:0> ] <EOL> except : <EOL> return None <EOL> args = list ( m . groups ( ) ) <EOL> kw = m . groupdict ( ) <EOL> for value in kw . values ( ) : <EOL> args . remove ( value ) <EOL> return self . url_pattern [ m . re . pattern ] ( * args , ** kw ) <EOL> def route ( self , pattern = None ) : <EOL> def decorator ( fn ) : <EOL> self . url_pattern [ pattern ] = fn <EOL> return fn <EOL> return decorator <EOL> def on_notify_load_status ( self , webkitView , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> status = webkitView . get_load_status ( ) <EOL> if status == status . FINISHED : <EOL> if self . debug is True : <EOL> print '<STR_LIT>' <EOL> def on_navigation_policy_decision_requested ( <EOL> self , <EOL> webkit_web_view , <EOL> webkit_web_frame , <EOL> webkit_network_request , <EOL> webkit_web_navigation_action , <EOL> webkit_web_policy_dicision ) : <EOL> if self . debug is True : <EOL> print '<STR_LIT>' <EOL> def on_web_view_resource_request_starting ( <EOL> self , <EOL> web_view , <EOL> web_frame , <EOL> web_resource , <EOL> network_request , <EOL> network_response = None ) : <EOL> if self . debug is True : <EOL> print '<STR_LIT>' <EOL> def on_web_view_resource_response_received ( <EOL> self , <EOL> web_view , <EOL> web_frame , <EOL> web_resource , <EOL> network_response , <EOL> * arg , ** kw ) : <EOL> if self . debug is True : <EOL> print '<STR_LIT>' <EOL> def on_web_view_resource_load_finished ( <EOL> self , <EOL> web_view , web_frame , web_resource , <EOL> * args , ** kw ) : <EOL> if self . debug is True : <EOL> print '<STR_LIT>' <EOL> def on_web_frame_resource_request_starting ( <EOL> self , <EOL> web_frame , <EOL> web_resource , <EOL> network_request , <EOL> network_response = None ) : <EOL> if self . debug is True : <EOL> print '<STR_LIT>' <EOL> url = urlparse . unquote ( network_request . get_uri ( ) ) <EOL> url = urlparse . urlparse ( url . decode ( '<STR_LIT:utf-8>' ) ) <EOL> if url . netloc == '<STR_LIT>' : <EOL> response = make_response ( self . _url_map_to_function ( url . path ) ) <EOL> if response : <EOL> ( content , mimetype ) = response <EOL> file_ext = mimetypes . guess_extension ( mimetype ) <EOL> tmp_file_path = tempfile . mkstemp ( suffix = file_ext ) [ <NUM_LIT:1> ] <EOL> f = codecs . open ( tmp_file_path , '<STR_LIT:w>' , encoding = '<STR_LIT:utf-8>' ) <EOL> f . write ( content ) <EOL> f . close ( ) <EOL> network_request . set_uri ( '<STR_LIT>' + tmp_file_path + '<STR_LIT>' ) <EOL> else : <EOL> url_path = re . sub ( self . app_dir , '<STR_LIT>' , url . path ) <EOL> splitted_path = url_path . split ( '<STR_LIT:/>' ) <EOL> if splitted_path [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> splitted_path . pop ( <NUM_LIT:1> ) <EOL> url_path = os . path . join ( * splitted_path ) <EOL> file_path = os . path . join ( self . app_dir , url_path ) <EOL> file_path = os . path . normcase ( file_path ) <EOL> file_path = os . path . normpath ( file_path ) <EOL> if not ( os . path . exists ( file_path ) ) : <EOL> raise Exception ( '<STR_LIT>' + file_path ) <EOL> network_request . set_uri ( '<STR_LIT>' + file_path ) <EOL> def on_web_frame_resource_response_received ( <EOL> self , <EOL> web_frame , <EOL> web_resource , <EOL> network_response , <EOL> * arg , ** kw ) : <EOL> if self . debug is True : <EOL> print '<STR_LIT>' <EOL> url = urlparse . urlparse ( network_response . get_uri ( ) ) <EOL> url = urlparse . urlparse ( url . path ) <EOL> query = urlparse . parse_qs ( url . query ) <EOL> if '<STR_LIT>' in query : <EOL> os . remove ( url . path ) <EOL> def on_web_frame_resource_load_finished ( <EOL> self , <EOL> web_frame , <EOL> web_resource , <EOL> * arg , ** kw ) : <EOL> if self . debug is True : <EOL> print '<STR_LIT>' <EOL> def on_web_frame_resource_load_failed ( <EOL> self , <EOL> web_frame , <EOL> web_resource , <EOL> * arg , ** kw ) : <EOL> if self . debug is True : <EOL> print '<STR_LIT>' <EOL> def _init_ui ( self ) : <EOL> """<STR_LIT>""" <EOL> ( content , mimetype ) = make_response ( self . _url_map_to_function ( '<STR_LIT:/>' ) ) <EOL> try : <EOL> beautifulsoup = BeautifulSoup ( content ) <EOL> self . window . set_title ( beautifulsoup . find ( '<STR_LIT:title>' ) . string ) <EOL> except : <EOL> pass <EOL> if self . debug is True : <EOL> print self . app_dir <EOL> self . webkit_web_view . load_string ( <EOL> content , <EOL> mime_type = mimetype , <EOL> encoding = '<STR_LIT:utf-8>' , <EOL> base_uri = '<STR_LIT:/>' , <EOL> ) <EOL> def run ( self ) : <EOL> self . _init_ui ( ) <EOL> Gtk . main ( ) <EOL> def make_response ( response ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( response , unicode ) or isinstance ( response , str ) : <EOL> response = ( response , '<STR_LIT>' ) <EOL> return response </s>
