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
|
# python import
import os, sys, threading, time
# gettext import
from gettext import gettext as _
# sugar import
from sugar.activity import activity
# hulahop import
from hulahop.webview import WebView
# add lib path to current python path
sys.path.append(os.path.join(activity.get_bundle_path(), 'lib'))
# server import
from server import config
from server.flask import app, run_app, logger
# {{package}} import
from {{package}}.controllers import app
# get port from config
_port = config.Config().get('server>port', type_=int)
_port = '5000' if _port is None or _port == '' else _port
# ..
URL_BASE = 'http://localhost:%s' % _port
class ThreadServer(threading.Thread):
def __init__(self, activity_):
# init parent
threading.Thread.__init__(self)
# main flag
self._die = False
# start right now
self.start()
def kill(self):
self._die = True
def run(self):
# do run
run_app()
# ...
while self._die is False:
continue
# ..
del app
def _toolbar_changed(toolbox, page, activity_):
"""Catch toolbox activity tab focus to display settings screen.
"""
# is the activity tab?
if page == 0:
# show the activity screen
activity_.change_screen('/')
else:
pass
# propagate it
return True
class {{package.capitalize()}}Activity(activity.Activity):
def __init__(self, handle):
# init thread server
self._thread = ThreadServer(self)
# ..
time.sleep(5)
# init parents
activity.Activity.__init__(self, handle)
self.max_participants = 1
# ..
if config.Config().get('activity>use-toolbar', type_=bool):
self.__init_toolbar()
else:
pass
# ...
self.web_view = WebView()
self.set_canvas(self.web_view)
self.web_view.show()
# ...
self.change_screen('')
def get_toolbox(self):
return self._toolbox
def change_screen(self, name):
self.web_view.load_uri('%s/%s' % (URL_BASE, name))
def __init_toolbar(self):
"""Keep an example of how to manage toolbar in our webapp ...
"""
# {{package}} ui import
from {{package}}.ui import toolbar
# get toolbox
self._toolbox = activity.ActivityToolbox(self)
# add tool bars
self.set_toolbox(self._toolbox)
# show
self._toolbox.show()
# tmp var
_toolbar = None
# init toolbars
for _n in ['menu']:
# init toolbar
_t = toolbar.Toolbar(self, name=_n)
# if default toolbar .. set default screen
if _n == 'post':
self.change_screen(n_)
_toolbar = _t
# set default tab
self._toolbox.set_current_toolbar(1)
# ..
self._toolbox.connect('current-toolbar-changed', _toolbar_changed, self)
def read_file(self, file_path):
# .. should be overriden
pass
def write_file(self, file_path):
# .. should be overriden
pass
def close(self, skip_save=False):
# stop the thread
self._thread.kill()
# ..
activity.Activity.close(self, skip_save=True)
|