Skip to content

Commit c6532d3

Browse files
committed
cefadvanced.py - Rebind example updated, now it only reloads
application modules, it checks it by module source file path.
1 parent 27c00b9 commit c6532d3

2 files changed

Lines changed: 29 additions & 19 deletions

File tree

cefexample/_changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
Version 0.42 not yet released.
2+
* Rebind/reload example added to cefadvanced.py
3+
14
Version 0.41 released on 2012-09-14.
25
* New options in [ApplicationSettings]: auto_detect_proxy_settings_enabled, pack_loading_disabled.
36
* You are allowed to bind all of object methods using [JavascriptBindings].!SetObject() (Issue 4).

cefexample/cefadvanced.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import os
1212
import imp
1313

14+
DEBUG = True
15+
1416
# TODO: creating popup windows from python.
1517
# TODO: creating modal windows from python.
1618
# TODO: allow to pack html/css/images to a zip and run content from this file, optionally allow to password protect this zip file (see WBEA implementation).
@@ -20,12 +22,10 @@ def CefAdvanced():
2022
# This hook does the following: in case of exception display it, write to error.log, shutdown CEF and exit application.
2123
sys.excepthook = cefpython.ExceptHook
2224

23-
# Whether to print debug output to console. It is advised to set this to True while developing application,
24-
# it is not only printing to output console, it also makes some additional error checking that may go
25-
# unnoticed otherwise, for example in debug mode it always checks whether V8 objects are created in
26-
# proper context, if created in wrong context it could result in application crash.
27-
cefpython.__debug = True
28-
cefwindow.__debug = True
25+
# Whether to print debug output to console.
26+
if DEBUG:
27+
cefpython.__debug = True
28+
cefwindow.__debug = True
2929

3030
# ApplicationSettings, see: http://code.google.com/p/cefpython/wiki/ApplicationSettings
3131
appSettings = dict()
@@ -62,10 +62,6 @@ def CefAdvanced():
6262
handlers["OnLoadError"] = clientHandler.OnLoadError
6363
handlers["OnKeyEvent"] = (clientHandler.OnKeyEvent, None, clientHandler.OnKeyEvent)
6464

65-
# If you want a way to rebind javascript functions later, for example combined with use of Python's reload()
66-
# on module, so that you can make changes to app without re-launching application, then see Issue 12
67-
# for an example on how to do this: http://code.google.com/p/cefpython/issues/detail?id=12 (test_noreload2.zip).
68-
6965
cefBindings = cefpython.JavascriptBindings(bindToFrames=False, bindToPopups=False)
7066
browser = cefpython.CreateBrowser(windowID, browserSettings, "cefadvanced.html", handlers, cefBindings)
7167

@@ -111,17 +107,26 @@ def Bind(self):
111107

112108
def Rebind(self):
113109

114-
# Reload all modules, next rebind javascript bindings.
110+
# Reload all application modules, next rebind javascript bindings.
115111
# Called from: OnKeyEvent > F5.
116112

113+
currentDir = cefpython.GetRealPath()
114+
117115
for mod in sys.modules.values():
118116
if mod and mod.__name__ != "__main__":
119-
try:
120-
imp.reload(mod)
121-
except:
122-
print("WARNING: reloading module failed: %s" % mod.__name__)
117+
if hasattr(mod, "__file__") and mod.__file__.find(currentDir) != -1: # this module resides in app's directory.
118+
try:
119+
imp.reload(mod)
120+
if DEBUG:
121+
print("Reloaded module: %s" % mod.__name__)
122+
except Exception, exc:
123+
print("WARNING: reloading module failed: %s. Exception: %s" % (mod.__name__, exc))
124+
125+
if DEBUG:
126+
# These modules have been reloaded, we need to set debug variables again.
127+
cefpython.__debug = True
128+
cefwindow.__debug = True
123129

124-
sys.excepthook = cefpython.ExceptHook # sys module was reloaded, need to set exception handler again.
125130
self.Bind()
126131
self.cefBindings.Rebind()
127132
# print("cefwindow.test=%s" % cefwindow.test)
@@ -233,15 +238,17 @@ class ClientHandler:
233238

234239
def OnLoadStart(self, browser, frame):
235240

236-
print("OnLoadStart(): frame URL: %s" % frame.GetURL())
241+
# print("OnLoadStart(): frame URL: %s" % frame.GetURL())
242+
pass
237243

238244
def OnLoadEnd(self, browser, frame, httpStatusCode):
239245

240-
print("OnLoadEnd(): frame URL: %s" % frame.GetURL())
246+
# print("OnLoadEnd(): frame URL: %s" % frame.GetURL())
247+
pass
241248

242249
def OnLoadError(self, browser, frame, errorCode, failedURL, errorText):
243250

244-
print("OnLoadError() failedURL: %s" % (failedURL))
251+
# print("OnLoadError() failedURL: %s" % (failedURL))
245252
errorText[0] = "Custom error message when loading URL fails, see: def OnLoadError()"
246253
return True
247254

0 commit comments

Comments
 (0)