Mercurial > libervia-web
comparison libervia.tac @ 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 | 05e264e96a1c |
children | c12c9a1acf2f |
comparison
equal
deleted
inserted
replaced
316:4148f8f4caa0 | 317:bbadd490e63c |
---|---|
39 import os.path, sys | 39 import os.path, sys |
40 import tempfile, shutil, uuid | 40 import tempfile, shutil, uuid |
41 from server_side.blog import MicroBlog | 41 from server_side.blog import MicroBlog |
42 from zope.interface import Interface, Attribute, implements | 42 from zope.interface import Interface, Attribute, implements |
43 from xml.dom import minidom | 43 from xml.dom import minidom |
44 from sat_frontends.constants import Const | 44 from constants import Const |
45 | 45 |
46 #import time | |
47 | |
48 TIMEOUT = 300 #Session's time out, after that the user will be disconnected | |
49 LIBERVIA_DIR = "output/" | |
50 MEDIA_DIR = "media/" | |
51 AVATARS_DIR = "avatars/" | |
52 CARDS_DIR = "games/cards/tarot" | |
53 | |
54 ERRNUM_BRIDGE_ERRBACK = 0 # FIXME | |
55 ERRNUM_LIBERVIA = 0 # FIXME | |
56 | |
57 # Security limit for Libervia (get/set params) | |
58 SECURITY_LIMIT = 0 | |
59 | 46 |
60 class ISATSession(Interface): | 47 class ISATSession(Interface): |
61 profile = Attribute("Sat profile") | 48 profile = Attribute("Sat profile") |
62 jid = Attribute("JID associated with the profile") | 49 jid = Attribute("JID associated with the profile") |
63 | 50 |
66 def __init__(self, session): | 53 def __init__(self, session): |
67 self.profile = None | 54 self.profile = None |
68 self.jid = None | 55 self.jid = None |
69 | 56 |
70 class LiberviaSession(server.Session): | 57 class LiberviaSession(server.Session): |
71 sessionTimeout = TIMEOUT | 58 sessionTimeout = Const.TIMEOUT |
72 | 59 |
73 def __init__(self, *args, **kwargs): | 60 def __init__(self, *args, **kwargs): |
74 self.__lock = False | 61 self.__lock = False |
75 server.Session.__init__(self, *args, **kwargs) | 62 server.Session.__init__(self, *args, **kwargs) |
76 | 63 |
147 if len(args) != 1: | 134 if len(args) != 1: |
148 Exception("Multiple return arguments not supported") | 135 Exception("Multiple return arguments not supported") |
149 d.callback(args[0]) | 136 d.callback(args[0]) |
150 | 137 |
151 def _errback(result): | 138 def _errback(result): |
152 d.errback(Failure(jsonrpclib.Fault(ERRNUM_BRIDGE_ERRBACK, unicode(result)))) | 139 d.errback(Failure(jsonrpclib.Fault(Const.ERRNUM_BRIDGE_ERRBACK, unicode(result)))) |
153 | 140 |
154 kwargs["callback"] = _callback | 141 kwargs["callback"] = _callback |
155 kwargs["errback"] = _errback | 142 kwargs["errback"] = _errback |
156 getattr(self.sat_host.bridge, method_name)(*args, **kwargs) | 143 getattr(self.sat_host.bridge, method_name)(*args, **kwargs) |
157 return d | 144 return d |
167 self.session = request.getSession() | 154 self.session = request.getSession() |
168 profile = ISATSession(self.session).profile | 155 profile = ISATSession(self.session).profile |
169 if not profile: | 156 if not profile: |
170 #user is not identified, we return a jsonrpc fault | 157 #user is not identified, we return a jsonrpc fault |
171 parsed = jsonrpclib.loads(request.content.read()) | 158 parsed = jsonrpclib.loads(request.content.read()) |
172 fault = jsonrpclib.Fault(ERRNUM_LIBERVIA, "Not allowed") #FIXME: define some standard error codes for libervia | 159 fault = jsonrpclib.Fault(Const.ERRNUM_LIBERVIA, "Not allowed") #FIXME: define some standard error codes for libervia |
173 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) | 160 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) |
174 return jsonrpc.JSONRPC.render(self, request) | 161 return jsonrpc.JSONRPC.render(self, request) |
175 | 162 |
176 def jsonrpc_getProfileJid(self): | 163 def jsonrpc_getProfileJid(self): |
177 """Return the jid of the profile""" | 164 """Return the jid of the profile""" |
404 | 391 |
405 def jsonrpc_getTarotCardsPaths(self): | 392 def jsonrpc_getTarotCardsPaths(self): |
406 """Give the path of all the tarot cards""" | 393 """Give the path of all the tarot cards""" |
407 _join = os.path.join | 394 _join = os.path.join |
408 _media_dir = _join(self.sat_host.media_dir,'') | 395 _media_dir = _join(self.sat_host.media_dir,'') |
409 return map(lambda x: _join(MEDIA_DIR, x[len(_media_dir):]),glob.glob(_join(_media_dir,CARDS_DIR,'*_*.png'))); | 396 return map(lambda x: _join(Const.MEDIA_DIR, x[len(_media_dir):]), glob.glob(_join(_media_dir, Const.CARDS_DIR, '*_*.png'))); |
410 | 397 |
411 def jsonrpc_tarotGameReady(self, player, referee): | 398 def jsonrpc_tarotGameReady(self, player, referee): |
412 """Tell to the server that we are ready to start the game""" | 399 """Tell to the server that we are ready to start the game""" |
413 profile = ISATSession(self.session).profile | 400 profile = ISATSession(self.session).profile |
414 self.sat_host.bridge.tarotGameReady(player, referee, profile) | 401 self.sat_host.bridge.tarotGameReady(player, referee, profile) |
452 return self.sat_host.bridge.getCard(jid, profile) | 439 return self.sat_host.bridge.getCard(jid, profile) |
453 | 440 |
454 def jsonrpc_getParamsUI(self): | 441 def jsonrpc_getParamsUI(self): |
455 """Return the parameters XML for profile""" | 442 """Return the parameters XML for profile""" |
456 profile = ISATSession(self.session).profile | 443 profile = ISATSession(self.session).profile |
457 d = self.asyncBridgeCall("getParams", SECURITY_LIMIT, profile) | 444 d = self.asyncBridgeCall("getParams", Const.SECURITY_LIMIT, profile) |
458 | 445 |
459 def setAuthorizedParams(d): | 446 def setAuthorizedParams(d): |
460 if self.authorized_params is None: | 447 if self.authorized_params is None: |
461 self.authorized_params = {} | 448 self.authorized_params = {} |
462 for cat in minidom.parseString(d.encode('utf-8')).getElementsByTagName("category"): | 449 for cat in minidom.parseString(d.encode('utf-8')).getElementsByTagName("category"): |
476 return d | 463 return d |
477 | 464 |
478 def jsonrpc_asyncGetParamA(self, param, category, attribute="value"): | 465 def jsonrpc_asyncGetParamA(self, param, category, attribute="value"): |
479 """Return the parameter value for profile""" | 466 """Return the parameter value for profile""" |
480 profile = ISATSession(self.session).profile | 467 profile = ISATSession(self.session).profile |
481 d = self.asyncBridgeCall("asyncGetParamA", param, category, attribute, SECURITY_LIMIT, profile_key=profile) | 468 d = self.asyncBridgeCall("asyncGetParamA", param, category, attribute, Const.SECURITY_LIMIT, profile_key=profile) |
482 return d | 469 return d |
483 | 470 |
484 def jsonrpc_setParam(self, name, value, category): | 471 def jsonrpc_setParam(self, name, value, category): |
485 profile = ISATSession(self.session).profile | 472 profile = ISATSession(self.session).profile |
486 if category in self.authorized_params and name in self.authorized_params[category]: | 473 if category in self.authorized_params and name in self.authorized_params[category]: |
487 return self.sat_host.bridge.setParam(name, value, category, SECURITY_LIMIT, profile) | 474 return self.sat_host.bridge.setParam(name, value, category, Const.SECURITY_LIMIT, profile) |
488 else: | 475 else: |
489 warning("Trying to set parameter '%s' in category '%s' without authorization!!!" | 476 warning("Trying to set parameter '%s' in category '%s' without authorization!!!" |
490 % (name, category)) | 477 % (name, category)) |
491 | 478 |
492 def jsonrpc_launchAction(self, callback_id, data): | 479 def jsonrpc_launchAction(self, callback_id, data): |
552 if parsed.get("method")!="isRegistered": | 539 if parsed.get("method")!="isRegistered": |
553 #if we don't call login or isRegistered, we need to be identified | 540 #if we don't call login or isRegistered, we need to be identified |
554 profile = ISATSession(_session).profile | 541 profile = ISATSession(_session).profile |
555 if not profile: | 542 if not profile: |
556 #user is not identified, we return a jsonrpc fault | 543 #user is not identified, we return a jsonrpc fault |
557 fault = jsonrpclib.Fault(ERRNUM_LIBERVIA, "Not allowed") #FIXME: define some standard error codes for libervia | 544 fault = jsonrpclib.Fault(Const.ERRNUM_LIBERVIA, "Not allowed") #FIXME: define some standard error codes for libervia |
558 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) | 545 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) |
559 self.request = request | 546 self.request = request |
560 return jsonrpc.JSONRPC.render(self, request) | 547 return jsonrpc.JSONRPC.render(self, request) |
561 | 548 |
562 def login(self, request): | 549 def login(self, request): |
823 _session = request.getSession() | 810 _session = request.getSession() |
824 parsed = jsonrpclib.loads(request.content.read()) | 811 parsed = jsonrpclib.loads(request.content.read()) |
825 profile = ISATSession(_session).profile | 812 profile = ISATSession(_session).profile |
826 if not profile: | 813 if not profile: |
827 #user is not identified, we return a jsonrpc fault | 814 #user is not identified, we return a jsonrpc fault |
828 fault = jsonrpclib.Fault(ERRNUM_LIBERVIA, "Not allowed") #FIXME: define some standard error codes for libervia | 815 fault = jsonrpclib.Fault(Const.ERRNUM_LIBERVIA, "Not allowed") #FIXME: define some standard error codes for libervia |
829 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) | 816 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) |
830 self.request = request | 817 self.request = request |
831 return jsonrpc.JSONRPC.render(self, request) | 818 return jsonrpc.JSONRPC.render(self, request) |
832 | 819 |
833 class UploadManager(Resource): | 820 class UploadManager(Resource): |
923 | 910 |
924 class Libervia(service.Service): | 911 class Libervia(service.Service): |
925 | 912 |
926 def __init__(self): | 913 def __init__(self): |
927 self._cleanup = [] | 914 self._cleanup = [] |
928 root = ProtectedFile(LIBERVIA_DIR) | 915 root = ProtectedFile(Const.LIBERVIA_DIR) |
929 self.signal_handler = SignalHandler(self) | 916 self.signal_handler = SignalHandler(self) |
930 _register = Register(self) | 917 _register = Register(self) |
931 _upload_radiocol = UploadManagerRadioCol(self) | 918 _upload_radiocol = UploadManagerRadioCol(self) |
932 _upload_avatar = UploadManagerAvatar(self) | 919 _upload_avatar = UploadManagerAvatar(self) |
933 self.signal_handler.plugRegister(_register) | 920 self.signal_handler.plugRegister(_register) |
961 root.putChild('register_api', _register) | 948 root.putChild('register_api', _register) |
962 root.putChild('upload_radiocol', _upload_radiocol) | 949 root.putChild('upload_radiocol', _upload_radiocol) |
963 root.putChild('upload_avatar', _upload_avatar) | 950 root.putChild('upload_avatar', _upload_avatar) |
964 root.putChild('blog', MicroBlog(self)) | 951 root.putChild('blog', MicroBlog(self)) |
965 root.putChild('css', ProtectedFile("server_css/")) | 952 root.putChild('css', ProtectedFile("server_css/")) |
966 root.putChild(os.path.dirname(MEDIA_DIR), ProtectedFile(self.media_dir)) | 953 root.putChild(os.path.dirname(Const.MEDIA_DIR), ProtectedFile(self.media_dir)) |
967 root.putChild(os.path.dirname(AVATARS_DIR), ProtectedFile(os.path.join(self.local_dir, AVATARS_DIR))) | 954 root.putChild(os.path.dirname(Const.AVATARS_DIR), ProtectedFile(os.path.join(self.local_dir, Const.AVATARS_DIR))) |
968 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 | 955 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 |
969 self.site = server.Site(root) | 956 self.site = server.Site(root) |
970 self.site.sessionFactory = LiberviaSession | 957 self.site.sessionFactory = LiberviaSession |
971 | 958 |
972 def addCleanup(self, callback, *args, **kwargs): | 959 def addCleanup(self, callback, *args, **kwargs): |
991 def stop(self): | 978 def stop(self): |
992 reactor.stop() | 979 reactor.stop() |
993 | 980 |
994 | 981 |
995 registerAdapter(SATSession, server.Session, ISATSession) | 982 registerAdapter(SATSession, server.Session, ISATSession) |
996 application = service.Application('Libervia') | 983 application = service.Application(Const.APP_NAME) |
997 service = Libervia() | 984 service = Libervia() |
998 service.setServiceParent(application) | 985 service.setServiceParent(application) |