Use parse_to_any, for protfuncs, handle empty list in $choice funcparser func

This commit is contained in:
Griatch 2022-10-05 22:25:46 +02:00
parent d3fe999c4b
commit 6e45ac0cb1
2 changed files with 17 additions and 11 deletions

View file

@ -132,7 +132,7 @@ def homogenize_prototype(prototype, custom_keys=None):
# attrs must be on form [(key, value, category, lockstr)]
if not is_iter(attr):
logger.log_error(
"Prototype's 'attr' field must " f"be a list of tuples: {prototype}"
f"Prototype's 'attr' field must be a list of tuples: {prototype}"
)
elif attr:
nattr = len(attr)
@ -237,8 +237,7 @@ def load_module_prototypes(*mod_or_prototypes, override=True):
for prot in prototype_list:
if not isinstance(prot, dict):
logger.log_err(
f"Prototype read from {mod}.PROTOTYPE_LIST "
f"is not a dict (skipping): {prot}"
f"Prototype read from {mod}.PROTOTYPE_LIST is not a dict (skipping): {prot}"
)
continue
elif "prototype_key" not in prot:
@ -823,7 +822,7 @@ def validate_prototype(
if strict and not (typeclass or prototype_parent):
if is_prototype_base:
_flags["errors"].append(
_("Prototype {protkey} requires `typeclass` " "or 'prototype_parent'.").format(
_("Prototype {protkey} requires `typeclass` or 'prototype_parent'.").format(
protkey=protkey
)
)
@ -866,8 +865,7 @@ def validate_prototype(
if not protparent:
_flags["errors"].append(
_(
"Prototype {protkey}'s `prototype_parent` (named '{parent}') "
"was not found."
"Prototype {protkey}'s `prototype_parent` (named '{parent}') was not found."
).format(protkey=protkey, parent=protstring)
)
@ -959,7 +957,7 @@ def protfunc_parser(
if not isinstance(value, str):
return value
result = FUNC_PARSER.parse(value, raise_errors=True, return_str=False, caller=caller, **kwargs)
result = FUNC_PARSER.parse_to_any(value, raise_errors=True, caller=caller, **kwargs)
return result

View file

@ -587,7 +587,9 @@ class FuncParser:
return fullstr
def parse_to_any(self, string, raise_errors=False, **reserved_kwargs):
def parse_to_any(
self, string, raise_errors=False, escape=False, strip=False, **reserved_kwargs
):
"""
This parses a string and if the string only contains a "$func(...)",
the return will be the return value of that function, even if it's not
@ -599,6 +601,10 @@ class FuncParser:
raise_errors (bool, optional): If unset, leave a failing (or
unrecognized) inline function as unparsed in the string. If set,
raise an ParsingError.
escape (bool, optional): If set, escape all found functions so they
are not executed by later parsing.
strip (bool, optional): If set, strip any inline funcs from string
as if they were not there.
**reserved_kwargs: If given, these are guaranteed to _always_ pass
as part of each parsed callable's **kwargs. These override
same-named default options given in `__init__` as well as any
@ -635,9 +641,9 @@ class FuncParser:
"""
return self.parse(
string,
raise_errors=False,
escape=False,
strip=False,
raise_errors=raise_errors,
escape=escape,
strip=strip,
return_str=False,
**reserved_kwargs,
)
@ -873,6 +879,8 @@ def funcparser_callable_choice(*args, **kwargs):
if not args:
return ""
args, _ = safe_convert_to_types(("py", {}), *args, **kwargs)
if not args[0]:
return ""
try:
return random.choice(args[0])
except Exception: