ensure triple quote will always work
This commit is contained in:
parent
6c103ec19e
commit
f31388eb04
2 changed files with 39 additions and 1 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import json
|
||||
import re
|
||||
from typing import Any
|
||||
from typing import Dict
|
||||
|
||||
|
|
@ -30,6 +31,10 @@ class ScriptUtils:
|
|||
elif isinstance(value, bool):
|
||||
out = f"{{%bool({value})}}"
|
||||
else:
|
||||
out = f"{{%from_json('''{json.dumps(value, ensure_ascii=False, sort_keys=True)}''')}}"
|
||||
dumped_json = json.dumps(value, ensure_ascii=False, sort_keys=True)
|
||||
# Remove triple-single-quotes from JSON to avoid parsing issues
|
||||
dumped_json = re.sub("'{3,}", "'", dumped_json)
|
||||
|
||||
out = f"{{%from_json('''{dumped_json}''')}}"
|
||||
|
||||
return out
|
||||
|
|
|
|||
33
tests/unit/utils/test_script_utils.py
Normal file
33
tests/unit/utils/test_script_utils.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import copy
|
||||
|
||||
from unit.script.conftest import single_variable_output
|
||||
|
||||
from ytdl_sub.utils.script import ScriptUtils
|
||||
|
||||
|
||||
class TestScriptUtils:
|
||||
def test_dict_to_script(self):
|
||||
json_dict = {
|
||||
"string": "value",
|
||||
"quotes": "has '' and \"\"",
|
||||
"triple-single-quote": "right here! '''''''''''''''''''''''''''''' ack '''''''",
|
||||
"int": 1,
|
||||
"bool": True,
|
||||
"list": [1, 2, 3],
|
||||
"dict": {"a": 1, "b": 2},
|
||||
"float": 3.14,
|
||||
"nested_dict": {
|
||||
"string": "value",
|
||||
"int": 1,
|
||||
"bool": True,
|
||||
"list": [1, 2, 3],
|
||||
"dict": {"a": 1, "b": 2},
|
||||
"float": 3.14,
|
||||
},
|
||||
}
|
||||
|
||||
expected_output = copy.deepcopy(json_dict)
|
||||
expected_output["triple-single-quote"] = "right here! ' ack '"
|
||||
|
||||
output = single_variable_output(ScriptUtils.to_script(json_dict))
|
||||
assert output == expected_output
|
||||
Loading…
Reference in a new issue