Repository navigation
Expand file tree
/
Copy pathwxpython.py
More file actions
431 lines (378 loc) · 16.6 KB
/
Copy pathwxpython.py
File metadata and controls
431 lines (378 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# An example of embedding CEF browser in wxPython on Linux.
import ctypes, os, sys
libcef_so = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'libcef.so')
if os.path.exists(libcef_so):
# Import local module
ctypes.CDLL(libcef_so, ctypes.RTLD_GLOBAL)
if 0x02070000 <= sys.hexversion < 0x03000000:
import cefpython_py27 as cefpython
else:
raise Exception("Unsupported python version: %s" % sys.version)
else:
# Import from package
from cefpython3 import cefpython
import wx
import time
import re
import uuid
import platform
# Which method to use for message loop processing.
# EVT_IDLE - wx application has priority (default)
# EVT_TIMER - cef browser has priority
# It seems that Flash content behaves better when using a timer.
# IMPORTANT! On Linux EVT_IDLE does not work, the events seems to
# be propagated only when you move your mouse, which is not the
# expected behavior, it is recommended to use EVT_TIMER on Linux,
# so set this value to False.
USE_EVT_IDLE = False
def GetApplicationPath(file=None):
import re, os, platform
# If file is None return current directory without trailing slash.
if file is None:
file = ""
# Only when relative path.
if not file.startswith("/") and not file.startswith("\\") and (
not re.search(r"^[\w-]+:", file)):
if hasattr(sys, "frozen"):
path = os.path.dirname(sys.executable)
elif "__file__" in globals():
path = os.path.dirname(os.path.realpath(__file__))
else:
path = os.getcwd()
path = path + os.sep + file
if platform.system() == "Windows":
path = re.sub(r"[/\\]+", re.escape(os.sep), path)
path = re.sub(r"[/\\]+$", "", path)
return path
return str(file)
def ExceptHook(excType, excValue, traceObject):
import traceback, os, time, codecs
# This hook does the following: in case of exception write it to
# the "error.log" file, display it to the console, shutdown CEF
# and exit application immediately by ignoring "finally" (os._exit()).
errorMsg = "\n".join(traceback.format_exception(excType, excValue,
traceObject))
errorFile = GetApplicationPath("error.log")
try:
appEncoding = cefpython.g_applicationSettings["string_encoding"]
except:
appEncoding = "utf-8"
if type(errorMsg) == bytes:
errorMsg = errorMsg.decode(encoding=appEncoding, errors="replace")
try:
with codecs.open(errorFile, mode="a", encoding=appEncoding) as fp:
fp.write("\n[%s] %s\n" % (
time.strftime("%Y-%m-%d %H:%M:%S"), errorMsg))
except:
print("cefpython: WARNING: failed writing to error file: %s" % (
errorFile))
# Convert error message to ascii before printing, otherwise
# you may get error like this:
# | UnicodeEncodeError: 'charmap' codec can't encode characters
errorMsg = errorMsg.encode("ascii", errors="replace")
errorMsg = errorMsg.decode("ascii", errors="replace")
print("\n"+errorMsg+"\n")
cefpython.QuitMessageLoop()
cefpython.Shutdown()
os._exit(1)
class MainFrame(wx.Frame):
browser = None
mainPanel = None
def __init__(self):
wx.Frame.__init__(self, parent=None, id=wx.ID_ANY,
title='wxPython CEF 3 example', size=(800,600))
self.CreateMenu()
# Cannot attach browser to the main frame as this will cause
# the menu not to work.
self.mainPanel = wx.Panel(self)
windowInfo = cefpython.WindowInfo()
windowInfo.SetAsChild(self.mainPanel.GetGtkWidget())
# Linux requires adding "file://" for local files,
# otherwise /home/some will be replaced as http://home/some
self.browser = cefpython.CreateBrowserSync(
windowInfo,
# If there are problems with Flash you can disable it here,
# by disabling all plugins.
browserSettings={"plugins_disabled": False},
navigateUrl="file://"+GetApplicationPath("wxpython.html"))
clientHandler = ClientHandler()
self.browser.SetClientHandler(clientHandler)
cefpython.SetGlobalClientCallback("OnCertificateError",
clientHandler._OnCertificateError)
cefpython.SetGlobalClientCallback("OnBeforePluginLoad",
clientHandler._OnBeforePluginLoad)
jsBindings = cefpython.JavascriptBindings(
bindToFrames=False, bindToPopups=True)
jsBindings.SetFunction("PyPrint", PyPrint)
jsBindings.SetProperty("pyProperty", "This was set in Python")
jsBindings.SetProperty("pyConfig", ["This was set in Python",
{"name": "Nested dictionary", "isNested": True},
[1,"2", None]])
jsBindings.SetObject("external", JavascriptExternal(self.browser))
self.browser.SetJavascriptBindings(jsBindings)
self.Bind(wx.EVT_CLOSE, self.OnClose)
if USE_EVT_IDLE:
# Bind EVT_IDLE only for the main application frame.
self.Bind(wx.EVT_IDLE, self.OnIdle)
def CreateMenu(self):
filemenu = wx.Menu()
filemenu.Append(1, "Open")
exit = filemenu.Append(2, "Exit")
self.Bind(wx.EVT_MENU, self.OnClose, exit)
aboutmenu = wx.Menu()
aboutmenu.Append(1, "CEF Python")
menubar = wx.MenuBar()
menubar.Append(filemenu,"&File")
menubar.Append(aboutmenu, "&About")
self.SetMenuBar(menubar)
def OnClose(self, event):
self.browser.CloseBrowser()
self.Destroy()
def OnIdle(self, event):
cefpython.MessageLoopWork()
def PyPrint(message):
print(message)
class JavascriptExternal:
mainBrowser = None
def __init__(self, mainBrowser):
self.mainBrowser = mainBrowser
def Print(self, message):
print(message)
def TestAllTypes(self, *args):
print(args)
def ExecuteFunction(self, *args):
self.mainBrowser.GetMainFrame().ExecuteFunction(*args)
def TestJSCallback(self, jsCallback):
print("jsCallback.GetFunctionName() = %s" % jsCallback.GetFunctionName())
print("jsCallback.GetFrame().GetIdentifier() = %s" % \
jsCallback.GetFrame().GetIdentifier())
jsCallback.Call("This message was sent from python using js callback")
def TestJSCallbackComplexArguments(self, jsObject):
jsCallback = jsObject["myCallback"];
jsCallback.Call(1, None, 2.14, "string", ["list", ["nested list", \
{"nested object":None}]], \
{"nested list next":[{"deeply nested object":1}]})
def TestPythonCallback(self, jsCallback):
jsCallback.Call(self.PyCallback)
def PyCallback(self, *args):
message = "PyCallback() was executed successfully! Arguments: %s" \
% str(args)
print(message)
self.mainBrowser.GetMainFrame().ExecuteJavascript(
"window.alert(\"%s\")" % message)
# -------------------------------------------------------------------------
# Cookies
# -------------------------------------------------------------------------
cookieVisitor = None
def VisitAllCookies(self):
# Need to keep the reference alive.
self.cookieVisitor = CookieVisitor()
cookieManager = self.mainBrowser.GetUserData("cookieManager")
if not cookieManager:
print("\nCookie manager not yet created! Visit http website first")
return
cookieManager.VisitAllCookies(self.cookieVisitor)
def VisitUrlCookies(self):
# Need to keep the reference alive.
self.cookieVisitor = CookieVisitor()
cookieManager = self.mainBrowser.GetUserData("cookieManager")
if not cookieManager:
print("\nCookie manager not yet created! Visit http website first")
return
cookieManager.VisitUrlCookies(
"http://www.html-kit.com/tools/cookietester/",
False, self.cookieVisitor)
# .www.html-kit.com
def SetCookie(self):
cookieManager = self.mainBrowser.GetUserData("cookieManager")
if not cookieManager:
print("\nCookie manager not yet created! Visit http website first")
return
cookie = cefpython.Cookie()
cookie.SetName("Created_Via_Python")
cookie.SetValue("yeah really")
cookieManager.SetCookie("http://www.html-kit.com/tools/cookietester/",
cookie)
print("\nCookie created! Visit html-kit cookietester to see it")
def DeleteCookies(self):
cookieManager = self.mainBrowser.GetUserData("cookieManager")
if not cookieManager:
print("\nCookie manager not yet created! Visit http website first")
return
cookieManager.DeleteCookies(
"http://www.html-kit.com/tools/cookietester/",
"Created_Via_Python")
print("\nCookie deleted! Visit html-kit cookietester to see the result")
class CookieVisitor:
def Visit(self, cookie, count, total, deleteCookie):
if count == 0:
print("\nCookieVisitor.Visit(): total cookies: %s" % total)
print("\nCookieVisitor.Visit(): cookie:")
print(cookie.Get())
# True to continue visiting cookies
return True
class ClientHandler:
# -------------------------------------------------------------------------
# DisplayHandler
# -------------------------------------------------------------------------
def OnLoadingStateChange(self, browser, isLoading, canGoBack,
canGoForward):
print("DisplayHandler::OnLoadingStateChange()")
print("isLoading = %s, canGoBack = %s, canGoForward = %s" \
% (isLoading, canGoBack, canGoForward))
def OnAddressChange(self, browser, frame, url):
print("DisplayHandler::OnAddressChange()")
print("url = %s" % url)
def OnTitleChange(self, browser, title):
print("DisplayHandler::OnTitleChange()")
print("title = %s" % title)
def OnTooltip(self, browser, textOut):
print("DisplayHandler::OnTooltip()")
print("text = %s" % textOut[0])
# OnTooltip seems not to work on Linux, reported bug on the CEF forum:
# http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10898
def OnStatusMessage(self, browser, value):
if not value:
# Do not notify in the console about empty statuses.
return
print("DisplayHandler::OnStatusMessage()")
print("value = %s" % value)
def OnConsoleMessage(self, browser, message, source, line):
print("DisplayHandler::OnConsoleMessage()")
print("message = %s" % message)
print("source = %s" % source)
print("line = %s" % line)
# -------------------------------------------------------------------------
# KeyboardHandler
# -------------------------------------------------------------------------
def OnPreKeyEvent(self, browser, event, eventHandle,
isKeyboardShortcutOut):
print("KeyboardHandler::OnPreKeyEvent()")
def OnKeyEvent(self, browser, event, eventHandle):
print("KeyboardHandler::OnKeyEvent()")
print("native_key_code = %s" % event["native_key_code"])
if platform.system() == "Linux":
# F5 = 71
if event["native_key_code"] == 71:
print("F5 pressed! Reloading page..")
browser.ReloadIgnoreCache()
# -------------------------------------------------------------------------
# RequestHandler
# -------------------------------------------------------------------------
def OnBeforeResourceLoad(self, browser, frame, request):
print("RequestHandler::OnBeforeResourceLoad()")
print("url = %s" % request.GetUrl())
return False
def OnResourceRedirect(self, browser, frame, oldUrl, newUrlOut):
print("RequestHandler::OnResourceRedirect()")
print("old url = %s" % oldUrl)
print("new url = %s" % newUrlOut[0])
def GetAuthCredentials(self, browser, frame, isProxy, host, port, realm,
scheme, callback):
print("RequestHandler::GetAuthCredentials()")
print("host = %s" % host)
print("realm = %s" % realm)
callback.Continue(username="test", password="test")
return True
def OnQuotaRequest(self, browser, originUrl, newSize, callback):
print("RequestHandler::OnQuotaRequest()")
print("origin url = %s" % originUrl)
print("new size = %s" % newSize)
callback.Continue(True)
return True
def GetCookieManager(self, browser, mainUrl):
# Create unique cookie manager for each browser.
# Buggy IO thread callbacks, need to update to revision 1306, see:
# http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10901
cookieManager = browser.GetUserData("cookieManager")
if cookieManager:
return cookieManager
else:
cookieManager = cefpython.CookieManager.CreateManager("")
browser.SetUserData("cookieManager", cookieManager)
return cookieManager
def OnProtocolExecution(self, browser, url, allowExecutionOut):
# There's no default implementation for OnProtocolExecution on Linux,
# you have to make OS system call on your own. You probably also need
# to use LoadHandler::OnLoadError() when implementing this on Linux.
print("RequestHandler::OnProtocolExecution()")
print("url = %s" % url)
if url.startswith("magnet:"):
print("Magnet link allowed!")
allowExecutionOut[0] = True
def _OnBeforePluginLoad(self, browser, url, policyUrl, info):
# This callback seems not to work on Linux, reported here:
# http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10901
print("RequestHandler::OnBeforePluginLoad()")
print("url = %s" % url)
print("policy url = %s" % policyUrl)
print("info.GetName() = %s" % info.GetName())
print("info.GetPath() = %s" % info.GetPath())
print("info.GetVersion() = %s" % info.GetVersion())
print("info.GetDescription() = %s" % info.GetDescription())
# False to allow, True to block plugin.
return False
def _OnCertificateError(self, certError, requestUrl, callback):
print("RequestHandler::OnCertificateError()")
print("certError = %s" % certError)
print("requestUrl = %s" % requestUrl)
if requestUrl.startswith(
"https://sage.math.washington.edu:8091/do-not-allow"):
print("Not allowed!")
return False
if requestUrl.startswith(
"https://sage.math.washington.edu:8091/hudson/job/"):
print("Allowed!")
callback.Continue(True)
return True
return False
class MyApp(wx.App):
timer = None
timerID = 1
timerCount = 0
def OnInit(self):
if not USE_EVT_IDLE:
self.CreateTimer()
frame = MainFrame()
self.SetTopWindow(frame)
frame.Show()
return True
def CreateTimer(self):
# See "Making a render loop":
# http://wiki.wxwidgets.org/Making_a_render_loop
# Another approach is to use EVT_IDLE in MainFrame,
# see which one fits you better.
self.timer = wx.Timer(self, self.timerID)
self.timer.Start(10) # 10ms
wx.EVT_TIMER(self, self.timerID, self.OnTimer)
def OnTimer(self, event):
self.timerCount += 1
# print("wxpython.py: OnTimer() %d" % self.timerCount)
cefpython.MessageLoopWork()
def OnExit(self):
# When app.MainLoop() returns, MessageLoopWork() should
# not be called anymore.
if not USE_EVT_IDLE:
self.timer.Stop()
if __name__ == '__main__':
sys.excepthook = ExceptHook
cefpython.g_debug = True
cefpython.g_debugFile = GetApplicationPath("debug.log")
settings = {
"log_severity": cefpython.LOGSEVERITY_INFO, # LOGSEVERITY_VERBOSE
"log_file": GetApplicationPath("debug.log"),
"release_dcheck_enabled": True, # Enable only when debugging.
# This directories must be set on Linux
"locales_dir_path": cefpython.GetModuleDirectory()+"/locales",
"resources_dir_path": cefpython.GetModuleDirectory(),
"browser_subprocess_path": "%s/%s" % (
cefpython.GetModuleDirectory(), "subprocess")
}
# print("browser_subprocess_path="+settings["browser_subprocess_path"])
cefpython.Initialize(settings)
print('wx.version=%s' % wx.version())
app = MyApp(False)
app.MainLoop()
# Let wx.App destructor do the cleanup before calling cefpython.Shutdown().
del app
cefpython.Shutdown()