changeset 317:bbadd490e63c

misc: gather the constants in a single file, as it is done for other frontends
author souliane <souliane@mailoo.org>
date Fri, 03 Jan 2014 14:14:34 +0100
parents 4148f8f4caa0
children c12c9a1acf2f
files constants.py libervia.py libervia.tac server_side/blog.py
diffstat 4 files changed, 63 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/constants.py	Fri Jan 03 14:14:34 2014 +0100
@@ -0,0 +1,45 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+# Primitivus: a SAT frontend
+# Copyright (C) 2009, 2010, 2011, 2012, 2013  Jérôme Poisson (goffi@goffi.org)
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from sat.core.i18n import _, D_
+from sat_frontends import constants
+
+
+class Const(constants.Const):
+
+    APP_NAME = 'Libervia'
+
+    TIMEOUT = 300  # Session's time out, after that the user will be disconnected
+    LIBERVIA_DIR = "output/"
+    MEDIA_DIR = "media/"
+    AVATARS_DIR = "avatars/"
+    CARDS_DIR = "games/cards/tarot"
+
+    ERRNUM_BRIDGE_ERRBACK = 0  # FIXME
+    ERRNUM_LIBERVIA = 0  # FIXME
+
+    # Security limit for Libervia (get/set params)
+    SECURITY_LIMIT = 0
+
+    # Security limit for Libervia server_side
+    SERVER_SECURITY_LIMIT = constants.Const.NO_SECURITY_LIMIT
+
+    # Frontend parameters
+    ENABLE_UNIBOX_KEY = D_("Composition")
+    ENABLE_UNIBOX_PARAM = D_("Enable unibox")
--- a/libervia.py	Fri Jan 03 13:33:44 2014 +0100
+++ b/libervia.py	Fri Jan 03 14:14:34 2014 +0100
@@ -37,7 +37,8 @@
 from browser_side.tools import html_sanitize
 from sat_frontends.tools.misc import InputHistory
 from sat_frontends.tools.strings import getURLParams
-from sat_frontends.constants import Const
+from constants import Const
+
 
 MAX_MBLOG_CACHE = 500  # Max microblog entries kept in memories
 
--- a/libervia.tac	Fri Jan 03 13:33:44 2014 +0100
+++ b/libervia.tac	Fri Jan 03 14:14:34 2014 +0100
@@ -41,21 +41,8 @@
 from server_side.blog import MicroBlog
 from zope.interface import Interface, Attribute, implements
 from xml.dom import minidom
-from sat_frontends.constants import Const
-
-#import time
+from constants import Const
 
-TIMEOUT = 300 #Session's time out, after that the user will be disconnected
-LIBERVIA_DIR = "output/"
-MEDIA_DIR = "media/"
-AVATARS_DIR = "avatars/"
-CARDS_DIR = "games/cards/tarot"
-
-ERRNUM_BRIDGE_ERRBACK = 0 # FIXME
-ERRNUM_LIBERVIA = 0 # FIXME
-
-# Security limit for Libervia (get/set params)
-SECURITY_LIMIT = 0
 
 class ISATSession(Interface):
     profile = Attribute("Sat profile")
@@ -68,7 +55,7 @@
         self.jid = None
 
 class LiberviaSession(server.Session):
-    sessionTimeout = TIMEOUT
+    sessionTimeout = Const.TIMEOUT
 
     def __init__(self, *args, **kwargs):
         self.__lock = False
@@ -149,7 +136,7 @@
                 d.callback(args[0])
 
         def _errback(result):
-            d.errback(Failure(jsonrpclib.Fault(ERRNUM_BRIDGE_ERRBACK, unicode(result))))
+            d.errback(Failure(jsonrpclib.Fault(Const.ERRNUM_BRIDGE_ERRBACK, unicode(result))))
 
         kwargs["callback"] = _callback
         kwargs["errback"] = _errback
@@ -169,7 +156,7 @@
         if not profile:
             #user is not identified, we return a jsonrpc fault
             parsed = jsonrpclib.loads(request.content.read())
-            fault = jsonrpclib.Fault(ERRNUM_LIBERVIA, "Not allowed") #FIXME: define some standard error codes for libervia
+            fault = jsonrpclib.Fault(Const.ERRNUM_LIBERVIA, "Not allowed") #FIXME: define some standard error codes for libervia
             return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc'))
         return jsonrpc.JSONRPC.render(self, request)
 
@@ -406,7 +393,7 @@
         """Give the path of all the tarot cards"""
         _join = os.path.join
         _media_dir = _join(self.sat_host.media_dir,'')
-        return map(lambda x: _join(MEDIA_DIR, x[len(_media_dir):]),glob.glob(_join(_media_dir,CARDS_DIR,'*_*.png')));
+        return map(lambda x: _join(Const.MEDIA_DIR, x[len(_media_dir):]), glob.glob(_join(_media_dir, Const.CARDS_DIR, '*_*.png')));
 
     def jsonrpc_tarotGameReady(self, player, referee):
         """Tell to the server that we are ready to start the game"""
@@ -454,7 +441,7 @@
     def jsonrpc_getParamsUI(self):
         """Return the parameters XML for profile"""
         profile = ISATSession(self.session).profile
