Skip to content

Instantly share code, notes, and snippets.

@weining-da
Last active September 7, 2020 22:32
Show Gist options
  • Save weining-da/a55cf9851382054132e5a99ab27326f6 to your computer and use it in GitHub Desktop.
Save weining-da/a55cf9851382054132e5a99ab27326f6 to your computer and use it in GitHub Desktop.
parse ingress message
#!/usr/bin/env python3
import re
import sys
def main(s):
s = s.replace('{', ' { ').replace('}', ' } ').replace(',', ' , ')
tags = re.findall(r'"<.+/>"', s)
tag_dict = dict()
for idx, tag in enumerate(tags):
name = f'tag_{idx}'
s = s.replace(tag, name)
tag_dict[name] = tag[1:-1]
tokens = re.findall(r'\S+', s)
ind = 0
for t in tokens:
if t == '{':
ind += 1
sys.stdout.write(f' with\n{" " * ind}')
elif t == '}':
ind -= 1
elif t == ',':
sys.stdout.write(f'\n{" " * ind}')
elif t == '=':
sys.stdout.write(f' = ')
else:
sys.stdout.write(t)
print()
if __name__ == '__main__':
buf = sys.stdin.read()
main(buf.strip().replace('\\""', '"'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment