-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebgui.py
More file actions
103 lines (72 loc) · 2.43 KB
/
webgui.py
File metadata and controls
103 lines (72 loc) · 2.43 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
import time
import Queue
import thread
import urllib
import gtk
import gobject
import webkit
class WebKitMethods(object):
@staticmethod
def create_browser():
return webkit.WebView()
@staticmethod
def inject_javascript(browser, script):
browser.execute_script(script)
@staticmethod
def connect_title_changed(browser, callback):
def callback_wrapper(widget, frame, title): callback(title)
browser.connect('title-changed', callback_wrapper)
@staticmethod
def open_uri(browser, uri):
browser.open(uri)
implementation = WebKitMethods
def asynchronous_gtk_message(fun):
def worker((function, args, kwargs)):
apply(function, args, kwargs)
def fun2(*args, **kwargs):
gobject.idle_add(worker, (fun, args, kwargs))
return fun2
def synchronous_gtk_message(fun):
class NoResult: pass
def worker((R, function, args, kwargs)):
R.result = apply(function, args, kwargs)
def fun2(*args, **kwargs):
class R: result = NoResult
gobject.idle_add(worker, (R, fun, args, kwargs))
while R.result is NoResult: time.sleep(0.01)
return R.result
return fun2
def launch_browser(uri, quit_function=None, echo=True, width=640, height=480):
window = gtk.Window()
window.set_position(gtk.WIN_POS_CENTER)
browser = implementation.create_browser()
box = gtk.VBox(homogeneous=False, spacing=0)
scroller = gtk.ScrolledWindow()
window.add(scroller)
scroller.add(browser)
if quit_function is not None:
window.connect('destroy', quit_function)
window.set_default_size(width, height)
window.show_all()
message_queue = Queue.Queue()
def title_changed(title):
if title != 'null': message_queue.put(title)
implementation.connect_title_changed(browser, title_changed)
implementation.open_uri(browser, uri)
def web_recv():
if message_queue.empty():
return None
else:
msg = message_queue.get()
if echo: print '>>>', msg
return msg
def web_send(msg):
if echo: print '<<<', msg
asynchronous_gtk_message(implementation.inject_javascript)(browser, msg)
return browser, web_recv, web_send, window
def start_gtk_thread():
# Start GTK in its own thread:
gtk.gdk.threads_init()
thread.start_new_thread(gtk.main, ())
def kill_gtk_thread():
asynchronous_gtk_message(gtk.main_quit)()