summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNeil Williams <neil.williams@linaro.org>2015-10-21 15:59:00 +0100
committerNeil Williams <neil.williams@linaro.org>2015-10-21 15:59:00 +0100
commit9e82cfb859e98b64e90eb39a78f7fd9dd65b6f25 (patch)
tree0dd12c44edfdcc72c02c155b1e87f3bb93c91d80
parent4067db7dcb393dc804e5d55a766dbf84189fe788 (diff)
output device files as jinja2 and add some basic error checks
-rw-r--r--netmap-csv.py36
1 files changed, 33 insertions, 3 deletions
diff --git a/netmap-csv.py b/netmap-csv.py
index 165cbd2..88b2587 100644
--- a/netmap-csv.py
+++ b/netmap-csv.py
@@ -22,7 +22,10 @@
#
#
+import os
+import sys
import csv
+import glob
import yaml
import argparse
@@ -55,6 +58,7 @@ def main(args):
rows = []
network = {}
devices = {}
+ errors = []
with open(args.filename, 'r') as csvfile:
content = csv.DictReader(csvfile)
for row in content:
@@ -62,6 +66,13 @@ def main(args):
for row in rows:
for key, value in row.items():
if key == 'Switch' and value and 'Switch Port' in row:
+ try:
+ int(row['Switch Port'])
+ except (ValueError, TypeError):
+ print >> sys.stderr, "Failed to parse 'Switch Port': '%s' as a number" % row['Switch Port']
+ print >> sys.stderr, "Skipping %s" % row['Device']
+ errors.append(row)
+ break
network.setdefault('switches', {})
network['switches'].setdefault(str(value), [])
data = {'port': row['Switch Port']}
@@ -73,6 +84,13 @@ def main(args):
}
network['switches'][str(value)].append(data)
elif key == 'Device' and value:
+ try:
+ int(row['Switch Port'])
+ except (ValueError, TypeError):
+ print >> sys.stderr, "Failed to parse 'Switch Port': '%s' as a number" % row['Switch Port']
+ print >> sys.stderr, "Skipping %s" % row['Device']
+ errors.append(row)
+ break
name = row['Device'].replace('*', '')
devices.setdefault(name, {})
devices[name]['extends'] = '' # needs to be Device Type
@@ -88,13 +106,25 @@ def main(args):
devices[name].setdefault('map', {})
devices[name]['map'].setdefault(iface, {})
devices[name]['map'][iface].update({
- row['Switch']: row['Switch Port']
+ row['Switch']: int(row['Switch Port'])
})
+ if devices:
+ outputs = glob.glob('./output/*.jinja2')
+ for stale in outputs:
+ os.unlink(stale)
for key, value in devices.items():
- with open('./output/%s' % key, 'w') as device_output:
- yaml.dump(value, device_output, default_flow_style=False)
+ with open('./output/%s.jinja2' % key, 'w') as device_output:
+ device_output.write("{%% extends '%s' %%}\n" % value['extends'])
+ device_output.write("{%% set interfaces = %s %%}\n" % value['interfaces'])
+ device_output.write("{%% set sysfs = %s %%}\n" % value['sysfs'])
+ device_output.write("{%% set mac_addr = %s %%}\n" % value['mac_addr'])
+ device_output.write("{%% set tags = %s %%}\n" % value['tags'])
+ device_output.write("{%% set map = %s %%}\n" % value['map'])
with open('./output/output.yaml', 'w') as output:
yaml.dump(network, output, default_flow_style=False)
+ if errors:
+ print "There were errors!"
+ return 1
return 0
if __name__ == '__main__':