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
|
# from http://www.aclevername.com/articles/python-webgui/
import time
import queue
import _thread
import urllib.request, urllib.parse, urllib.error
import gi
from gi.repository import Gdk
from gi.repository import Gtk
from gi.repository import GLib
try:
gi.require_version('WebKit2', '4.0')
from gi.repository import WebKit2
have_webkit = True
except:
have_webkit = False
class UseWebKit: pass
use = UseWebKit
class WebKitMethods(object):
@staticmethod
def create_browser():
return WebKit2.WebView()
@staticmethod
def inject_javascript(browser, script):
browser.execute_script(script)
@staticmethod
def connect_title_changed(browser, callback):
def callback_wrapper(widget, param): callback(widget.get_property(param.name))
browser.connect('notify::title', callback_wrapper)
@staticmethod
def open_uri(browser, html):
browser.load_html(html, 'file:///usr/share/mageiawelcome/')
if use is UseWebKit:
implementation = WebKitMethods
def asynchronous_gtk_message(fun):
def worker(wargs):
(function, args, kwargs) = wargs
function(*args, **kwargs)
def fun2(*args, **kwargs):
GLib.idle_add(worker, (fun, args, kwargs))
return fun2
def synchronous_gtk_message(fun):
class NoResult: pass
def worker(wargs):
(R, function, args, kwargs) = wargs
R.result = function(*args, **kwargs)
def fun2(*args, **kwargs):
class R: result = NoResult
GLib.idle_add(worker, (R, fun, args, kwargs))
while R.result is NoResult: time.sleep(0.01)
return R.result
return fun2
def launch_browser(html, quit_function=None, echo=True):
def no_menu(*args): return True
window = Gtk.Window()
browser = implementation.create_browser()
settings = browser.get_settings()
settings.set_enable_write_console_messages_to_stdout(1)
settings.set_allow_universal_access_from_file_urls(1);
browser.connect('context-menu', no_menu)
inspector = browser.get_inspector()
inspector.show()
box = Gtk.VBox(homogeneous=False, spacing=0)
window.add(box)
if quit_function is not None:
window.connect('destroy', quit_function)
box.pack_start(browser, expand=True, fill=True, padding=0)
window.set_icon_name('preferences-desktop-personal')
window.set_title(_('Welcome to Mageia!'))
window.set_position(Gtk.WindowPosition.CENTER)
window.set_default_size(800, 500)
window.set_size_request(800, 500)
window.set_resizable(False)
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, html)
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
def start_gtk_thread():
# Start GTK in its own thread:
Gdk.threads_init()
Gdk.threads_enter()
_thread.start_new_thread(Gtk.main, ())
def kill_gtk_thread():
asynchronous_gtk_message(Gtk.main_quit)()
|