forked from networktocode/netutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
development_scripts.py
145 lines (134 loc) · 4.72 KB
/
development_scripts.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Developer script to generate markdown tables."""
from jinja2 import Environment, BaseLoader, select_autoescape
from netutils.utils import _JINJA2_FUNCTION_MAPPINGS
from netutils import lib_mapper
from netutils.config.compliance import parser_map
LIB_MAPPER_TEMPLATE = """| {{ header_src }} | | {{ header_dst }} |
| ---------- | -- | ------ |
{%- for key, val in _dict|dictsort %}
| {{ key }} | → | {{ val }} |
{%- endfor %}
"""
PARSER_MAPPER_TEMPLATE = """| OS Name | Parser Class |
| ---------- | ------ |
{%- for key, val in _dict|dictsort %}
| {{ key }} | netutils.config.parser.{{ val.__name__ }} |
{%- endfor %}
"""
JINJA_MAPPER_TEMPLATE = """| Filter name | Function |
| ---------- | ------ |
{%- for key, val in _dict|dictsort(false, 'value') %}
| {{ key }} | netutils.{{ val }} |
{%- endfor %}
"""
TEMPLATE = {
"LIB_MAPPER_TEMPLATE": LIB_MAPPER_TEMPLATE,
"PARSER_MAPPER_TEMPLATE": PARSER_MAPPER_TEMPLATE,
"JINJA_MAPPER_TEMPLATE": JINJA_MAPPER_TEMPLATE,
}
MAPPER = {
"LIB_MAPPER_TEMPLATE": {
"ansible": {
"header_src": "NORMALIZED",
"header_dst": "ANSIBLE",
"_dict": lib_mapper.ANSIBLE_LIB_MAPPER,
"_file": "docs/user/lib_mapper/ansible.md",
},
"ansible_reverse": {
"header_src": "ANSIBLE",
"header_dst": "NORMALIZED",
"_dict": lib_mapper.ANSIBLE_LIB_MAPPER_REVERSE,
"_file": "docs/user/lib_mapper/ansible_reverse.md",
},
"napalm": {
"header_src": "NORMALIZED",
"header_dst": "NAPALM",
"_dict": lib_mapper.NAPALM_LIB_MAPPER,
"_file": "docs/user/lib_mapper/napalm.md",
},
"napalm_reverse": {
"header_src": "NAPALM",
"header_dst": "NORMALIZED",
"_dict": lib_mapper.NAPALM_LIB_MAPPER_REVERSE,
"_file": "docs/user/lib_mapper/napalm_reverse.md",
},
"ntctemplates": {
"header_src": "NORMALIZED",
"header_dst": "NTCTEMPLATES",
"_dict": lib_mapper.NTCTEMPLATES_LIB_MAPPER,
"_file": "docs/user/lib_mapper/ntctemplates.md",
},
"ntctemplates_reverse": {
"header_src": "NTCTEMPLATES",
"header_dst": "NORMALIZED",
"_dict": lib_mapper.NTCTEMPLATES_LIB_MAPPER_REVERSE,
"_file": "docs/user/lib_mapper/ntctemplates_reverse.md",
},
"pyats": {
"header_src": "NORMALIZED",
"header_dst": "PYATS",
"_dict": lib_mapper.PYATS_LIB_MAPPER,
"_file": "docs/user/lib_mapper/pyats.md",
},
"pyats_reverse": {
"header_src": "PYATS",
"header_dst": "NORMALIZED",
"_dict": lib_mapper.PYATS_LIB_MAPPER_REVERSE,
"_file": "docs/user/lib_mapper/pyats_reverse.md",
},
"pyntc": {
"header_src": "NORMALIZED",
"header_dst": "PYNTC",
"_dict": lib_mapper.PYNTC_LIB_MAPPER,
"_file": "docs/user/lib_mapper/pyntc.md",
},
"pyntc_reverse": {
"header_src": "PYNTC",
"header_dst": "NORMALIZED",
"_dict": lib_mapper.PYNTC_LIB_MAPPER_REVERSE,
"_file": "docs/user/lib_mapper/pyntc_reverse.md",
},
"scrapli": {
"header_src": "NORMALIZED",
"header_dst": "SCRAPLI",
"_dict": lib_mapper.SCRAPLI_LIB_MAPPER,
"_file": "docs/user/lib_mapper/scrapli.md",
},
"scrapli_reverse": {
"header_src": "SCRAPLI",
"header_dst": "NORMALIZED",
"_dict": lib_mapper.SCRAPLI_LIB_MAPPER_REVERSE,
"_file": "docs/user/lib_mapper/scrapli_reverse.md",
},
},
"PARSER_MAPPER_TEMPLATE": {
"default": {
"_dict": parser_map,
"_file": "docs/dev/include_parser_list.md",
},
},
"JINJA_MAPPER_TEMPLATE": {
"default": {
"_dict": _JINJA2_FUNCTION_MAPPINGS,
"_file": "docs/user/include_jinja_list.md",
},
},
}
def main(test=False):
"""Generate or test generation of standard files."""
env = Environment(loader=BaseLoader, autoescape=select_autoescape())
for template_name, value in MAPPER.items():
for data in value.values():
_file = data["_file"]
output = env.from_string(TEMPLATE[template_name]).render(**data)
if test:
with open(_file, encoding="utf8") as file:
actual = file.read()
if output != actual:
return False
else:
with open(_file, "w", encoding="utf8") as file:
file.write(output)
return True
if __name__ == "__main__":
main()