aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexandru Avadanii <Alexandru.Avadanii@enea.com>2017-09-15 16:21:32 +0200
committerAlexandru Avadanii <Alexandru.Avadanii@enea.com>2017-09-16 20:37:15 +0200
commitb2613943c7e904771f420c7ffe4a549035a3e107 (patch)
treef1d330f9f8183dd5cd1f1527b6c8a089a9e897b2
parent6b2e09a86374d7d7b884f4ff04e78fd3d10391e6 (diff)
utils/generate_config.py: Add ipaddr_index filter
v5 -> v6: - IP address can be IPv4 or IPv6; - add fallback to 'str' type for py3-incompatible 'unicode'; - fix pylint complaints (silence unnecessary ones); Change-Id: Iea1049a7f5379e9bcb4b785fdd810b67f51c94ab Signed-off-by: Guillermo Herrero <guillermo.herrero@enea.com> Signed-off-by: Alexandru Avadanii <Alexandru.Avadanii@enea.com>
-rwxr-xr-xutils/generate_config.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/utils/generate_config.py b/utils/generate_config.py
index b65bf7f..353cd47 100755
--- a/utils/generate_config.py
+++ b/utils/generate_config.py
@@ -1,6 +1,7 @@
#!/usr/bin/python
"""This module does blah blah."""
import argparse
+import ipaddress
import yaml
from jinja2 import Environment, FileSystemLoader
@@ -9,8 +10,19 @@ PARSER.add_argument("--yaml", "-y", type=str, required=True)
PARSER.add_argument("--jinja2", "-j", type=str, required=True)
ARGS = PARSER.parse_args()
+# Custom filter to allow simple IP address operations returning
+# a new address from an upper or lower (negative) index
+def ipaddr_index(base_address, index):
+ """Return IP address in given network at given index"""
+ try:
+ base_address_str = unicode(base_address)
+ #pylint: disable=unused-variable
+ except NameError as ex:
+ base_address_str = str(base_address)
+ return ipaddress.ip_address(base_address_str) + int(index)
ENV = Environment(loader=FileSystemLoader('./'))
+ENV.filters['ipaddr_index'] = ipaddr_index
with open(ARGS.yaml) as _:
DICT = yaml.safe_load(_)
@@ -20,4 +32,5 @@ with open(ARGS.yaml) as _:
# Render template and print generated conf to console
TEMPLATE = ENV.get_template(ARGS.jinja2)
+#pylint: disable=superfluous-parens
print(TEMPLATE.render(conf=DICT))