Format code with black. Add makefile to run fmt/tests

This commit is contained in:
Griatch 2019-09-28 18:18:11 +02:00
parent d00bce9288
commit c2c7fa311a
299 changed files with 19037 additions and 11611 deletions

View file

@ -17,8 +17,13 @@ import time
from django.conf import settings
from evennia.commands.cmdhandler import CMD_LOGINSTART
from evennia.utils.logger import log_trace
from evennia.utils.utils import (variable_from_module, is_iter,
make_iter, delay, callables_from_module)
from evennia.utils.utils import (
variable_from_module,
is_iter,
make_iter,
delay,
callables_from_module,
)
from evennia.server.signals import SIGNAL_ACCOUNT_POST_LOGIN, SIGNAL_ACCOUNT_POST_LOGOUT
from evennia.server.signals import SIGNAL_ACCOUNT_POST_FIRST_LOGIN, SIGNAL_ACCOUNT_POST_LAST_LOGOUT
from evennia.utils.inlinefuncs import parse_inlinefunc
@ -33,7 +38,7 @@ _ServerConfig = None
_ScriptDB = None
_OOB_HANDLER = None
_ERR_BAD_UTF8 = 'Your client sent an incorrect UTF-8 sequence.'
_ERR_BAD_UTF8 = "Your client sent an incorrect UTF-8 sequence."
class DummySession(object):
@ -43,23 +48,23 @@ class DummySession(object):
DUMMYSESSION = DummySession()
# AMP signals
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
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 session sync
SCONN = chr(11) # server portal connection (for bots)
PCONNSYNC = chr(12) # portal post-syncing session
SSHUTD = chr(7) # server shutdown
SSYNC = chr(8) # server session sync
SCONN = chr(11) # server portal connection (for bots)
PCONNSYNC = chr(12) # portal post-syncing session
PDISCONNALL = chr(13) # portal session discnnect all
SRELOAD = chr(14) # server reloading (have portal start a new server)
SSTART = chr(15) # server start (portal must already be running anyway)
PSHUTD = chr(16) # portal (+server) shutdown
SSHUTD = chr(17) # server shutdown
PSTATUS = chr(18) # ping server or portal status
SRESET = chr(19) # server shutdown in reset mode
SRELOAD = chr(14) # server reloading (have portal start a new server)
SSTART = chr(15) # server start (portal must already be running anyway)
PSHUTD = chr(16) # portal (+server) shutdown
SSHUTD = chr(17) # server shutdown
PSTATUS = chr(18) # ping server or portal status
SRESET = chr(19) # server shutdown in reset mode
# i18n
from django.utils.translation import ugettext as _
@ -96,15 +101,16 @@ def delayed_import():
if not _ScriptDB:
from evennia.scripts.models import ScriptDB as _ScriptDB
# including once to avoid warnings in Python syntax checkers
assert(_ServerSession)
assert(_AccountDB)
assert(_ServerConfig)
assert(_ScriptDB)
assert _ServerSession
assert _AccountDB
assert _ServerConfig
assert _ScriptDB
#-----------------------------------------------------------
# -----------------------------------------------------------
# SessionHandler base class
#------------------------------------------------------------
# ------------------------------------------------------------
class SessionHandler(dict):
"""
@ -199,7 +205,7 @@ class SessionHandler(dict):
except UnicodeDecodeError:
# incorrect unicode sequence
session.sendLine(_ERR_BAD_UTF8)
data = ''
data = ""
return data
@ -220,8 +226,11 @@ class SessionHandler(dict):
data = parse_inlinefunc(data, strip=strip_inlinefunc, session=session)
return str(data)
elif hasattr(data, "id") and hasattr(data, "db_date_created") \
and hasattr(data, '__dbclass__'):
elif (
hasattr(data, "id")
and hasattr(data, "db_date_created")
and hasattr(data, "__dbclass__")
):
# convert database-object to their string representation.
return _validate(str(data))
else:
@ -259,6 +268,7 @@ class SessionHandler(dict):
# Server-SessionHandler class
# ------------------------------------------------------------
class ServerSessionHandler(SessionHandler):
"""
This object holds the stack of sessions active in the game at
@ -382,7 +392,7 @@ class ServerSessionHandler(SessionHandler):
self[sessid] = sess
sess.at_sync()
mode = 'reload'
mode = "reload"
# tell the server hook we synced
self.server.at_post_portal_sync(mode)
@ -440,9 +450,9 @@ class ServerSessionHandler(SessionHandler):
the Server.
"""
self.server.amp_protocol.send_AdminServer2Portal(DUMMYSESSION, operation=SCONN,
protocol_path=protocol_path,
config=configdict)
self.server.amp_protocol.send_AdminServer2Portal(
DUMMYSESSION, operation=SCONN, protocol_path=protocol_path, config=configdict
)
def portal_restart_server(self):
"""
@ -464,8 +474,7 @@ class ServerSessionHandler(SessionHandler):
itself down)
"""
self.server.amp_protocol.send_AdminServer2Portal(DUMMYSESSION,
operation=PSHUTD)
self.server.amp_protocol.send_AdminServer2Portal(DUMMYSESSION, operation=PSHUTD)
def login(self, session, account, force=False, testmode=False):
"""
@ -512,10 +521,9 @@ class ServerSessionHandler(SessionHandler):
session.logged_in = True
# sync the portal to the session
if not testmode:
self.server.amp_protocol.send_AdminServer2Portal(session,
operation=SLOGIN,
sessiondata={"logged_in": True,
"uid": session.uid})
self.server.amp_protocol.send_AdminServer2Portal(
session, operation=SLOGIN, sessiondata={"logged_in": True, "uid": session.uid}
)
account.at_post_login(session=session)
if nsess < 2:
SIGNAL_ACCOUNT_POST_FIRST_LOGIN.send(sender=account, session=session)
@ -543,8 +551,9 @@ class ServerSessionHandler(SessionHandler):
nsess = len(self.sessions_from_account(session.account)) - 1
sreason = " ({})".format(reason) if reason else ""
string = "Logged out: {account} {address} ({nsessions} sessions(s) remaining){reason}"
string = string.format(reason=sreason, account=session.account,
address=session.address, nsessions=nsess)
string = string.format(
reason=sreason, account=session.account, address=session.address, nsessions=nsess
)
session.log(string)
if nsess == 0:
@ -557,9 +566,9 @@ class ServerSessionHandler(SessionHandler):
del self[sessid]
if sync_portal:
# inform portal that session should be closed.
self.server.amp_protocol.send_AdminServer2Portal(session,
operation=SDISCONN,
reason=reason)
self.server.amp_protocol.send_AdminServer2Portal(
session, operation=SDISCONN, reason=reason
)
def all_sessions_portal_sync(self):
"""
@ -568,9 +577,9 @@ class ServerSessionHandler(SessionHandler):
"""
sessdata = self.get_all_sync_data()
return self.server.amp_protocol.send_AdminServer2Portal(DUMMYSESSION,
operation=SSYNC,
sessiondata=sessdata)
return self.server.amp_protocol.send_AdminServer2Portal(
DUMMYSESSION, operation=SSYNC, sessiondata=sessdata
)
def session_portal_sync(self, session):
"""
@ -579,10 +588,9 @@ class ServerSessionHandler(SessionHandler):
"""
sessdata = {session.sessid: session.get_sync_data()}
return self.server.amp_protocol.send_AdminServer2Portal(DUMMYSESSION,
operation=SSYNC,
sessiondata=sessdata,
clean=False)
return self.server.amp_protocol.send_AdminServer2Portal(
DUMMYSESSION, operation=SSYNC, sessiondata=sessdata, clean=False
)
def session_portal_partial_sync(self, session_data):
"""
@ -593,10 +601,9 @@ class ServerSessionHandler(SessionHandler):
more sessions in detail.
"""
return self.server.amp_protocol.send_AdminServer2Portal(DUMMYSESSION,
operation=SSYNC,
sessiondata=session_data,
clean=False)
return self.server.amp_protocol.send_AdminServer2Portal(
DUMMYSESSION, operation=SSYNC, sessiondata=session_data, clean=False
)
def disconnect_all_sessions(self, reason="You have been disconnected."):
"""
@ -610,12 +617,13 @@ class ServerSessionHandler(SessionHandler):
for session in self:
del session
# tell portal to disconnect all sessions
self.server.amp_protocol.send_AdminServer2Portal(DUMMYSESSION,
operation=SDISCONNALL,
reason=reason)
self.server.amp_protocol.send_AdminServer2Portal(
DUMMYSESSION, operation=SDISCONNALL, reason=reason
)
def disconnect_duplicate_sessions(self, curr_session,
reason=_("Logged in from elsewhere. Disconnecting.")):
def disconnect_duplicate_sessions(
self, curr_session, reason=_("Logged in from elsewhere. Disconnecting.")
):
"""
Disconnects any existing sessions with the same user.
@ -628,10 +636,9 @@ class ServerSessionHandler(SessionHandler):
# we can't compare sessions directly since this will compare addresses and
# mean connecting from the same host would not catch duplicates
sid = id(curr_session)
doublet_sessions = [sess for sess in self.values()
if sess.logged_in and
sess.uid == uid and
id(sess) != sid]
doublet_sessions = [
sess for sess in self.values() if sess.logged_in and sess.uid == uid and id(sess) != sid
]
for session in doublet_sessions:
self.disconnect(session, reason)
@ -644,9 +651,13 @@ class ServerSessionHandler(SessionHandler):
"""
tcurr = time.time()
reason = _("Idle timeout exceeded, disconnecting.")
for session in (session for session in self.values()
if session.logged_in and _IDLE_TIMEOUT > 0 and
(tcurr - session.cmd_last) > _IDLE_TIMEOUT):
for session in (
session
for session in self.values()
if session.logged_in
and _IDLE_TIMEOUT > 0
and (tcurr - session.cmd_last) > _IDLE_TIMEOUT
):
self.disconnect(session, reason=reason)
def account_count(self):
@ -670,8 +681,13 @@ class ServerSessionHandler(SessionHandler):
amount of Sessions due to multi-playing).
"""
return list(set(session.account for session in self.values()
if session.logged_in and session.account))
return list(
set(
session.account
for session in self.values()
if session.logged_in and session.account
)
)
def session_from_sessid(self, sessid):
"""
@ -702,8 +718,11 @@ class ServerSessionHandler(SessionHandler):
sessions (Session or list): Session(s) found.
"""
sessions = [self[sid] for sid in make_iter(sessid)
if sid in self and self[sid].logged_in and account.uid == self[sid].uid]
sessions = [
self[sid]
for sid in make_iter(sessid)
if sid in self and self[sid].logged_in and account.uid == self[sid].uid
]
return sessions[0] if len(sessions) == 1 else sessions
def sessions_from_account(self, account):
@ -734,6 +753,7 @@ class ServerSessionHandler(SessionHandler):
"""
sessions = puppet.sessid.get()
return sessions[0] if len(sessions) == 1 else sessions
sessions_from_character = sessions_from_puppet
def sessions_from_csessid(self, csessid):
@ -749,8 +769,9 @@ class ServerSessionHandler(SessionHandler):
"""
if csessid:
return []
return [session for session in self.values()
if session.csessid and session.csessid == csessid]
return [
session for session in self.values() if session.csessid and session.csessid == csessid
]
def announce_all(self, message):
"""
@ -779,8 +800,7 @@ class ServerSessionHandler(SessionHandler):
kwargs = self.clean_senddata(session, kwargs)
# send across AMP
self.server.amp_protocol.send_MsgServer2Portal(session,
**kwargs)
self.server.amp_protocol.send_MsgServer2Portal(session, **kwargs)
def get_inputfuncs(self):
"""