From 6ef4467203b30a98a0ef082a560c3c7c8a6a0f75 Mon Sep 17 00:00:00 2001 From: Griatch Date: Sun, 26 Oct 2014 10:46:22 +0100 Subject: [PATCH] Added session object to kwarg of inlinefuncs. This means that the new call signature for inlinefuncs is funcname(text, *args, **kwargs), where **kwargs are only used for engine-related things (currently only session=session). --- src/server/serversession.py | 2 +- src/utils/inlinefunc.py | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/server/serversession.py b/src/server/serversession.py index 913f6f9b14..1a8896b750 100644 --- a/src/server/serversession.py +++ b/src/server/serversession.py @@ -237,7 +237,7 @@ class ServerSession(Session): """ text = text if text else "" if INLINEFUNC_ENABLED and not "raw" in kwargs: - text = parse_inlinefunc(text, strip="strip_inlinefunc" in kwargs) + text = parse_inlinefunc(text, strip="strip_inlinefunc" in kwargs, session=self) self.sessionhandler.data_out(self, text=text, **kwargs) def __eq__(self, other): diff --git a/src/utils/inlinefunc.py b/src/utils/inlinefunc.py index edfb06cd8c..50c044336c 100644 --- a/src/utils/inlinefunc.py +++ b/src/utils/inlinefunc.py @@ -50,7 +50,7 @@ from src.utils import utils # inline functions -def pad(text, *args): +def pad(text, *args, **kwargs): "Pad to width. pad(text, width=78, align='c', fillchar=' ')" width = 78 align = 'c' @@ -66,7 +66,7 @@ def pad(text, *args): break return utils.pad(text, width=width, align=align, fillchar=fillchar) -def crop(text, *args): +def crop(text, *args, **kwargs): "Crop to width. crop(text, width=78, suffix='[...]')" width = 78 suffix = "[...]" @@ -79,7 +79,7 @@ def crop(text, *args): break return utils.crop(text, width=width, suffix=suffix) -def wrap(text, *args): +def wrap(text, *args, **kwargs): "Wrap/Fill text to width. fill(text, width=78, indent=0)" width = 78 indent = 0 @@ -90,7 +90,7 @@ def wrap(text, *args): indent = int(arg) if arg.isdigit() else indent return utils.wrap(text, width=width, indent=indent) -def time(text, *args): +def time(text, *args, **kwargs): "Inserts current time" import time strformat = "%h %d, %H:%M" @@ -98,6 +98,14 @@ def time(text, *args): strformat = str(args[0]) return time.strftime(strformat) +def you(text, *args, **kwargs): + "Inserts your name" + name = "You" + sess = kwargs.get("session") + if sess and sess.puppet: + name = sess.puppet.key + return name + # load functions from module (including this one, if using default settings) _INLINE_FUNCS = {} @@ -123,7 +131,7 @@ _FUNCCLEAN_REGEX = re.compile("|".join([_RE_FUNCCLEAN % (key, key) for key in _I # inline parser functions -def _execute_inline_function(funcname, text): +def _execute_inline_function(funcname, text, session): """ Get the enclosed text between {funcname(...) and {/funcname and execute the inline function to replace the whole block @@ -137,15 +145,17 @@ def _execute_inline_function(funcname, text): "replace the entire block with the result of the function call" args = [part.strip() for part in match.group(1).split(",")] intext = match.group(2) - return _INLINE_FUNCS[funcname][0](intext, *args) + kwargs = {"session":session} + return _INLINE_FUNCS[funcname][0](intext, *args, **kwargs) return _INLINE_FUNCS[funcname][1].sub(subfunc, text) -def parse_inlinefunc(text, strip=False): +def parse_inlinefunc(text, strip=False, session=None): """ Parse inline function-replacement. strip - remove all supported inlinefuncs from text + session - session calling for the parsing """ if strip: @@ -165,7 +175,7 @@ def parse_inlinefunc(text, strip=False): if starttag: startname = starttag.group(1) if startname == endname: - part = _execute_inline_function(startname, part) + part = _execute_inline_function(startname, part, session) break stack.append(part) return "".join(stack)