mirror of
https://github.com/evennia/evennia.git
synced 2026-04-05 07:27:17 +02:00
PEP8 cleanup of the entire codebase. Unchanged are many cases of too-long lines, partly because of the rewrite they would require but also because splitting many lines up would make the code harder to read. Also the third-party libraries (idmapper, prettytable etc) were not cleaned.
This commit is contained in:
parent
30b7d2a405
commit
1ae17bcbe4
154 changed files with 5613 additions and 4054 deletions
|
|
@ -1,17 +1,18 @@
|
|||
"""
|
||||
Contains the protocols, commands, and client factory needed for the Server and Portal
|
||||
to communicate with each other, letting Portal work as a proxy. Both sides use this
|
||||
same protocol.
|
||||
Contains the protocols, commands, and client factory needed for the Server
|
||||
and Portal to communicate with each other, letting Portal work as a proxy.
|
||||
Both sides use this same protocol.
|
||||
|
||||
The separation works like this:
|
||||
|
||||
Portal - (AMP client) handles protocols. It contains a list of connected sessions in a
|
||||
dictionary for identifying the respective player connected. If it looses the AMP connection
|
||||
it will automatically try to reconnect.
|
||||
Portal - (AMP client) handles protocols. It contains a list of connected
|
||||
sessions in a dictionary for identifying the respective player
|
||||
connected. If it looses the AMP connection it will automatically
|
||||
try to reconnect.
|
||||
|
||||
Server - (AMP server) Handles all mud operations. The server holds its own list
|
||||
of sessions tied to player objects. This is synced against the portal at startup
|
||||
and when a session connects/disconnects
|
||||
of sessions tied to player objects. This is synced against the portal
|
||||
at startup and when a session connects/disconnects
|
||||
|
||||
"""
|
||||
|
||||
|
|
@ -29,16 +30,17 @@ from src.utils.utils import to_str, variable_from_module
|
|||
|
||||
# communication bits
|
||||
|
||||
PCONN = chr(1) # portal session connect
|
||||
PDISCONN = chr(2) # portal session disconnect
|
||||
PSYNC = chr(3) # portal session sync
|
||||
SLOGIN = chr(4) # server session login
|
||||
SDISCONN = chr(5) # server session disconnect
|
||||
SDISCONNALL = chr(6) # server session disconnect all
|
||||
SSHUTD = chr(7) # server shutdown
|
||||
SSYNC = chr(8) # server sessigon sync
|
||||
PCONN = chr(1) # portal session connect
|
||||
PDISCONN = chr(2) # portal session disconnect
|
||||
PSYNC = chr(3) # portal session sync
|
||||
SLOGIN = chr(4) # server session login
|
||||
SDISCONN = chr(5) # server session disconnect
|
||||
SDISCONNALL = chr(6) # server session disconnect all
|
||||
SSHUTD = chr(7) # server shutdown
|
||||
SSYNC = chr(8) # server sessigon sync
|
||||
|
||||
MAXLEN = 65535 # max allowed data length in AMP protocol
|
||||
|
||||
MAXLEN = 65535 # max allowed data length in AMP protocol
|
||||
|
||||
def get_restart_mode(restart_file):
|
||||
"""
|
||||
|
|
@ -49,6 +51,7 @@ def get_restart_mode(restart_file):
|
|||
return flag == "True"
|
||||
return False
|
||||
|
||||
|
||||
class AmpServerFactory(protocol.ServerFactory):
|
||||
"""
|
||||
This factory creates the Server as a new AMPProtocol instance for accepting
|
||||
|
|
@ -71,9 +74,11 @@ class AmpServerFactory(protocol.ServerFactory):
|
|||
self.server.amp_protocol.factory = self
|
||||
return self.server.amp_protocol
|
||||
|
||||
|
||||
class AmpClientFactory(protocol.ReconnectingClientFactory):
|
||||
"""
|
||||
This factory creates an instance of the Portal, an AMPProtocol instances to use to connect
|
||||
This factory creates an instance of the Portal, an AMPProtocol
|
||||
instances to use to connect
|
||||
"""
|
||||
# Initial reconnect delay in seconds.
|
||||
initialDelay = 1
|
||||
|
|
@ -139,6 +144,7 @@ class MsgPortal2Server(amp.Command):
|
|||
errors = [(Exception, 'EXCEPTION')]
|
||||
response = []
|
||||
|
||||
|
||||
class MsgServer2Portal(amp.Command):
|
||||
"""
|
||||
Message server -> portal
|
||||
|
|
@ -151,6 +157,7 @@ class MsgServer2Portal(amp.Command):
|
|||
errors = [(Exception, 'EXCEPTION')]
|
||||
response = []
|
||||
|
||||
|
||||
class ServerAdmin(amp.Command):
|
||||
"""
|
||||
Portal -> Server
|
||||
|
|
@ -165,6 +172,7 @@ class ServerAdmin(amp.Command):
|
|||
errors = [(Exception, 'EXCEPTION')]
|
||||
response = []
|
||||
|
||||
|
||||
class PortalAdmin(amp.Command):
|
||||
"""
|
||||
Server -> Portal
|
||||
|
|
@ -178,6 +186,7 @@ class PortalAdmin(amp.Command):
|
|||
errors = [(Exception, 'EXCEPTION')]
|
||||
response = []
|
||||
|
||||
|
||||
class FunctionCall(amp.Command):
|
||||
"""
|
||||
Bidirectional
|
||||
|
|
@ -202,6 +211,7 @@ loads = lambda data: pickle.loads(to_str(data))
|
|||
|
||||
MSGBUFFER = defaultdict(list)
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Core AMP protocol for communication Server <-> Portal
|
||||
#------------------------------------------------------------
|
||||
|
|
@ -241,7 +251,8 @@ class AMPProtocol(amp.AMP):
|
|||
def errback(self, e, info):
|
||||
"error handler, to avoid dropping connections on server tracebacks."
|
||||
f = e.trap(Exception)
|
||||
print "AMP Error for %(info)s: %(e)s" % {'info': info, 'e': e.getErrorMessage()}
|
||||
print "AMP Error for %(info)s: %(e)s" % {'info': info,
|
||||
'e': e.getErrorMessage()}
|
||||
|
||||
def send_split_msg(self, sessid, msg, data, command):
|
||||
"""
|
||||
|
|
@ -258,14 +269,16 @@ class AMPProtocol(amp.AMP):
|
|||
datastr = dumps(data)
|
||||
nmsg, ndata = len(msg), len(datastr)
|
||||
if nmsg > MAXLEN or ndata > MAXLEN:
|
||||
msglist = [msg[i:i+MAXLEN] for i in range(0, len(msg), MAXLEN)]
|
||||
datalist = [datastr[i:i+MAXLEN] for i in range(0, len(datastr), MAXLEN)]
|
||||
msglist = [msg[i:i + MAXLEN] for i in range(0, len(msg), MAXLEN)]
|
||||
datalist = [datastr[i:i + MAXLEN]
|
||||
for i in range(0, len(datastr), MAXLEN)]
|
||||
nmsglist, ndatalist = len(msglist), len(datalist)
|
||||
if ndatalist < nmsglist:
|
||||
datalist.extend("" for i in range(nmsglist-ndatalist))
|
||||
datalist.extend("" for i in range(nmsglist - ndatalist))
|
||||
if nmsglist < ndatalist:
|
||||
msglist.extend("" for i in range(ndatalist-nmsglist))
|
||||
# we have split the msg/data into right-size chunks. Now we send it in sequence
|
||||
msglist.extend("" for i in range(ndatalist - nmsglist))
|
||||
# we have split the msg/data into right-size chunks. Now we
|
||||
# send it in sequence
|
||||
return [self.callRemote(command,
|
||||
sessid=sessid,
|
||||
msg=to_str(msg),
|
||||
|
|
@ -295,8 +308,8 @@ class AMPProtocol(amp.AMP):
|
|||
return {}
|
||||
else:
|
||||
# we have all parts. Put it all together in the right order.
|
||||
msg = "".join(t[1] for t in sorted(MSGBUFFER[sessid], key=lambda o:o[0]))
|
||||
data = "".join(t[2] for t in sorted(MSGBUFFER[sessid], key=lambda o:o[0]))
|
||||
msg = "".join(t[1] for t in sorted(MSGBUFFER[sessid], key=lambda o: o[0]))
|
||||
data = "".join(t[2] for t in sorted(MSGBUFFER[sessid], key=lambda o: o[0]))
|
||||
del MSGBUFFER[sessid]
|
||||
# call session hook with the data
|
||||
self.factory.server.sessions.data_in(sessid, text=msg, **loads(data))
|
||||
|
|
@ -311,13 +324,14 @@ class AMPProtocol(amp.AMP):
|
|||
try:
|
||||
return self.callRemote(MsgPortal2Server,
|
||||
sessid=sessid,
|
||||
msg=to_str(msg) if msg!=None else "",
|
||||
msg=to_str(msg) if msg is not None else "",
|
||||
ipart=0,
|
||||
nparts=1,
|
||||
data=dumps(data)).addErrback(self.errback, "MsgPortal2Server")
|
||||
except amp.TooLong:
|
||||
# the msg (or data) was too long for AMP to send. We need to send in blocks.
|
||||
return self.send_split_msg(sessid, msg, kwargs, MsgPortal2Server)
|
||||
# the msg (or data) was too long for AMP to send.
|
||||
# We need to send in blocks.
|
||||
return self.send_split_msg(sessid, msg, data, MsgPortal2Server)
|
||||
|
||||
# Server -> Portal message
|
||||
|
||||
|
|
@ -335,8 +349,8 @@ class AMPProtocol(amp.AMP):
|
|||
return {}
|
||||
else:
|
||||
# we have all parts. Put it all together in the right order.
|
||||
msg = "".join(t[1] for t in sorted(MSGBUFFER[sessid], key=lambda o:o[0]))
|
||||
data = "".join(t[2] for t in sorted(MSGBUFFER[sessid], key=lambda o:o[0]))
|
||||
msg = "".join(t[1] for t in sorted(MSGBUFFER[sessid], key=lambda o: o[0]))
|
||||
data = "".join(t[2] for t in sorted(MSGBUFFER[sessid], key=lambda o: o[0]))
|
||||
del MSGBUFFER[sessid]
|
||||
# call session hook with the data
|
||||
self.factory.portal.sessions.data_out(sessid, text=msg, **loads(data))
|
||||
|
|
@ -351,14 +365,14 @@ class AMPProtocol(amp.AMP):
|
|||
try:
|
||||
return self.callRemote(MsgServer2Portal,
|
||||
sessid=sessid,
|
||||
msg=to_str(msg) if msg!=None else "",
|
||||
msg=to_str(msg) if msg is not None else "",
|
||||
ipart=0,
|
||||
nparts=1,
|
||||
data=dumps(data)).addErrback(self.errback, "MsgServer2Portal")
|
||||
except amp.TooLong:
|
||||
# the msg (or data) was too long for AMP to send. We need to send in blocks.
|
||||
return self.send_split_msg(sessid, msg, kwargs, MsgServer2Portal)
|
||||
|
||||
# the msg (or data) was too long for AMP to send.
|
||||
# We need to send in blocks.
|
||||
return self.send_split_msg(sessid, msg, data, MsgServer2Portal)
|
||||
|
||||
# Server administration from the Portal side
|
||||
def amp_server_admin(self, sessid, operation, data):
|
||||
|
|
@ -372,17 +386,18 @@ class AMPProtocol(amp.AMP):
|
|||
|
||||
#print "serveradmin (server side):", sessid, ord(operation), data
|
||||
|
||||
if operation == PCONN: #portal_session_connect
|
||||
if operation == PCONN: # portal_session_connect
|
||||
# create a new session and sync it
|
||||
server_sessionhandler.portal_connect(data)
|
||||
|
||||
elif operation == PDISCONN: #'portal_session_disconnect'
|
||||
elif operation == PDISCONN: # portal_session_disconnect
|
||||
# session closed from portal side
|
||||
self.factory.server.sessions.portal_disconnect(sessid)
|
||||
|
||||
elif operation == PSYNC: #'portal_session_sync'
|
||||
# force a resync of sessions when portal reconnects to server (e.g. after a server reboot)
|
||||
# the data kwarg contains a dict {sessid: {arg1:val1,...}} representing the attributes
|
||||
elif operation == PSYNC: # portal_session_sync
|
||||
# force a resync of sessions when portal reconnects to server
|
||||
# (e.g. after a server reboot) the data kwarg contains a dict
|
||||
# {sessid: {arg1:val1,...}} representing the attributes
|
||||
# to sync for each session.
|
||||
server_sessionhandler.portal_session_sync(data)
|
||||
else:
|
||||
|
|
@ -414,23 +429,23 @@ class AMPProtocol(amp.AMP):
|
|||
portal_sessionhandler = self.factory.portal.sessions
|
||||
|
||||
#print "portaladmin (portal side):", sessid, ord(operation), data
|
||||
if operation == SLOGIN: # 'server_session_login'
|
||||
if operation == SLOGIN: # server_session_login
|
||||
# a session has authenticated; sync it.
|
||||
portal_sessionhandler.server_logged_in(sessid, data)
|
||||
|
||||
elif operation == SDISCONN: #'server_session_disconnect'
|
||||
elif operation == SDISCONN: # server_session_disconnect
|
||||
# the server is ordering to disconnect the session
|
||||
portal_sessionhandler.server_disconnect(sessid, reason=data)
|
||||
|
||||
elif operation == SDISCONNALL: #'server_session_disconnect_all'
|
||||
elif operation == SDISCONNALL: # server_session_disconnect_all
|
||||
# server orders all sessions to disconnect
|
||||
portal_sessionhandler.server_disconnect_all(reason=data)
|
||||
|
||||
elif operation == SSHUTD: #server_shutdown'
|
||||
elif operation == SSHUTD: # server_shutdown
|
||||
# the server orders the portal to shut down
|
||||
self.factory.portal.shutdown(restart=False)
|
||||
|
||||
elif operation == SSYNC: #'server_session_sync'
|
||||
elif operation == SSYNC: # server_session_sync
|
||||
# server wants to save session data to the portal, maybe because
|
||||
# it's about to shut down.
|
||||
portal_sessionhandler.server_session_sync(data)
|
||||
|
|
@ -465,25 +480,28 @@ class AMPProtocol(amp.AMP):
|
|||
result = variable_from_module(module, function)(*args, **kwargs)
|
||||
|
||||
if isinstance(result, Deferred):
|
||||
# if result is a deferred, attach handler to properly wrap the return value
|
||||
result.addCallback(lambda r: {"result":dumps(r)})
|
||||
# if result is a deferred, attach handler to properly
|
||||
# wrap the return value
|
||||
result.addCallback(lambda r: {"result": dumps(r)})
|
||||
return result
|
||||
else:
|
||||
return {'result':dumps(result)}
|
||||
return {'result': dumps(result)}
|
||||
FunctionCall.responder(amp_function_call)
|
||||
|
||||
|
||||
def call_remote_FunctionCall(self, modulepath, functionname, *args, **kwargs):
|
||||
"""
|
||||
Access method called by either process. This will call an arbitrary function
|
||||
on the other process (On Portal if calling from Server and vice versa).
|
||||
Access method called by either process. This will call an arbitrary
|
||||
function on the other process (On Portal if calling from Server and
|
||||
vice versa).
|
||||
|
||||
Inputs:
|
||||
modulepath (str) - python path to module holding function to call
|
||||
functionname (str) - name of function in given module
|
||||
*args, **kwargs will be used as arguments/keyword args for the remote function call
|
||||
*args, **kwargs will be used as arguments/keyword args for the
|
||||
remote function call
|
||||
Returns:
|
||||
A deferred that fires with the return value of the remote function call
|
||||
A deferred that fires with the return value of the remote
|
||||
function call
|
||||
"""
|
||||
return self.callRemote(FunctionCall,
|
||||
module=modulepath,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue