-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgentool
More file actions
executable file
·93 lines (71 loc) · 2.49 KB
/
Copy pathgentool
File metadata and controls
executable file
·93 lines (71 loc) · 2.49 KB
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
#!/usr/bin/env python3
"""
KEES
The ColoClue Network Automation Toolchain
(c) 2014-2017 Job Snijders <job@instituut.net>
(c) 2017-2023 Network committee Coloclue <routers@coloclue.net>
"""
from jinja2 import Environment, FileSystemLoader
import argparse
import hiyapyco
import os
import re
# Allow regex matching in templates
def is_regex_match(value='', pattern='', ignorecase=False, multiline=False, match_type='search'):
flags = 0
if ignorecase:
flags |= re.I
if multiline:
flags |= re.M
_regex = re.compile(pattern, flags=flags)
return bool(getattr(_regex, match_type, 'search')(value))
def render(tpl_path, context):
path, filename = os.path.split(tpl_path)
env = Environment(
loader=FileSystemLoader(path or './')
)
env.tests['regex'] = is_regex_match
return env.get_template(filename).render(context)
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-y', dest='yamldata',
default="vars/main.yml",
type=str,
nargs='*',
help="""Location of the YAML data""")
parser.add_argument('-t', dest='template',
type=str,
required=True,
help="""Location of the template""")
parser.add_argument('-o', dest='output',
type=str,
default='-',
help='Output file (default: STDOUT)')
parser.add_argument('-d', dest='debug',
action='store_true',
help="""Enable debug""")
group = parser.add_mutually_exclusive_group()
group.add_argument('-4', dest='afi',
action='store_const',
const="ipv4",
help="""IPv4 mode""")
group.add_argument('-6', dest='afi',
action='store_const',
const="ipv6",
help="""IPv6 mode""")
args = parser.parse_args()
if args.debug:
print(args)
data = hiyapyco.load(*args.yamldata, method=hiyapyco.METHOD_MERGE)
data['afi'] = args.afi
if args.debug:
print(data)
if args.output == "-":
print(render(args.template, data))
else:
f = open(args.output, 'w')
f.write(render(args.template, data))
f.close()
if __name__ == '__main__':
main()