Moved inlinefunc-parsing to sessionhandler level. First text throughput for webclient alongside telnet.

This commit is contained in:
Griatch 2016-02-10 22:27:01 +01:00
parent 2890371900
commit 1044006303
4 changed files with 49 additions and 31 deletions

View file

@ -23,12 +23,15 @@ from evennia.utils.utils import (variable_from_module, is_iter,
to_str, to_unicode,
make_iter,
callables_from_module)
from evennia.utils.inlinefunc import parse_inlinefunc
from evennia.utils.nested_inlinefuncs import parse_inlinefunc as parse_nested_inlinefunc
try:
import cPickle as pickle
except ImportError:
import pickle
_INLINEFUNC_ENABLED = settings.INLINEFUNC_ENABLED
# delayed imports
_PlayerDB = None
@ -129,7 +132,7 @@ class SessionHandler(dict):
def clean_senddata(self, session, kwargs):
"""
Clean up data for sending across the AMP wire.
Clean up data for sending across the AMP wire. Also apply INLINEFUNCS.
Args:
session (Session): The relevant session instance.
@ -146,9 +149,14 @@ class SessionHandler(dict):
Returns:
kwargs (dict): A cleaned dictionary of cmdname:[[args],{kwargs}] pairs,
where the keys, args and kwargs have all been converted to
send-safe entities (strings or numbers).
send-safe entities (strings or numbers), and inlinefuncs have been
applied.
"""
options = kwargs.get("options", None) or {}
raw = options.get("raw", False)
strip_inlinefunc = options.get("strip_inlinefunc", False)
def _validate(data):
"Helper function to convert data to AMP-safe (picketable) values"
if isinstance(data, dict):
@ -161,11 +169,15 @@ class SessionHandler(dict):
elif isinstance(data, basestring):
# make sure strings are in a valid encoding
try:
return data and to_str(to_unicode(data), encoding=session.encoding)
data = data and to_str(to_unicode(data), encoding=session.encoding)
except LookupError:
# wrong encoding set on the session. Set it to a safe one
session.encoding = "utf-8"
return to_str(to_unicode(data), encoding=session.encoding)
data = to_str(to_unicode(data), encoding=session.encoding)
if _INLINEFUNC_ENABLED and not raw:
data = parse_inlinefunc(data, strip=strip_inlinefunc, session=session) # deprecated!
data = parse_nested_inlinefunc(data, strip=strip_inlinefunc, session=session)
return data
elif hasattr(data, "id") and hasattr(data, "db_date_created") \
and hasattr(data, '__dbclass__'):
# convert database-object to their string representation.
@ -176,14 +188,20 @@ class SessionHandler(dict):
rkwargs = {}
for key, data in kwargs.iteritems():
print "sessionhandler.clean_senddata:", key, data
key = _validate(key)
if not data:
rkwargs[key] = [ [], {} ]
elif isinstance(data, dict):
rkwargs[key] = [ [], _validate(data) ]
elif hasattr(data, "__iter__"):
if isinstance(data[-1], dict):
# last entry is a kwarg dict
rkwargs[key] = [ _validate(data[:-1]), _validate(data[-1]) ]
if len(data) == 2:
if hasattr(data[0], "__iter__"):
rkwargs[key] = [_validate(data[0]), _validate(data[1])]
else:
rkwargs[key] = [[_validate(data[0])], _validate(data[1])]
else:
rkwargs[key] = [ _validate(data[:-1]), _validate(data[-1]) ]
else:
rkwargs[key] = [ _validate(data), {} ]
else:
@ -586,9 +604,11 @@ class ServerSessionHandler(SessionHandler):
the wire here.
"""
# clean output for sending
print "sessionhandler before clean_senddata:", kwargs
kwargs = self.clean_senddata(session, kwargs)
# send across AMP
print "sessionhandler.data_out:", kwargs
print "sessionhandler after clean_senddata:", kwargs
self.server.amp_protocol.send_MsgServer2Portal(session,
**kwargs)