-        d = self.asyncBridgeCall("getParams", SECURITY_LIMIT, profile)
+        d = self.asyncBridgeCall("getParams", Const.SECURITY_LIMIT, profile)
 
         def setAuthorizedParams(d):
             if self.authorized_params is None:
@@ -478,13 +465,13 @@
     def jsonrpc_asyncGetParamA(self, param, category, attribute="value"):
         """Return the parameter value for profile"""
         profile = ISATSession(self.session).profile
-        d = self.asyncBridgeCall("asyncGetParamA", param, category, attribute, SECURITY_LIMIT, profile_key=profile)
+        d = self.asyncBridgeCall("asyncGetParamA", param, category, attribute, Const.SECURITY_LIMIT, profile_key=profile)
         return d
 
     def jsonrpc_setParam(self, name, value, category):
         profile = ISATSession(self.session).profile
         if category in self.authorized_params and name in self.authorized_params[category]:
-            return self.sat_host.bridge.setParam(name, value, category, SECURITY_LIMIT, profile)
+            return self.sat_host.bridge.setParam(name, value, category, Const.SECURITY_LIMIT, profile)
         else:
             warning("Trying to set parameter '%s' in category '%s' without authorization!!!"
                     % (name, category))
@@ -554,7 +541,7 @@
             profile = ISATSession(_session).profile
             if not profile:
                 #user is not identified, we return a jsonrpc fault
-                fault = jsonrpclib.Fault(ERRNUM_LIBERVIA, "Not allowed") #FIXME: define some standard error codes for libervia
+                fault = jsonrpclib.Fault(Const.ERRNUM_LIBERVIA, "Not allowed") #FIXME: define some standard error codes for libervia
                 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc'))
         self.request = request
         return jsonrpc.JSONRPC.render(self, request)
@@ -825,7 +812,7 @@
         profile = ISATSession(_session).profile
         if not profile:
             #user is not identified, we return a jsonrpc fault
-            fault = jsonrpclib.Fault(ERRNUM_LIBERVIA, "Not allowed") #FIXME: define some standard error codes for libervia
+            fault = jsonrpclib.Fault(Const.ERRNUM_LIBERVIA, "Not allowed") #FIXME: define some standard error codes for libervia
             return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc'))
         self.request = request
         return jsonrpc.JSONRPC.render(self, request)
@@ -925,7 +912,7 @@
 
     def __init__(self):
         self._cleanup = []
-        root = ProtectedFile(LIBERVIA_DIR)
+        root = ProtectedFile(Const.LIBERVIA_DIR)
         self.signal_handler = SignalHandler(self)
         _register = Register(self)
         _upload_radiocol = UploadManagerRadioCol(self)
@@ -963,8 +950,8 @@
         root.putChild('upload_avatar', _upload_avatar)
         root.putChild('blog', MicroBlog(self))
         root.putChild('css', ProtectedFile("server_css/"))
-        root.putChild(os.path.dirname(MEDIA_DIR), ProtectedFile(self.media_dir))
-        root.putChild(os.path.dirname(AVATARS_DIR), ProtectedFile(os.path.join(self.local_dir, AVATARS_DIR)))
+        root.putChild(os.path.dirname(Const.MEDIA_DIR), ProtectedFile(self.media_dir))
+        root.putChild(os.path.dirname(Const.AVATARS_DIR), ProtectedFile(os.path.join(self.local_dir, Const.AVATARS_DIR)))
         root.putChild('radiocol', ProtectedFile(_upload_radiocol.getTmpDir(), defaultType="audio/ogg")) #We cheat for PoC because we know we are on the same host, so we use directly upload dir
         self.site = server.Site(root)
         self.site.sessionFactory = LiberviaSession
@@ -993,6 +980,6 @@
 
 
 registerAdapter(SATSession, server.Session, ISATSession)
-application = service.Application('Libervia')
+application = service.Application(Const.APP_NAME)
 service = Libervia()
 service.setServiceParent(application)
--- a/server_side/blog.py	Fri Jan 03 13:33:44 2014 +0100
+++ b/server_side/blog.py	Fri Jan 03 14:14:34 2014 +0100
@@ -26,9 +26,7 @@
 from twisted.web.resource import Resource
 from twisted.words.protocols.jabber.jid import JID
 from datetime import datetime
-
-# Security limit for Libervia server_side
-SECURITY_LIMIT = -1
+from constants import Const
 
 
 class MicroBlog(Resource):
@@ -68,7 +66,7 @@
                     self.host.bridge.getLastGroupBlogs(pub_jid.userhost(), 10, 'libervia', d2.callback, d2.errback)
 
                 d1 = defer.Deferred()
-                JID(self.host.bridge.asyncGetParamA('JabberID', 'Connection', 'value', SECURITY_LIMIT, prof_found, callback=d1.callback, errback=d1.errback))
+                JID(self.host.bridge.asyncGetParamA('JabberID', 'Connection', 'value', Const.SERVER_SECURITY_LIMIT, prof_found, callback=d1.callback, errback=d1.errback))
                 d1.addCallbacks(got_jid)
 
                 return server.NOT_DONE_YET