Fixed more pattern matching things. Will implement to test for liquid formatting
This commit is contained in:
@@ -1,17 +1,23 @@
|
|||||||
import re
|
import re
|
||||||
|
import json
|
||||||
|
|
||||||
|
input_data = """{
|
||||||
|
"test4": $test,
|
||||||
|
"test5": ,
|
||||||
|
"test6": "what"
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
input_data = """{
|
input_data = """{
|
||||||
"test0": {{ '' | default: [] }},
|
"test0": {{ '' | default: [] }},
|
||||||
"test": {{ | default: [] }},
|
"test": {{ | default: [] }},
|
||||||
"test2": {{ $test.asd | default: [] }},
|
"test2": {{ $test.asd | default: [] }},
|
||||||
"test3": {{ {"key": "val} | default: [] }},
|
"test3": {{ {"key": "val} | default: [] }},
|
||||||
}"""
|
"test4": $test,
|
||||||
|
"test5": ,
|
||||||
#
|
"test6": "what"
|
||||||
# "test4": $test,
|
}
|
||||||
# "test5": ,
|
"""
|
||||||
# "test6": "what"
|
|
||||||
#}"""
|
|
||||||
|
|
||||||
|
|
||||||
liquiddata = "{{ $test.asd | some other stuff {{ $test.xyz | more stuff"
|
liquiddata = "{{ $test.asd | some other stuff {{ $test.xyz | more stuff"
|
||||||
@@ -21,18 +27,32 @@ replaced_data = re.sub(pattern, "{{ '' |", liquiddata)
|
|||||||
print(replaced_data)
|
print(replaced_data)
|
||||||
|
|
||||||
|
|
||||||
def patternfix_liquid(liquiddata):
|
def patternfix_string(liquiddata, patterns, regex_patterns, inputtype="liquid"):
|
||||||
if "{{" not in liquiddata or "}}" not in liquiddata:
|
if not inputtype or inputtype == "liquid":
|
||||||
|
if "{{" not in liquiddata or "}}" not in liquiddata:
|
||||||
|
return liquiddata
|
||||||
|
elif inputtype == "json":
|
||||||
|
liquiddata = liquiddata.strip()
|
||||||
|
|
||||||
|
# Validating if it looks like json or not
|
||||||
|
if liquiddata[0] == "{" and liquiddata[len(liquiddata)-1] == "}":
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
if liquiddata[0] == "[" and liquiddata[len(liquiddata)-1] == "]":
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
return liquiddata
|
||||||
|
|
||||||
|
# If it's already json, don't touch it
|
||||||
|
try:
|
||||||
|
json.loads(liquiddata)
|
||||||
|
return liquiddata
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
print("No replace handler for %s" % inputtype)
|
||||||
return liquiddata
|
return liquiddata
|
||||||
|
|
||||||
patterns = {
|
|
||||||
"{{|": "{{ '' |",
|
|
||||||
}
|
|
||||||
|
|
||||||
regex_patterns = {
|
|
||||||
r'\{\{\s*\$[^|}]+\s*\|': "{{ '' |",
|
|
||||||
}
|
|
||||||
|
|
||||||
skipkeys = [" "]
|
skipkeys = [" "]
|
||||||
newoutput = liquiddata[:]
|
newoutput = liquiddata[:]
|
||||||
for pattern in patterns:
|
for pattern in patterns:
|
||||||
@@ -41,6 +61,15 @@ def patternfix_liquid(liquiddata):
|
|||||||
record = False
|
record = False
|
||||||
index = -1
|
index = -1
|
||||||
for key in liquiddata:
|
for key in liquiddata:
|
||||||
|
|
||||||
|
# Return instant if possible
|
||||||
|
if inputtype == "json":
|
||||||
|
try:
|
||||||
|
json.loads(newoutput)
|
||||||
|
return newoutput
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
index += 1
|
index += 1
|
||||||
if not key:
|
if not key:
|
||||||
if record:
|
if record:
|
||||||
@@ -76,11 +105,19 @@ def patternfix_liquid(liquiddata):
|
|||||||
evaluated_value = "".join(evaluated_value.split(skipkey))
|
evaluated_value = "".join(evaluated_value.split(skipkey))
|
||||||
|
|
||||||
if evaluated_value == pattern:
|
if evaluated_value == pattern:
|
||||||
print("Found matching: %s (%s)" % (parsedvalue, keylocations))
|
#print("Found matching: %s (%s)" % (parsedvalue, keylocations))
|
||||||
print("Should replace with: %s" % patterns[pattern])
|
#print("Should replace with: %s" % patterns[pattern])
|
||||||
|
|
||||||
newoutput = newoutput.replace(parsedvalue, patterns[pattern], -1)
|
newoutput = newoutput.replace(parsedvalue, patterns[pattern], -1)
|
||||||
break
|
|
||||||
|
# Return instant if possible
|
||||||
|
if inputtype == "json":
|
||||||
|
try:
|
||||||
|
json.loads(newoutput)
|
||||||
|
return newoutput
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
for pattern in regex_patterns:
|
for pattern in regex_patterns:
|
||||||
newlines = []
|
newlines = []
|
||||||
@@ -90,13 +127,52 @@ def patternfix_liquid(liquiddata):
|
|||||||
|
|
||||||
newoutput = "\n".join(newlines)
|
newoutput = "\n".join(newlines)
|
||||||
|
|
||||||
|
# Return instant if possible
|
||||||
|
if inputtype == "json":
|
||||||
|
try:
|
||||||
|
json.loads(newoutput)
|
||||||
|
return newoutput
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
return newoutput
|
return newoutput
|
||||||
|
|
||||||
print("Start:\n%s" % input_data)
|
print("Start:\n%s" % input_data)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
newinput = patternfix_liquid(input_data)
|
newinput = patternfix_string(input_data,
|
||||||
|
{
|
||||||
|
"{{|": '{{ "" |',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
#r'\{\{\s*|': "{{ '' |",
|
||||||
|
r'\{\{\s*\$[^|}]+\s*\|': '{{ "" |',
|
||||||
|
}
|
||||||
|
,
|
||||||
|
inputtype="liquid"
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("[ERROR} Failed liquid parsing fix: %s" % e)
|
print("[ERROR} Failed liquid parsing fix: %s" % e)
|
||||||
newinput = input_data
|
newinput = input_data
|
||||||
|
|
||||||
|
try:
|
||||||
|
newinput = patternfix_string(newinput,
|
||||||
|
{
|
||||||
|
},
|
||||||
|
{
|
||||||
|
r'\"\s*\:\s*,': '\": "",',
|
||||||
|
r'\"\s*\:\s*\$[^,]+\w*\,': '\": "",',
|
||||||
|
}
|
||||||
|
,
|
||||||
|
inputtype="json"
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
json.loads(newinput)
|
||||||
|
print("It's json! Override.")
|
||||||
|
except Exception as e:
|
||||||
|
print("Bad json. DONT use the value at all: %s" % e)
|
||||||
|
except Exception as e:
|
||||||
|
print("[ERROR} Failed json parsing fix: %s" % e)
|
||||||
|
|
||||||
print("\nEnd:\n%s" % newinput)
|
print("\nEnd:\n%s" % newinput)
|
||||||
|
|||||||
Reference in New Issue
Block a user