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).
This commit is contained in:
parent
5b42b31240
commit
6ef4467203
2 changed files with 19 additions and 9 deletions
|
|
@ -237,7 +237,7 @@ class ServerSession(Session):
|
||||||
"""
|
"""
|
||||||
text = text if text else ""
|
text = text if text else ""
|
||||||
if INLINEFUNC_ENABLED and not "raw" in kwargs:
|
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)
|
self.sessionhandler.data_out(self, text=text, **kwargs)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ from src.utils import utils
|
||||||
|
|
||||||
# inline functions
|
# inline functions
|
||||||
|
|
||||||
def pad(text, *args):
|
def pad(text, *args, **kwargs):
|
||||||
"Pad to width. pad(text, width=78, align='c', fillchar=' ')"
|
"Pad to width. pad(text, width=78, align='c', fillchar=' ')"
|
||||||
width = 78
|
width = 78
|
||||||
align = 'c'
|
align = 'c'
|
||||||
|
|
@ -66,7 +66,7 @@ def pad(text, *args):
|
||||||
break
|
break
|
||||||
return utils.pad(text, width=width, align=align, fillchar=fillchar)
|
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='[...]')"
|
"Crop to width. crop(text, width=78, suffix='[...]')"
|
||||||
width = 78
|
width = 78
|
||||||
suffix = "[...]"
|
suffix = "[...]"
|
||||||
|
|
@ -79,7 +79,7 @@ def crop(text, *args):
|
||||||
break
|
break
|
||||||
return utils.crop(text, width=width, suffix=suffix)
|
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)"
|
"Wrap/Fill text to width. fill(text, width=78, indent=0)"
|
||||||
width = 78
|
width = 78
|
||||||
indent = 0
|
indent = 0
|
||||||
|
|
@ -90,7 +90,7 @@ def wrap(text, *args):
|
||||||
indent = int(arg) if arg.isdigit() else indent
|
indent = int(arg) if arg.isdigit() else indent
|
||||||
return utils.wrap(text, width=width, indent=indent)
|
return utils.wrap(text, width=width, indent=indent)
|
||||||
|
|
||||||
def time(text, *args):
|
def time(text, *args, **kwargs):
|
||||||
"Inserts current time"
|
"Inserts current time"
|
||||||
import time
|
import time
|
||||||
strformat = "%h %d, %H:%M"
|
strformat = "%h %d, %H:%M"
|
||||||
|
|
@ -98,6 +98,14 @@ def time(text, *args):
|
||||||
strformat = str(args[0])
|
strformat = str(args[0])
|
||||||
return time.strftime(strformat)
|
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)
|
# load functions from module (including this one, if using default settings)
|
||||||
_INLINE_FUNCS = {}
|
_INLINE_FUNCS = {}
|
||||||
|
|
@ -123,7 +131,7 @@ _FUNCCLEAN_REGEX = re.compile("|".join([_RE_FUNCCLEAN % (key, key) for key in _I
|
||||||
|
|
||||||
# inline parser functions
|
# inline parser functions
|
||||||
|
|
||||||
def _execute_inline_function(funcname, text):
|
def _execute_inline_function(funcname, text, session):
|
||||||
"""
|
"""
|
||||||
Get the enclosed text between {funcname(...) and {/funcname
|
Get the enclosed text between {funcname(...) and {/funcname
|
||||||
and execute the inline function to replace the whole block
|
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"
|
"replace the entire block with the result of the function call"
|
||||||
args = [part.strip() for part in match.group(1).split(",")]
|
args = [part.strip() for part in match.group(1).split(",")]
|
||||||
intext = match.group(2)
|
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)
|
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.
|
Parse inline function-replacement.
|
||||||
|
|
||||||
strip - remove all supported inlinefuncs from text
|
strip - remove all supported inlinefuncs from text
|
||||||
|
session - session calling for the parsing
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if strip:
|
if strip:
|
||||||
|
|
@ -165,7 +175,7 @@ def parse_inlinefunc(text, strip=False):
|
||||||
if starttag:
|
if starttag:
|
||||||
startname = starttag.group(1)
|
startname = starttag.group(1)
|
||||||
if startname == endname:
|
if startname == endname:
|
||||||
part = _execute_inline_function(startname, part)
|
part = _execute_inline_function(startname, part, session)
|
||||||
break
|
break
|
||||||
stack.append(part)
|
stack.append(part)
|
||||||
return "".join(stack)
|
return "".join(stack)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue