2016-04-07 15:03:22 +02:00
|
|
|
#!/usr/bin/env python
|
2016-03-10 17:15:34 +01:00
|
|
|
from __future__ import print_function
|
2017-02-05 21:24:28 +01:00
|
|
|
from __future__ import absolute_import
|
2013-12-04 18:57:55 +01:00
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import json
|
|
|
|
|
2017-02-05 21:24:28 +01:00
|
|
|
# check for the venv
|
|
|
|
from lib import sanity_check
|
|
|
|
sanity_check.check_venv(__file__)
|
|
|
|
|
2016-07-24 18:42:32 +02:00
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
2013-12-04 18:57:55 +01:00
|
|
|
def debug(obj):
|
2016-10-16 07:27:55 +02:00
|
|
|
# type: (Any) -> None
|
2013-12-04 18:57:55 +01:00
|
|
|
print(json.dumps(obj, indent=4))
|
|
|
|
|
|
|
|
def parse_file(fn):
|
2016-07-24 18:42:32 +02:00
|
|
|
# type: (str) -> Dict[str, Any]
|
2013-12-04 18:57:55 +01:00
|
|
|
text = open(fn).read()
|
|
|
|
tags = re.findall('{+\s*(.*?)\s*}+', text)
|
2016-07-24 18:42:32 +02:00
|
|
|
root = {} # type: Dict[str, Any]
|
2013-12-04 18:57:55 +01:00
|
|
|
context = root
|
2016-07-24 18:42:32 +02:00
|
|
|
stack = [] # type: List[Dict[str, Any]]
|
2013-12-04 18:57:55 +01:00
|
|
|
|
|
|
|
def set_var(var, val):
|
2016-07-24 18:42:32 +02:00
|
|
|
# type: (str, Any) -> None
|
2013-12-04 18:57:55 +01:00
|
|
|
num_levels_up = len(re.findall('\.\.', var))
|
|
|
|
if num_levels_up:
|
|
|
|
var = var.split('/')[-1]
|
|
|
|
stack[-1 * num_levels_up][var] = val
|
|
|
|
else:
|
|
|
|
context[var] = val
|
|
|
|
|
|
|
|
for tag in tags:
|
|
|
|
if tag.startswith('! '):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tag == 'else':
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tag[0] in '#^' and ' ' not in tag:
|
|
|
|
var = tag[1:]
|
|
|
|
set_var(var, True)
|
|
|
|
stack.append(context)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tag.startswith('#if'):
|
|
|
|
vars = tag.split()[1:]
|
|
|
|
for var in vars:
|
|
|
|
set_var(var, True)
|
|
|
|
stack.append(context)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tag.startswith('/if'):
|
|
|
|
context = stack.pop()
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tag.startswith('#with '):
|
|
|
|
var = tag.split()[1]
|
2016-07-24 18:42:32 +02:00
|
|
|
new_context = {} # type: Dict[str, Any]
|
2013-12-04 18:57:55 +01:00
|
|
|
context[var] = new_context
|
|
|
|
stack.append(context)
|
|
|
|
context = new_context
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tag.startswith('/with'):
|
|
|
|
context = stack.pop()
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tag.startswith('#unless '):
|
|
|
|
var = tag.split()[1]
|
|
|
|
set_var(var, True)
|
|
|
|
stack.append(context)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tag.startswith('/unless'):
|
|
|
|
context = stack.pop()
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tag.startswith('#each '):
|
|
|
|
var = tag.split()[1]
|
|
|
|
new_context = {}
|
|
|
|
context[var] = [new_context]
|
|
|
|
stack.append(context)
|
|
|
|
context = new_context
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tag.startswith('/each'):
|
|
|
|
context = stack.pop()
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tag.startswith('/'):
|
|
|
|
context = stack.pop()
|
|
|
|
continue
|
|
|
|
|
|
|
|
set_var(tag, '')
|
|
|
|
|
|
|
|
def clean_this(obj):
|
2016-07-24 18:42:32 +02:00
|
|
|
# type: (Any) -> Any
|
2013-12-04 18:57:55 +01:00
|
|
|
if isinstance(obj, list):
|
|
|
|
return [clean_this(item) for item in obj]
|
|
|
|
if isinstance(obj, dict):
|
2016-04-05 08:32:33 +02:00
|
|
|
if len(obj) == 1 and 'this' in obj:
|
2013-12-04 18:57:55 +01:00
|
|
|
return clean_this(obj['this'])
|
|
|
|
return {k: clean_this(v) for k, v in obj.items()}
|
|
|
|
return obj
|
|
|
|
root = clean_this(root)
|
|
|
|
return root
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
for fn in sys.argv[1:]:
|
2016-03-10 17:15:34 +01:00
|
|
|
print('===', fn)
|
2013-12-04 18:57:55 +01:00
|
|
|
root = parse_file(fn)
|
|
|
|
debug(root)
|
2016-03-10 17:15:34 +01:00
|
|
|
print()
|