Fixed more pattern matching things. Will implement to test for liquid formatting
This commit is contained in:
@@ -1,17 +1,23 @@
|
||||
import re
|
||||
import json
|
||||
|
||||
input_data = """{
|
||||
"test4": $test,
|
||||
"test5": ,
|
||||
"test6": "what"
|
||||
}
|
||||
"""
|
||||
|
||||
input_data = """{
|
||||
"test0": {{ '' | default: [] }},
|
||||
"test": {{ | default: [] }},
|
||||
"test2": {{ $test.asd | 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"
|
||||
@@ -21,17 +27,31 @@ replaced_data = re.sub(pattern, "{{ '' |", liquiddata)
|
||||
print(replaced_data)
|
||||
|
||||
|
||||
def patternfix_liquid(liquiddata):
|
||||
if "{{" not in liquiddata or "}}" not in liquiddata:
|
||||
return liquiddata
|
||||
def patternfix_string(liquiddata, patterns, regex_patterns, inputtype="liquid"):
|
||||
if not inputtype or inputtype == "liquid":
|
||||
if "{{" not in liquiddata or "}}" not in liquiddata:
|
||||
return liquiddata
|
||||
elif inputtype == "json":
|
||||
liquiddata = liquiddata.strip()
|
||||
|
||||
patterns = {
|
||||
"{{|": "{{ '' |",
|
||||
}
|
||||
# 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
|
||||
|
||||
regex_patterns = {
|
||||
r'\{\{\s*\$[^|}]+\s*\|': "{{ '' |",
|
||||
}
|
||||
# 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
|
||||
|
||||
skipkeys = [" "]
|
||||
newoutput = liquiddata[:]
|
||||
@@ -41,6 +61,15 @@ def patternfix_liquid(liquiddata):
|
||||
record = False
|
||||
index = -1
|
||||
for key in liquiddata:
|
||||
|
||||
# Return instant if possible
|
||||
if inputtype == "json":
|
||||
try:
|
||||
json.loads(newoutput)
|
||||
return newoutput
|
||||
except:
|
||||
pass
|
||||
|
||||
index += 1
|
||||
if not key:
|
||||
if record:
|
||||
@@ -76,11 +105,19 @@ def patternfix_liquid(liquiddata):
|
||||
evaluated_value = "".join(evaluated_value.split(skipkey))
|
||||
|
||||
if evaluated_value == pattern:
|
||||
print("Found matching: %s (%s)" % (parsedvalue, keylocations))
|
||||
print("Should replace with: %s" % patterns[pattern])
|
||||
#print("Found matching: %s (%s)" % (parsedvalue, keylocations))
|
||||
#print("Should replace with: %s" % patterns[pattern])
|
||||
|
||||
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:
|
||||
newlines = []
|
||||
@@ -90,13 +127,52 @@ def patternfix_liquid(liquiddata):
|
||||
|
||||
newoutput = "\n".join(newlines)
|
||||
|
||||
# Return instant if possible
|
||||
if inputtype == "json":
|
||||
try:
|
||||
json.loads(newoutput)
|
||||
return newoutput
|
||||
except:
|
||||
pass
|
||||
|
||||
return newoutput
|
||||
|
||||
print("Start:\n%s" % input_data)
|
||||
|
||||
try:
|
||||
newinput = patternfix_liquid(input_data)
|
||||
newinput = patternfix_string(input_data,
|
||||
{
|
||||
"{{|": '{{ "" |',
|
||||
},
|
||||
{
|
||||
#r'\{\{\s*|': "{{ '' |",
|
||||
r'\{\{\s*\$[^|}]+\s*\|': '{{ "" |',
|
||||
}
|
||||
,
|
||||
inputtype="liquid"
|
||||
)
|
||||
except Exception as e:
|
||||
print("[ERROR} Failed liquid parsing fix: %s" % e)
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user