From b8c225d083f24a3e078b6922502f4d23aa71106e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Thu, 23 Feb 2023 18:46:23 +0100 Subject: import koblingsplan script from confluence to git --- tools/koblingsplan/script.py | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tools/koblingsplan/script.py (limited to 'tools/koblingsplan/script.py') diff --git a/tools/koblingsplan/script.py b/tools/koblingsplan/script.py new file mode 100644 index 0000000..3c4fa4b --- /dev/null +++ b/tools/koblingsplan/script.py @@ -0,0 +1,47 @@ +#!/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 + +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 = {} + + # Holds all data. List of objects, each object represents a row in the table + dataset = [] + + 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 = {} + current_iteration['a_type'] = row[0] if len(row[0].strip()) > 0 else prev_iteration['a_type'] + current_iteration['a_model'] = row[1] if len(row[1].strip()) > 0 else prev_iteration['a_model'] + current_iteration['a_node'] = row[2] if len(row[2].strip()) > 0 else prev_iteration['a_node'] + current_iteration['a_interface'] = row[3] if len(row[3].strip()) > 0 else prev_iteration['a_interface'] + current_iteration['a_ae'] = row[4] if len(row[4].strip()) > 0 else prev_iteration['a_ae'] + current_iteration['b_type'] = row[5] if len(row[5].strip()) > 0 else prev_iteration['b_type'] + current_iteration['b_model'] = row[6] if len(row[6].strip()) > 0 else prev_iteration['b_model'] + current_iteration['b_node'] = row[7] if len(row[7].strip()) > 0 else prev_iteration['b_node'] + current_iteration['b_interface'] = row[8] if len(row[8].strip()) > 0 else prev_iteration['b_interface'] + current_iteration['b_ae'] = row[9] if len(row[9].strip()) > 0 else prev_iteration['b_ae'] + current_iteration['cable_type'] = row[10] if len(row[10].strip()) > 0 else prev_iteration['cable_type'] + dataset.append(current_iteration) + + print(yaml.dump(dataset, default_flow_style=False, sort_keys=False)) -- cgit v1.2.3 From ee9d93f1d31d259c0e2502894051c03eea3f553a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Thu, 23 Feb 2023 19:04:17 +0100 Subject: fix(koblingsplan): Strip whitespace from interface names --- tools/koblingsplan/script.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tools/koblingsplan/script.py') diff --git a/tools/koblingsplan/script.py b/tools/koblingsplan/script.py index 3c4fa4b..7fa12b6 100644 --- a/tools/koblingsplan/script.py +++ b/tools/koblingsplan/script.py @@ -34,12 +34,12 @@ with open('tg23-koblingsplan.csv', newline='') as csvfile: current_iteration['a_type'] = row[0] if len(row[0].strip()) > 0 else prev_iteration['a_type'] current_iteration['a_model'] = row[1] if len(row[1].strip()) > 0 else prev_iteration['a_model'] current_iteration['a_node'] = row[2] if len(row[2].strip()) > 0 else prev_iteration['a_node'] - current_iteration['a_interface'] = row[3] if len(row[3].strip()) > 0 else prev_iteration['a_interface'] + current_iteration['a_interface'] = row[3].strip() if len(row[3].strip()) > 0 else prev_iteration['a_interface'] current_iteration['a_ae'] = row[4] if len(row[4].strip()) > 0 else prev_iteration['a_ae'] current_iteration['b_type'] = row[5] if len(row[5].strip()) > 0 else prev_iteration['b_type'] current_iteration['b_model'] = row[6] if len(row[6].strip()) > 0 else prev_iteration['b_model'] current_iteration['b_node'] = row[7] if len(row[7].strip()) > 0 else prev_iteration['b_node'] - current_iteration['b_interface'] = row[8] if len(row[8].strip()) > 0 else prev_iteration['b_interface'] + current_iteration['b_interface'] = row[8].strip() if len(row[8].strip()) > 0 else prev_iteration['b_interface'] current_iteration['b_ae'] = row[9] if len(row[9].strip()) > 0 else prev_iteration['b_ae'] current_iteration['cable_type'] = row[10] if len(row[10].strip()) > 0 else prev_iteration['cable_type'] dataset.append(current_iteration) -- cgit v1.2.3 From 01ce44904bdec7ec1c1d3cadcfd1e26c5d083d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Thu, 23 Feb 2023 19:04:31 +0100 Subject: fix(koblingsplan): Move extra info about interface from if name --- tools/koblingsplan/script.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'tools/koblingsplan/script.py') diff --git a/tools/koblingsplan/script.py b/tools/koblingsplan/script.py index 7fa12b6..20396bc 100644 --- a/tools/koblingsplan/script.py +++ b/tools/koblingsplan/script.py @@ -42,6 +42,16 @@ with open('tg23-koblingsplan.csv', newline='') as csvfile: current_iteration['b_interface'] = row[8].strip() if len(row[8].strip()) > 0 else prev_iteration['b_interface'] current_iteration['b_ae'] = row[9] if len(row[9].strip()) > 0 else prev_iteration['b_ae'] 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 + extra_info = "" + 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] + dataset.append(current_iteration) print(yaml.dump(dataset, default_flow_style=False, sort_keys=False)) -- cgit v1.2.3 From ec8386d110d0632d29ee6679c0c8c4a2a7742797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Thu, 23 Feb 2023 19:06:47 +0100 Subject: fix(koblingsplan): As a Norwegian I support unicode characters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also: systemstøtte, not systemst\xF8tte --- tools/koblingsplan/script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools/koblingsplan/script.py') diff --git a/tools/koblingsplan/script.py b/tools/koblingsplan/script.py index 20396bc..c27eda9 100644 --- a/tools/koblingsplan/script.py +++ b/tools/koblingsplan/script.py @@ -54,4 +54,4 @@ with open('tg23-koblingsplan.csv', newline='') as csvfile: dataset.append(current_iteration) - print(yaml.dump(dataset, default_flow_style=False, sort_keys=False)) + print(yaml.dump(dataset, default_flow_style=False, sort_keys=False, allow_unicode=True)) -- cgit v1.2.3 From af7e5a3b3be6e9ca82eef4b4e340b1c7e6ef6115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Thu, 23 Feb 2023 19:08:52 +0100 Subject: feat(koblingsplan): Write YAML to file --- tools/koblingsplan/script.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'tools/koblingsplan/script.py') diff --git a/tools/koblingsplan/script.py b/tools/koblingsplan/script.py index c27eda9..8fdc518 100644 --- a/tools/koblingsplan/script.py +++ b/tools/koblingsplan/script.py @@ -8,6 +8,9 @@ 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='"') @@ -17,9 +20,6 @@ with open('tg23-koblingsplan.csv', newline='') as csvfile: # Holds the data from the current iteration current_iteration = {} - # Holds all data. List of objects, each object represents a row in the table - dataset = [] - for row in csv_data: i += 1 # skip first 2 lines, they only contain table headers @@ -54,4 +54,5 @@ with open('tg23-koblingsplan.csv', newline='') as csvfile: dataset.append(current_iteration) - print(yaml.dump(dataset, default_flow_style=False, sort_keys=False, allow_unicode=True)) +with open('tg23-koblingsplan.yml', 'w') as f: + f.write(yaml.dump(dataset, default_flow_style=False, sort_keys=False, allow_unicode=True)) -- cgit v1.2.3 From 5c320ca37e9c8e682838896bea42879500754bbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Thu, 23 Feb 2023 19:13:42 +0100 Subject: chore(koblingsplan): Map a and b connection to objects --- tools/koblingsplan/script.py | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) (limited to 'tools/koblingsplan/script.py') diff --git a/tools/koblingsplan/script.py b/tools/koblingsplan/script.py index 8fdc518..755d9bf 100644 --- a/tools/koblingsplan/script.py +++ b/tools/koblingsplan/script.py @@ -31,26 +31,32 @@ with open('tg23-koblingsplan.csv', newline='') as csvfile: # The not-so-delicate blob of code for assigning data to object keys current_iteration = {} - current_iteration['a_type'] = row[0] if len(row[0].strip()) > 0 else prev_iteration['a_type'] - current_iteration['a_model'] = row[1] if len(row[1].strip()) > 0 else prev_iteration['a_model'] - current_iteration['a_node'] = row[2] if len(row[2].strip()) > 0 else prev_iteration['a_node'] - current_iteration['a_interface'] = row[3].strip() if len(row[3].strip()) > 0 else prev_iteration['a_interface'] - current_iteration['a_ae'] = row[4] if len(row[4].strip()) > 0 else prev_iteration['a_ae'] - current_iteration['b_type'] = row[5] if len(row[5].strip()) > 0 else prev_iteration['b_type'] - current_iteration['b_model'] = row[6] if len(row[6].strip()) > 0 else prev_iteration['b_model'] - current_iteration['b_node'] = row[7] if len(row[7].strip()) > 0 else prev_iteration['b_node'] - current_iteration['b_interface'] = row[8].strip() if len(row[8].strip()) > 0 else prev_iteration['b_interface'] - current_iteration['b_ae'] = row[9] if len(row[9].strip()) > 0 else prev_iteration['b_ae'] + 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 extra_info = "" - 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] + 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] dataset.append(current_iteration) -- cgit v1.2.3 From 2ede2da02763747dd33a781863217b9371737652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Thu, 23 Feb 2023 20:26:23 +0100 Subject: feat(netbox): Script to import koblingsplan to netbox --- tools/koblingsplan/script.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'tools/koblingsplan/script.py') diff --git a/tools/koblingsplan/script.py b/tools/koblingsplan/script.py index 755d9bf..e296eae 100644 --- a/tools/koblingsplan/script.py +++ b/tools/koblingsplan/script.py @@ -50,7 +50,6 @@ with open('tg23-koblingsplan.csv', newline='') as csvfile: 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 - extra_info = "" 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] @@ -58,6 +57,14 @@ with open('tg23-koblingsplan.csv', newline='') as csvfile: 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: -- cgit v1.2.3