33
44#--------------------------------------------------------------------------------
55
6- import platform
7- if platform .architecture ()[0 ] != "32bit" :
8- raise Exception ("Only 32bit architecture is supported" )
9-
10- import os
11- import sys
12- try :
13- # Import local PYD file (portable zip).
14- if sys .hexversion >= 0x02070000 and sys .hexversion < 0x03000000 :
15- import cefpython_py27 as cefpython
16- elif sys .hexversion >= 0x03000000 and sys .hexversion < 0x04000000 :
17- import cefpython_py32 as cefpython
18- else :
19- raise Exception ("Unsupported python version: %s" % sys .version )
20- except ImportError :
21- # Import from package (installer).
22- from cefpython1 import cefpython
23-
6+ from cefpython1 import cefpython
7+ from cefpython1 .wx .utils import ExceptHook
8+ import os , sys , platform
249import wx
2510import wx .lib .buttons as buttons
2611
27- from cefpython1 .wx .utils import GetApplicationPath
28-
2912#-------------------------------------------------------------------------------
3013
3114# Default timer interval when timer used to service CEF message loop
@@ -111,14 +94,35 @@ def __init__(self, parent, url="", useTimer=False,
11194 * args , ** kwargs ):
11295 wx .Window .__init__ (self , parent , id = wx .ID_ANY , size = size ,
11396 * args , ** kwargs )
97+ # On Linux absolute file urls need to start with "file://"
98+ # otherwise a path of "/home/some" is converted to "http://home/some".
99+ if platform .system () == "Linux" :
100+ if url .startswith ("/" ):
101+ url = "file://" + url
114102 self .url = url
103+
115104 windowInfo = cefpython .WindowInfo ()
116- windowInfo .SetAsChild (self .GetHandle ())
105+ if platform .system () == "Windows" :
106+ windowInfo .SetAsChild (self .GetHandle ())
107+ elif platform .system () == "Linux" :
108+ windowInfo .SetAsChild (self .GetGtkWidget ())
109+ else :
110+ raise Exception ("Unsupported OS" )
111+
112+ # TODO: allow for custom browser settings for the ChromeWindow
113+ browserSettings = {}
114+ if platform .system () == "Linux" :
115+ # Disable plugins on Linux as Flash will crash the application
116+ # in CEF 1 on Linux, it's a known problem.
117+ if not "plugins_disabled" in browserSettings :
118+ browserSettings ["plugins_disabled" ] = True
119+
117120 self .browser = cefpython .CreateBrowserSync (windowInfo ,
118- browserSettings = {} , navigateUrl = url )
121+ browserSettings = browserSettings , navigateUrl = url )
119122
120- self .Bind (wx .EVT_SET_FOCUS , self .OnSetFocus )
121- self .Bind (wx .EVT_SIZE , self .OnSize )
123+ if platform .system () == "Windows" :
124+ self .Bind (wx .EVT_SET_FOCUS , self .OnSetFocus )
125+ self .Bind (wx .EVT_SIZE , self .OnSize )
122126 if useTimer :
123127 self .timerID = 1
124128 self ._CreateTimer (timerMillis )
@@ -149,11 +153,12 @@ def OnIdle(self, event):
149153 event .Skip ()
150154
151155 def OnSetFocus (self , event ):
156+ """OS_WIN only."""
152157 cefpython .WindowUtils .OnSetFocus (self .GetHandle (), 0 , 0 , 0 )
153158 event .Skip ()
154159
155160 def OnSize (self , event ):
156- """Handle the the size event"""
161+ """OS_WIN only. Handle the the size event"""
157162 cefpython .WindowUtils .OnSize (self .GetHandle (), 0 , 0 , 0 )
158163 event .Skip ()
159164
@@ -246,6 +251,8 @@ def OnLoadStart(self, browser, frame):
246251 browser .GetMainFrame ().GetUrl ())
247252 self .navigationBar .AddToHistory (browser .GetMainFrame ().GetUrl ())
248253
254+ def OnLoadEnd (self , browser , frame , httpStatusCode ):
255+ pass
249256
250257class DefaultClientHandler (object ):
251258 def __init__ (self , parentCtrl ):
@@ -259,7 +266,7 @@ def OnLoadEnd(self, browser, frame, httpStatusCode):
259266
260267 def OnLoadError (self , browser , frame , errorCode , failedUrl , errorText ):
261268 # TODO
262- print "ERROR LOADING URL : %" % failedUrl
269+ print ( "ERROR LOADING URL : %s " % failedUrl )
263270
264271class CallbackClientHandler (object ):
265272 def __init__ (self , onLoadStart = None , onLoadEnd = None ):
@@ -276,7 +283,7 @@ def OnLoadEnd(self, browser, frame, httpStatusCode):
276283
277284 def OnLoadError (self , browser , frame , errorCode , failedUrl , errorText ):
278285 # TODO
279- print "ERROR LOADING URL : %" % failedUrl
286+ print ( "ERROR LOADING URL : %s " % failedUrl )
280287
281288#-------------------------------------------------------------------------------
282289
@@ -286,26 +293,21 @@ def Initialize(settings=None):
286293 """
287294 sys .excepthook = ExceptHook
288295 if not settings :
289- settings = {
290- "log_severity" : cefpython .LOGSEVERITY_INFO ,
291- "log_file" : GetApplicationPath ("debug.log" ),
292- "release_dcheck_enabled" : True # Enable only when debugging.
293- }
296+ settings = {}
297+ if not "log_severity" in settings :
298+ settings ["log_severity" ] = cefpython .LOGSEVERITY_INFO
299+ if not "log_file" in settings :
300+ settings ["log_file" ] = ""
301+ if platform .system () == "Linux" :
302+ # On Linux we need to set locales and resources directories.
303+ if not "locales_dir_path" in settings :
304+ settings ["locales_dir_path" ] = cefpython .GetModuleDirectory ()+ (
305+ "/locales" )
306+ if not "resources_dir_path" in settings :
307+ settings ["resources_dir_path" ] = cefpython .GetModuleDirectory ()
308+
294309 cefpython .Initialize (settings )
295310
296311def Shutdown ():
297312 """Shuts down CEF, should be called by app exiting code"""
298313 cefpython .Shutdown ()
299-
300- def ExceptHook (t , value , traceObject ):
301- import traceback , os , time
302- # This hook does the following: in case of exception display it,
303- # write to error.log, shutdown CEF and exit application.
304- error = "\n " .join (traceback .format_exception (t , value , traceObject ))
305- with open (GetApplicationPath ("error.log" ), "a" ) as f :
306- f .write ("\n [%s] %s\n " % (time .strftime ("%Y-%m-%d %H:%M:%S" ), error ))
307- print ("\n " + error + "\n " )
308- ##cefpython.QuitMessageLoop()
309- ##cefpython.Shutdown()
310- # So that "finally" does not execute.
311- ##os._exit(1)
0 commit comments