forked from samrocketman/servicenow-servicecatalog-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.py
52 lines (46 loc) · 1.11 KB
/
template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from jinja2 import Template
import json
import re
def get_value_by_type(obj):
value = str(obj)
if isinstance(obj, bool):
value.lower()
if re.match(r'.*[<>].*', value):
return '<![CDATA[%s]]>' % value
else:
return value
def is_dict(obj):
isinstance(obj, dict)
template = """
{% macro format_value(value) -%}
{%- if value is mapping -%}
{{value['value']}}
{%- elif '<' in value or '>' in value -%}
<![CDATA[{{value}}]]>
{%- else -%}
{{value | escape}}
{%- endif -%}
{%- endmacro %}
<?xml version="1.0" encoding="UTF-8"?>
<unload>{%- for table in tables %}
{%- for record in tables[table] %}
<{{table}} action="INSERT_OR_UPDATE">
{%- for field, value in record | dictsort %}
{%- if format_value(value) %}
<{{field}}>{{format_value(value)}}</{{field}}>
{%- else %}
<{{field}} />
{%- endif %}
{%- endfor %}
</{{table}}>
{%- endfor %}
{%- endfor %}
</unload>
""".strip()
"""
"""
export = {}
with open('dump.json') as f:
export = json.load(f)
t = Template(template)
print t.render(tables = export).strip()