From 8f31d026978352524cd8d8059d313980b90a4947 Mon Sep 17 00:00:00 2001 From: florent Date: Sat, 10 Sep 2011 13:36:37 +0000 Subject: upgrade web POC with last lib version from nutrinoweb experiments --- diff --git a/atoideweb/activity.py b/atoideweb/activity.py index 5331be8..5215b22 100644 --- a/atoideweb/activity.py +++ b/atoideweb/activity.py @@ -1,5 +1,5 @@ # python import -import logging +import os, sys, threading, time # gettext import from gettext import gettext as _ @@ -8,15 +8,45 @@ from sugar.activity import activity # hulahop import from hulahop.webview import WebView -# atoideweb import - first to update lib path -import run +# 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 logger +from server.flask import app, run_app, logger +# atoideweb import +from atoideweb.controllers import app -URL_BASE = 'http://localhost:5000/' + +# 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_): @@ -25,43 +55,45 @@ def _toolbar_changed(toolbox, page, activity_): # is the activity tab? if page == 0: # show the activity screen - activity_.change_screen('activity') + activity_.change_screen('main') else: pass # propagate it return True -class AToiDeWebActivity(run.Server, activity.Activity): +class AToiDeWebActivity(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 # .. - run.Server.__init__(self) - # .. if config.Config().get('activity>use-toolbar', type_=bool): self.__init_toolbar() - else + else: pass # ... self.web_view = WebView() self.set_canvas(self.web_view) self.web_view.show() # ... - self.change_screen('atoideweb') + self.change_screen('') def get_toolbox(self): return self._toolbox def change_screen(self, name): - self.web_view.load_uri(URL_BASE + 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 ... """ - # atoideweb ui imort + # atoideweb ui import from atoideweb.ui import toolbar # get toolbox self._toolbox = activity.ActivityToolbox(self) @@ -93,5 +125,7 @@ class AToiDeWebActivity(run.Server, activity.Activity): pass def close(self, skip_save=False): - run.Server.close(self) + # stop the thread + self._thread.kill() + # .. activity.Activity.close(self, skip_save=True) diff --git a/atoideweb/controllers/gallery.py b/atoideweb/controllers/gallery.py index c3173d5..e164c68 100644 --- a/atoideweb/controllers/gallery.py +++ b/atoideweb/controllers/gallery.py @@ -10,16 +10,17 @@ from server.flask import app, logger, render, request, jsonify # server tools import from server.tools import storage -JNL_PATH = os.path.join('images', 'journal') +JNL_PATH = os.path.join('static', 'images', 'journal') @app.route('/gallery') def gallery(): # list images - _images = storage.list_dir(path=JNL_PATH) + _images_names = storage.list_dir(path=JNL_PATH) + _images_urls = ['/static/images/journal/%s' % _f for _f in _images_names] # prepare result _content = { 'title' : _('atdw - Gallery'), - 'images': enumerate(_images) + 'images': enumerate(_images_urls) } # render result return render('atoideweb/gallery.html', **_content) diff --git a/atoideweb/ui/toolbar.py b/atoideweb/ui/toolbar.py index 9266606..89c3d17 100644 --- a/atoideweb/ui/toolbar.py +++ b/atoideweb/ui/toolbar.py @@ -24,26 +24,27 @@ def _cb_next(widget, toolbar, next_): BUTTONS = { - 'post' : ['document-generic', _cb_next], - 'ajax' : ['document-generic', _cb_next], - 'gallery' : ['document-generic', _cb_next], - 'separator' : [None, None] + 'post' : ['document-generic', _cb_next], + 'ajax' : ['document-generic', _cb_next], + 'separator' : [None, None], + 'gallery' : ['document-generic', _cb_next] } TOOLBARS = { 'menu' : [ - ['post', 'ajax'], + ['post', 'ajax', 'gallery'], [] ], } TITLES = { 'menu' : { - 'toolbox': _('Menu'), + 'toolbox': _('Debug'), 'buttons': { - 'post': _('Post'), - 'ajax': _('Ajax') + 'post' : _('Post'), + 'ajax' : _('Ajax'), + 'gallery': _('Gallery') } } } diff --git a/lib/server/_server.py b/lib/server/_server.py index eb7f94c..cdc9164 100644 --- a/lib/server/_server.py +++ b/lib/server/_server.py @@ -1,13 +1,14 @@ -# python import -import atexit, multiprocessing # server import from server import config -from server.flask import app, run_app class Server(object): def __init__(self): + # python import + import atexit, multiprocessing + # .. + from server.flask import run_app # start the server self._server = multiprocessing.Process(target=run_app) self._server.start() diff --git a/lib/server/flask/_app.py b/lib/server/flask/_app.py index 025e745..89d9cb4 100644 --- a/lib/server/flask/_app.py +++ b/lib/server/flask/_app.py @@ -23,10 +23,11 @@ import flask try: from sugar.activity import activity BUNDLE = activity.get_bundle_path() - ROOT = activity.get_bundle_path() + ROOT = activity.get_activity_root() except Exception, e: - BUNDLE = '.' - ROOT = '.' + BUNDLE = os.path.abspath(os.path.join( + os.path.dirname(__file__), '..', '..', '..')) + ROOT = BUNDLE # init app app = flask.Flask(__name__) diff --git a/lib/server/tools/__init__.py b/lib/server/tools/__init__.py index e69de29..ec464ac 100644 --- a/lib/server/tools/__init__.py +++ b/lib/server/tools/__init__.py @@ -0,0 +1 @@ +from server.tools._obj import obj diff --git a/lib/server/tools/_obj.py b/lib/server/tools/_obj.py new file mode 100644 index 0000000..c2c3383 --- /dev/null +++ b/lib/server/tools/_obj.py @@ -0,0 +1,8 @@ + +class obj(object): + def __init__(self, d): + for a, b in d.items(): + if isinstance(b, (list, tuple)): + setattr(self, a, [obj(x) if isinstance(x, dict) else x for x in b]) + else: + setattr(self, a, obj(b) if isinstance(b, dict) else b) diff --git a/lib/server/tools/storage.py b/lib/server/tools/storage.py index 535eb66..2dc386c 100644 --- a/lib/server/tools/storage.py +++ b/lib/server/tools/storage.py @@ -12,21 +12,37 @@ ACTIVITY_NAMES = { } +def check_dir(path): + if os.path.exists(path): + pass + else: + os.mkdir(path) + + +def check_file(path): + if os.path.exists(path): + pass + else: + _f = open(path, 'wb') + _f.write('') + _f.close() + + def get_path(path=None, bundle=True): # .. - path = 'static' if path is None else os.path.join('static', path) + path = 'static' if path is None else path # .. if bundle is True: return os.path.join(BUNDLE, path) else: - return os.path.join(ROOT, 'data', path) + return os.path.join(ROOT, path) def list_dir(path=None, bundle=True): # .. path = get_path(path=path, bundle=bundle) # .. - return [os.path.join(path, _f) for _f in os.listdir(path)] + return os.listdir(path) def get_sound_path(filename, path=None, bundle=True, ext=None): -- cgit v0.9.1