aboutsummaryrefslogtreecommitdiffstats
path: root/tools/koblingsplan/script.py
diff options
context:
space:
mode:
authorHåkon Solbjørg <hakon@solbj.org>2023-02-25 12:24:44 +0100
committerGitHub <noreply@github.com>2023-02-25 12:24:44 +0100
commitd6f5a7bd1075aef42d7dc983a5a51fd1e31f69e6 (patch)
treee3e62b8cd64203100852875988c178ff49aa9a7d /tools/koblingsplan/script.py
parent709c78569b26677624e60588fa1166dc659ac93c (diff)
parent2ede2da02763747dd33a781863217b9371737652 (diff)
Merge pull request #105 from gathering/koblingsplan
Koblingsplan To NetBox
Diffstat (limited to 'tools/koblingsplan/script.py')
-rw-r--r--tools/koblingsplan/script.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/tools/koblingsplan/script.py b/tools/koblingsplan/script.py
new file mode 100644
index 0000000..e296eae
--- /dev/null
+++ b/tools/koblingsplan/script.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python3
+
+'''
+ Parse the exported table of tg23-koblingsplan (copypaste via libreoffice -> save as csv (standard values everywhere)) into a sensible yaml file
+ Will handle merged cells (e.g. keep previous iterations value if current iteration is empty)
+'''
+
+import csv
+import yaml
+
+# Holds all data. List of objects, each object represents a row in the table
+dataset = []
+
+with open('tg23-koblingsplan.csv', newline='') as csvfile:
+ csv_data = csv.reader(csvfile, delimiter=',', quotechar='"')
+
+ # for loop counter
+ i = 0
+
+ # Holds the data from the current iteration
+ current_iteration = {}
+
+ for row in csv_data:
+ i += 1
+ # skip first 2 lines, they only contain table headers
+ if i <= 2:
+ continue
+
+ # To be able to access previous iteration fields, so we can handle merged cells
+ prev_iteration = current_iteration.copy()
+
+ # The not-so-delicate blob of code for assigning data to object keys
+ current_iteration = {}
+ a = {}
+ b = {}
+
+ a['type'] = row[0] if len(row[0].strip()) > 0 else prev_iteration['a']['type']
+ a['model'] = row[1] if len(row[1].strip()) > 0 else prev_iteration['a']['model']
+ a['node'] = row[2] if len(row[2].strip()) > 0 else prev_iteration['a']['node']
+ a['interface'] = row[3].strip() if len(row[3].strip()) > 0 else prev_iteration['a']['interface']
+ a['ae'] = row[4] if len(row[4].strip()) > 0 else prev_iteration['a']['ae']
+ b['type'] = row[5] if len(row[5].strip()) > 0 else prev_iteration['b']['type']
+ b['model'] = row[6] if len(row[6].strip()) > 0 else prev_iteration['b']['model']
+ b['node'] = row[7] if len(row[7].strip()) > 0 else prev_iteration['b']['node']
+ b['interface'] = row[8].strip() if len(row[8].strip()) > 0 else prev_iteration['b']['interface']
+ b['ae'] = row[9] if len(row[9].strip()) > 0 else prev_iteration['b']['ae']
+
+ current_iteration['a'] = a
+ current_iteration['b'] = b
+ current_iteration['cable_type'] = row[10] if len(row[10].strip()) > 0 else prev_iteration['cable_type']
+
+ # strip trailing data from interface sections and put it in a description field
+ if (if_data := current_iteration['a']['interface'].split(" ")) and len(if_data) > 1:
+ current_iteration['a']['interface_description'] = " ".join(if_data[1:])
+ current_iteration['a']['interface'] = if_data[0]
+ if (if_data := current_iteration['b']['interface'].split(" ")) and len(if_data) > 1:
+ current_iteration['b']['interface_description'] = " ".join(if_data[1:])
+ current_iteration['b']['interface'] = if_data[0]
+
+ # strip trailing data from node sections and put it in a description field
+ if (if_data := current_iteration['a']['node'].split(" ")) and len(if_data) > 1:
+ current_iteration['a']['node_description'] = " ".join(if_data[1:])
+ current_iteration['a']['node'] = if_data[0]
+ if (if_data := current_iteration['b']['node'].split(" ")) and len(if_data) > 1:
+ current_iteration['b']['node_description'] = " ".join(if_data[1:])
+ current_iteration['b']['node'] = if_data[0]
+
+ dataset.append(current_iteration)
+
+with open('tg23-koblingsplan.yml', 'w') as f:
+ f.write(yaml.dump(dataset, default_flow_style=False, sort_keys=False, allow_unicode=True))