Mercurial > libervia-web
diff src/server/server.py @ 451:1a0cec9b0f1e
better PEP-8 compliance
author | souliane <souliane@mailoo.org> |
---|---|
date | Tue, 20 May 2014 10:54:03 +0200 |
parents | 981ed669d3b3 |
children | da2a7f2b34c9 |
line wrap: on
line diff
--- a/src/server/server.py Tue May 20 07:22:10 2014 +0200 +++ b/src/server/server.py Tue May 20 10:54:03 2014 +0200 @@ -67,6 +67,7 @@ class SATSession(object): implements(ISATSession) + def __init__(self, session): self.profile = None self.jid = None @@ -93,15 +94,17 @@ if not self.__lock: server.Session.touch(self) + class ProtectedFile(File): """A File class which doens't show directory listing""" def directoryListing(self): return NoResource() + class SATActionIDHandler(object): """Manage SàT action action_id lifecycle""" - ID_LIFETIME = 30 #after this time (in seconds), action_id will be suppressed and action result will be ignored + ID_LIFETIME = 30 # after this time (in seconds), action_id will be suppressed and action result will be ignored def __init__(self): self.waiting_ids = {} @@ -120,7 +123,7 @@ def purgeID(self, action_tuple): """Called when an action_id has not be handled in time""" if action_tuple in self.waiting_ids: - log.warning ("action of action_id %s [%s] has not been managed, action_id is now ignored" % action_tuple) + log.warning("action of action_id %s [%s] has not been managed, action_id is now ignored" % action_tuple) del self.waiting_ids[action_tuple] def actionResultCb(self, answer_type, action_id, data, profile): @@ -131,11 +134,12 @@ del self.waiting_ids[action_tuple] callback(answer_type, action_id, data, *args, **kwargs) + class JSONRPCMethodManager(jsonrpc.JSONRPC): def __init__(self, sat_host): jsonrpc.JSONRPC.__init__(self) - self.sat_host=sat_host + self.sat_host = sat_host def asyncBridgeCall(self, method_name, *args, **kwargs): """Call an asynchrone bridge method and return a deferred @@ -174,7 +178,7 @@ if not profile: #user is not identified, we return a jsonrpc fault parsed = jsonrpclib.loads(request.content.read()) - fault = jsonrpclib.Fault(C.ERRNUM_LIBERVIA, "Not allowed") #FIXME: define some standard error codes for libervia + fault = jsonrpclib.Fault(C.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) @@ -233,7 +237,6 @@ profile = ISATSession(self.session).profile self.sat_host.bridge.setPresence('', presence, {'': status}, profile) - def jsonrpc_sendMessage(self, to_jid, msg, subject, type_, options={}): """send message""" profile = ISATSession(self.session).profile @@ -343,7 +346,6 @@ d = self.asyncBridgeCall("getGroupBlogComments", service, node, profile) return d - def jsonrpc_getPresenceStatuses(self): """Get Presence information for connected contacts""" profile = ISATSession(self.session).profile @@ -361,6 +363,7 @@ log.error("Trying to get history from a different jid, maybe a hack attempt ?") return {} d = self.asyncBridgeCall("getHistory", from_jid, to_jid, size, between, profile) + def show(result_dbus): result = [] for line in result_dbus: @@ -429,8 +432,8 @@ def jsonrpc_getTarotCardsPaths(self): """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(C.MEDIA_DIR, x[len(_media_dir):]), glob.glob(_join(_media_dir, C.CARDS_DIR, '*_*.png'))); + _media_dir = _join(self.sat_host.media_dir, '') + return map(lambda x: _join(C.MEDIA_DIR, x[len(_media_dir):]), glob.glob(_join(_media_dir, C.CARDS_DIR, '*_*.png'))) def jsonrpc_tarotGameReady(self, player, referee): """Tell to the server that we are ready to start the game""" @@ -554,12 +557,12 @@ def __init__(self, sat_host): JSONRPCMethodManager.__init__(self, sat_host) - self.profiles_waiting={} - self.request=None + self.profiles_waiting = {} + self.request = None def getWaitingRequest(self, profile): """Tell if a profile is trying to log in""" - if self.profiles_waiting.has_key(profile): + if profile in self.profiles_waiting: return self.profiles_waiting[profile] else: return None @@ -581,7 +584,7 @@ profile = ISATSession(_session).profile if not profile: #user is not identified, we return a jsonrpc fault - fault = jsonrpclib.Fault(C.ERRNUM_LIBERVIA, "Not allowed") #FIXME: define some standard error codes for libervia + fault = jsonrpclib.Fault(C.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) @@ -743,8 +746,8 @@ def jsonrpc_connect(self): _session = self.request.getSession() profile = ISATSession(_session).profile - if self.profiles_waiting.has_key(profile): - raise jsonrpclib.Fault(1,'Already waiting') #FIXME: define some standard error codes for libervia + if profile in self.profiles_waiting: + raise jsonrpclib.Fault(1, 'Already waiting') # FIXME: define some standard error codes for libervia self.profiles_waiting[profile] = self.request self.sat_host.bridge.connect(profile) return server.NOT_DONE_YET @@ -801,8 +804,8 @@ def __init__(self, sat_host): Resource.__init__(self) - self.register=None - self.sat_host=sat_host + self.register = None + self.sat_host = sat_host self.signalDeferred = {} self.queue = {} @@ -814,13 +817,14 @@ @return: (signal, *signal_args)""" _session = self.request.getSession() profile = ISATSession(_session).profile - if profile in self.queue: #if we have signals to send in queue + if profile in self.queue: # if we have signals to send in queue if self.queue[profile]: return self.queue[profile].pop(0) else: #the queue is empty, we delete the profile from queue del self.queue[profile] - _session.lock() #we don't want the session to expire as long as this connection is active + _session.lock() # we don't want the session to expire as long as this connection is active + def unlock(signal, profile): _session.unlock() try: @@ -846,7 +850,7 @@ self.signalDeferred[profile].callback((function_name,args[:-1])) del self.signalDeferred[profile] else: - if not self.queue.has_key(profile): + if profile not in self.queue: self.queue[profile] = [] self.queue[profile].append((function_name, args[:-1])) return genericCb @@ -866,15 +870,14 @@ self.signalDeferred[profile].callback(("disconnected",)) del self.signalDeferred[profile] else: - if not self.queue.has_key(profile): + if profile not in self.queue: self.queue[profile] = [] self.queue[profile].append(("disconnected",)) - def connectionError(self, error_type, profile): - assert(self.register) #register must be plugged + assert(self.register) # register must be plugged request = self.register.getWaitingRequest(profile) - if request: #The user is trying to log in + if request: # The user is trying to log in if error_type == "AUTH_ERROR": _error_t = "AUTH ERROR" else: @@ -890,19 +893,20 @@ profile = ISATSession(_session).profile if not profile: #user is not identified, we return a jsonrpc fault - fault = jsonrpclib.Fault(C.ERRNUM_LIBERVIA, "Not allowed") #FIXME: define some standard error codes for libervia + fault = jsonrpclib.Fault(C.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) + class UploadManager(Resource): """This class manage the upload of a file It redirect the stream to SàT core backend""" isLeaf = True - NAME = 'path' #name use by the FileUpload + NAME = 'path' # name use by the FileUpload def __init__(self, sat_host): - self.sat_host=sat_host + self.sat_host = sat_host self.upload_dir = tempfile.mkdtemp() self.sat_host.addCleanup(shutil.rmtree, self.upload_dir) @@ -936,11 +940,11 @@ # be written in the futur. In addition, it is not yet possible to get progression informations # (see http://twistedmatrix.com/trac/ticket/288) - with open(filepath,'w') as f: + with open(filepath, 'w') as f: f.write(request.args[self.NAME][0]) def finish(d): - error = isinstance(d, Exception) or isinstance (d, Failure) + error = isinstance(d, Exception) or isinstance(d, Failure) request.write('KO' if error else 'OK') # TODO: would be great to re-use the original Exception class and message # but it is lost in the middle of the backtrace and encapsulated within @@ -1048,15 +1052,16 @@ _upload_radiocol = UploadManagerRadioCol(self) _upload_avatar = UploadManagerAvatar(self) self.signal_handler.plugRegister(_register) - self.sessions = {} #key = session value = user - self.prof_connected = set() #Profiles connected + self.sessions = {} # key = session value = user + self.prof_connected = set() # Profiles connected self.action_handler = SATActionIDHandler() ## bridge ## try: - self.bridge=DBusBridgeFrontend() + self.bridge = DBusBridgeFrontend() except BridgeExceptionNoService: print(u"Can't connect to SàT backend, are you sure it's launched ?") sys.exit(1) + def backendReady(dummy): self.bridge.register("connected", self.signal_handler.connected) self.bridge.register("disconnected", self.signal_handler.disconnected)