Skip to content

Commit

Permalink
fix: added missing parameters for safeEval
Browse files Browse the repository at this point in the history
  • Loading branch information
akelch committed Nov 10, 2021
1 parent f0b9ef7 commit bb377ee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

This file documents any relevant changes.

## [1.0.8] - 2021-11-10
- Fix: EvalFormatStrings now work properly again

## [1.0.7] - 2021-09-30
- Fix: interalEdit Forms no longer lose data

Expand Down
24 changes: 16 additions & 8 deletions flare/forms/formatString.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def formatString(format: str, data: typing.Dict, structure=None, language=None):
"""
if "$(" in format:
return formatStringHandler(format,data,structure,language=language)
return formatStringHandler(format, data, structure, language=language)
return evalStringHandler(format, data, structure, language)

return evalStringHandler(format,data,structure,language)

# formatString ---------------------------------------------------------------------------------------------------------
def formatOneEntry(key, format, data, structure=None, prefix=None, language=None, context=None, _rec=0):
Expand Down Expand Up @@ -43,7 +43,8 @@ def formatOneEntry(key, format, data, structure=None, prefix=None, language=None
res, val, structure, prefix + [key], language, _rec=_rec + 1
)

elif isinstance(val, list) and len(val) > 0 and isinstance(val[0], dict): # if bone is relationalbone with rel and dest
elif isinstance(val, list) and len(val) > 0 and isinstance(val[0],
dict): # if bone is relationalbone with rel and dest
if struct and "dest" in val[0] and "rel" in val[0]:
if "relskel" in struct and "format" in struct:
format = struct["format"]
Expand Down Expand Up @@ -83,6 +84,7 @@ def formatOneEntry(key, format, data, structure=None, prefix=None, language=None
res = res.replace("$(%s)" % (".".join(prefix + [key])), str(val))
return res


def formatStringHandler(format, data, structure=None, prefix=None, language=None, context=None, _rec=0):
"""Parses a string given by format and substitutes placeholders using values specified by data.
The syntax for the placeholders is $(%s).
Expand All @@ -104,7 +106,7 @@ def formatStringHandler(format, data, structure=None, prefix=None, language=None
:return: The traversed string with the replaced values.
:rtype: str
"""
#print("FORMAT STRING", format, data, structure, prefix)
# print("FORMAT STRING", format, data, structure, prefix)
if structure and isinstance(structure, list):
structure = {k: v for k, v in structure}

Expand All @@ -130,8 +132,10 @@ def formatStringHandler(format, data, structure=None, prefix=None, language=None

return res


# displayString ---------------------------------------------------------------------------------------------------------
def displayStringHandler(display: str, value: typing.Dict, structure: typing.Dict, language: str = "de") -> [html5.Widget]:
def displayStringHandler(display: str, value: typing.Dict, structure: typing.Dict, language: str = "de") -> [
html5.Widget]:
from flare.forms import boneSelector

# --- Helpers ---
Expand All @@ -142,6 +146,7 @@ def listToDict(l):

assert isinstance(l, dict)
return l

# ---------------

widgets = []
Expand Down Expand Up @@ -188,17 +193,20 @@ def listToDict(l):

return widgets


# evalString ---------------------------------------------------------------------------------------------------------
def evalStringHandler(format, data, structure, language):
if not html5.core.htmlExpressionEvaluator:
return format

try:
value = html5.core.htmlExpressionEvaluator.execute(format, data)
value = html5.core.htmlExpressionEvaluator.execute(format, {"value":data,
"structure":structure,
"language":language
}
)
except Exception as e:
logging.exception(e)
value = "(invalid format string)"

return value


0 comments on commit bb377ee

Please # to comment.