OOB passing data client->server. A few more default commands are needed.

This commit is contained in:
Griatch 2013-10-07 19:57:01 +02:00
parent 96c6ad4aff
commit 16bbe009c3
6 changed files with 103 additions and 48 deletions

View file

@ -27,6 +27,7 @@ _ServerSession = None
_ServerConfig = None
_ScriptDB = None
# AMP signals
PCONN = chr(1) # portal session connect
PDISCONN = chr(2) # portal session disconnect
@ -357,6 +358,49 @@ class ServerSessionHandler(SessionHandler):
return self.sessions.get(sessid)
return None
def oobstruct_parser(self, oobstruct):
"""
Helper method for each session to use to parse oob structures
(The 'oob' kwarg of the msg() method)
allowed oob structures are
cmdname
(cmdname, cmdname)
(cmdname,(arg, ))
(cmdname,(arg1,arg2))
(cmdname,{key:val,key2:val2})
(cmdname, (args,), {kwargs})
((cmdname, (arg1,arg2)), cmdname, (cmdname, (arg1,)))
outputs an ordered structure on the form
((cmdname, (args,), {kwargs}), ...), where the two last parts of each tuple may be empty
"""
slen = len(oobstruct)
if not oobstruct:
return tuple(None, (), {})
elif not hasattr(oobstruct, "__iter__"):
# a singular command name, without arguments or kwargs
return (oobstruct.lower(), (), {})
# regardless of number of args/kwargs, the first element must be the function name.
# we will not catch this error if not, but allow it to propagate.
if slen == 1:
return (oobstruct[0].lower(), (), {})
elif slen == 2:
if isinstance(oobstruct[1], dict):
# cmdname, {kwargs}
return (oobstruct[0].lower(), (), dict((key.lower(), val) for key,val in oobstruct[1].items()))
elif isinstance(oobstruct[1], (tuple, list)):
# cmdname, (args,)
return (oobstruct[0].lower(), tuple(arg.lower() for arg in oobstruct[1]), {})
else:
# cmdname, (args,), {kwargs}
return (oobstruct[0].lower(), tuple(arg.lower for arg in oobstruct[1]),
dict((key.lower(), val) for key, val in oobstruct[2].items()))
# either multiple funcnames or multiple func tuples; descend recursively
out = []
for oobpart in oobstruct:
out.append(self.oobstruct_parser(oobpart)[0])
return tuple(out)
def announce_all(self, message):
"""
Send message to all connected sessions
@ -379,5 +423,4 @@ class ServerSessionHandler(SessionHandler):
if session:
session.data_in(text=text, **kwargs)
SESSIONS = ServerSessionHandler()