Many more tests, debugging of protfuncs/inlinefuncs

This commit is contained in:
Griatch 2018-06-17 23:42:53 +02:00
parent 646b73e872
commit 721cdb5ae0
5 changed files with 267 additions and 92 deletions

View file

@ -161,13 +161,15 @@ def clr(*args, **kwargs):
def null(*args, **kwargs):
return args[0] if args else ''
_INLINE_FUNCS = {}
# we specify a default nomatch function to use if no matching func was
# found. This will be overloaded by any nomatch function defined in
# the imported modules.
_INLINE_FUNCS = {"nomatch": lambda *args, **kwargs: "<UKNOWN>",
"stackfull": lambda *args, **kwargs: "\n (not parsed: "
"inlinefunc stack size exceeded.)"}
_DEFAULT_FUNCS = {"nomatch": lambda *args, **kwargs: "<UNKNOWN>",
"stackfull": lambda *args, **kwargs: "\n (not parsed: "}
_INLINE_FUNCS.update(_DEFAULT_FUNCS)
# load custom inline func modules.
for module in utils.make_iter(settings.INLINEFUNC_MODULES):
@ -285,6 +287,11 @@ def parse_inlinefunc(string, strip=False, available_funcs=None, **kwargs):
if not available_funcs:
available_funcs = _INLINE_FUNCS
usecache = True
else:
# make sure the default keys are available, but also allow overriding
tmp = _DEFAULT_FUNCS.copy()
tmp.update(available_funcs)
available_funcs = tmp
if usecache and string in _PARSING_CACHE:
# stack is already cached
@ -299,9 +306,14 @@ def parse_inlinefunc(string, strip=False, available_funcs=None, **kwargs):
# process string on stack
ncallable = 0
nlparens = 0
# print("STRING: {} =>".format(string))
for match in _RE_TOKEN.finditer(string):
gdict = match.groupdict()
# print("match: {}".format({key: val for key, val in gdict.items() if val}))
# print(" MATCH: {}".format({key: val for key, val in gdict.items() if val}))
if gdict["singlequote"]:
stack.append(gdict["singlequote"])
elif gdict["doublequote"]:
@ -386,10 +398,10 @@ def parse_inlinefunc(string, strip=False, available_funcs=None, **kwargs):
kwargs["inlinefunc_stack_depth"] = depth
retval = "" if strip else func(*args, **kwargs)
return utils.to_str(retval, force_string=True)
# print("STACK:\n{}".format(stack))
retval = "".join(_run_stack(item) for item in stack)
# print("STACK: \n{} => {}\n".format(stack, retval))
# execute the stack
return "".join(_run_stack(item) for item in stack)
return retval
#
# Nick templating

View file

@ -43,8 +43,6 @@ _GA = object.__getattribute__
_SA = object.__setattr__
_DA = object.__delattr__
_DEFAULT_WIDTH = settings.CLIENT_DEFAULT_WIDTH
def is_iter(iterable):
"""
@ -80,7 +78,7 @@ def make_iter(obj):
return not hasattr(obj, '__iter__') and [obj] or obj
def wrap(text, width=_DEFAULT_WIDTH, indent=0):
def wrap(text, width=None, indent=0):
"""
Safely wrap text to a certain number of characters.
@ -93,6 +91,7 @@ def wrap(text, width=_DEFAULT_WIDTH, indent=0):
text (str): Properly wrapped text.
"""
width = width if width else settings.CLIENT_DEFAULT_WIDTH
if not text:
return ""
text = to_unicode(text)
@ -104,7 +103,7 @@ def wrap(text, width=_DEFAULT_WIDTH, indent=0):
fill = wrap
def pad(text, width=_DEFAULT_WIDTH, align="c", fillchar=" "):
def pad(text, width=None, align="c", fillchar=" "):
"""
Pads to a given width.
@ -119,6 +118,7 @@ def pad(text, width=_DEFAULT_WIDTH, align="c", fillchar=" "):
text (str): The padded text.
"""
width = width if width else settings.CLIENT_DEFAULT_WIDTH
align = align if align in ('c', 'l', 'r') else 'c'
fillchar = fillchar[0] if fillchar else " "
if align == 'l':
@ -129,7 +129,7 @@ def pad(text, width=_DEFAULT_WIDTH, align="c", fillchar=" "):
return text.center(width, fillchar)
def crop(text, width=_DEFAULT_WIDTH, suffix="[...]"):
def crop(text, width=None, suffix="[...]"):
"""
Crop text to a certain width, throwing away text from too-long
lines.
@ -147,7 +147,7 @@ def crop(text, width=_DEFAULT_WIDTH, suffix="[...]"):
text (str): The cropped text.
"""
width = width if width else settings.CLIENT_DEFAULT_WIDTH
utext = to_unicode(text)
ltext = len(utext)
if ltext <= width:
@ -179,7 +179,7 @@ def dedent(text):
return textwrap.dedent(text)
def justify(text, width=_DEFAULT_WIDTH, align="f", indent=0):
def justify(text, width=None, align="f", indent=0):
"""
Fully justify a text so that it fits inside `width`. When using
full justification (default) this will be done by padding between
@ -198,6 +198,7 @@ def justify(text, width=_DEFAULT_WIDTH, align="f", indent=0):
justified (str): The justified and indented block of text.
"""
width = width if width else settings.CLIENT_DEFAULT_WIDTH
def _process_line(line):
"""