From ebc46097a683a81fd9c2ad5dc3618f3f2ad17440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Mon, 1 Apr 2019 22:27:21 +0200 Subject: =?UTF-8?q?Initial=20switch=20label=20scripts=20=F0=9F=9A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.rst | 4 ++++ gondul.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 README.rst create mode 100644 gondul.py create mode 100644 main.py diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..7ea2815 --- /dev/null +++ b/README.rst @@ -0,0 +1,4 @@ +TG label scripts +================ + +Scripts. diff --git a/gondul.py b/gondul.py new file mode 100644 index 0000000..65c18fa --- /dev/null +++ b/gondul.py @@ -0,0 +1,66 @@ +import base64 +import json +import os +import re +import urllib.parse +import urllib.request + +GONDUL_USERNAME = os.getenv("GONDUL_USERNAME", "") +GONDUL_PASSWORD = os.getenv("GONDUL_PASSWORD", "") +GONDUL_API = os.getenv("GONDUL_API", "https://tg18.gondul.gathering.org/api") +GONDUL_SWITCHES_ENDPOINT = os.getenv( + "GONDUL_SWITCHES_ENDPOINT", "/public/switches") + + +def _generate_credentials(username, password): + return base64.standard_b64encode( + (username + ":" + password) + .encode("utf-8")).decode("utf-8") + + +def _do_switches_request( + api=GONDUL_API, + endpoint=GONDUL_SWITCHES_ENDPOINT, + credentials=_generate_credentials(GONDUL_USERNAME, GONDUL_PASSWORD)): + switches_url = api + endpoint + + # Build request + request = urllib.request.Request(switches_url) + request.add_header("Authorization", "Basic " + credentials) + resp = urllib.request.urlopen(request, timeout=5) + assert resp.status == 200, "HTTP return was not 200 OK" + + # Read response + body = resp.read().decode("utf-8") + data = json.loads(body) + assert "switches" in data, "Missing switches object from HTTP response" + + switches = data.get("switches") + print("Found {} switches in Gondul".format(len(switches))) + return switches + + +def _match_switches(switches, match="^e(.*)"): + pattern = re.compile(match) + + included_switches = [] + for switch in switches: + include = re.search(pattern, switch) + if include: + included_switches.append(switch) + + print("'{}' matches {} switches.".format(match, len(included_switches))) + return included_switches + + +def _sort_switches(switches): + # The lambda returns two values to compare on; + # * The switch number (e77-4) - picks out the number 77 + # * The number of the switch in relation to other switches on the same row + # E.g. "e77-4" will return 4 + return sorted(switches, key=lambda x: (int(x[1:].split("-")[0]), x.split("-")[1])) + + +def fetch_gondul_switches(match="^e(.*)"): + # credentials = _generate_credentials() + return _sort_switches(_match_switches(_do_switches_request())) diff --git a/main.py b/main.py new file mode 100644 index 0000000..d397f1a --- /dev/null +++ b/main.py @@ -0,0 +1,49 @@ +from gondul import fetch_gondul_switches + +switch_label_format = "%(switch_name)s-%(switch_num)s" +switch_label_layout = """ + + + +%s +""" +switch_label_page = '
%s
' + + +def generate_label(switch_name, switch_number): + return switch_label_page % switch_label_format % { + "switch_name": switch_name, + "switch_num": switch_number, + } + + +def generate_labels(switches): + labels = list(map(lambda switch: generate_label( + switch[1:].split("-")[0], switch.split("-")[1]), switches)) + + return switch_label_layout % "".join(labels) + + +def write_html_to_file(html, outfile="switch_labels.html"): + with open(outfile, "w") as f: + f.write(html) + print("Wrote labels to '{}'.\nOpen the file in your browser and print it.".format(outfile)) + + +def make_switch_labels(): + switches = fetch_gondul_switches() + labels = generate_labels(switches) + write_html_to_file(labels) + + +if __name__ == "__main__": + make_switch_labels() -- cgit v1.2.3 From 8dac5e5615dfa2ef581cc590e4f452893ccb57f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Mon, 1 Apr 2019 22:27:39 +0200 Subject: =?UTF-8?q?chore:=20Add=20old=20scripts=20=E2=98=A0=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- old/README.txt | 29 +++++++++++++ old/cable_labels.pl | 16 +++++++ old/switch_labels.py | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 old/README.txt create mode 100644 old/cable_labels.pl create mode 100644 old/switch_labels.py diff --git a/old/README.txt b/old/README.txt new file mode 100644 index 0000000..fa02d44 --- /dev/null +++ b/old/README.txt @@ -0,0 +1,29 @@ +Disse filene brukes for � generere merking av kabler og switcher til The Gathering (Eller andre event med lignende behov) + +############## +switch_lables.py: +############## +Brukes til � generere lapper som henges opp p� switcher/switchstativer for enkel identifisering. + + +Howto: +Endre configen i filen (Antall rader, antall switcher, filnavn + eventuell config for Creativia), og kj�r filen med python. + +Den lager en HTML fil med valgt navn, som s� kan printes i en vanlig printer. + + +############## +cable_lables.pl +############## +Brukes til � generere teksten til lappene som settes i begge ender av alle kablene i hallen. + +CSV-filen mates inn i dymo programvaren og formatteres der. Husk at alle lapper m� skrives ut i to eksemplarer. + +Howto: +Kj�r filen med perl, sett variablene og pipe ut til csv med passende navn. + +Variablene filen spiser er f�lgende: Antall rader, antall switcher per rad, antall kabler per switch. + +Eksempel: + +perl cable_lables.pl 82 4 4 > Lapper.csv diff --git a/old/cable_labels.pl b/old/cable_labels.pl new file mode 100644 index 0000000..b461d03 --- /dev/null +++ b/old/cable_labels.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +my ($rows, $switches, $cables) = @ARGV; + +for my $row (1 .. $rows) { + next if (!($row & 1)); + + for my $switch (1 .. $switches) { + for my $cable (1 .. $cables) { + print join('-', ($row, $switch, $cable)) . ';' . "\n"; + } + } +} diff --git a/old/switch_labels.py b/old/switch_labels.py new file mode 100644 index 0000000..867c5ab --- /dev/null +++ b/old/switch_labels.py @@ -0,0 +1,120 @@ +#!/usr/bin/python +#coding: utf-8 +# +# @version: 0.1 +# @date: 19.04.2011 +# +# @description: A quick script to output a html page that prints +# switch labels in the format: - +# i.e: 71-4. One label per page. +# +# NB! only makes odd number labels. +# +# @author: technocake +# Found at: blog.technocake.net +#-------------------------------------------- + +import sys + +###################################### +# Configuration +###################################### +rows = 84 #rows +switches = 4 #switches per row +outFile = "tg-switch-labels-print-me.html" + + +#### CREATIVIA #### +creative_rows = 12 +creative_prepend = "C" + + +output = "" + +# the top of the html page +def head(): + return """ + + + + +""" + +#the bottom of the html page +def tail(): + return "" + +#ONE switch label +def a4(s ): + return "
%s
" % (s, ) + + +def saveToFile(data, fileName): + f = open(fileName, 'w+') + f.write( data ) + f.close() + +# In python 3, raw_input is renamed to input. In python v <3. input does something else. +# this function fixes that +def prompt(text): + try: + return raw_input(text) + except: + try: + return input(text) + + except: + exit() + + +################################################### +# This is where the actual generating takes place +################################################### + + +if __name__ == "__main__": + output += head() + + + #Generating all the labels for the switches + for row in range(1, rows+1, 2): + for SWITCH in range(1, switches+1): + output += a4("%s-%s\n" % (row, SWITCH) ) + + + # Generating all the labels for the CREATIVE area + for row in range(1, creative_rows+1): + output += a4("%s-%s\n" % (creative_prepend, row)) + + + + output += tail() + + # Taking it out to the big tg-world + + if len(sys.argv) > 1: + #Printing to stdout if second argument is passed to the script + print ( output ) + else: + saveToFile(output, outFile) + #normally, this is what happens. Saving it to a new html file + + + print ( """ + Generated labels for %d switches per row and %d rows. \n + The html file is in this folder, and is named %s \n + Pages to print: %d \n\n + """ + % (switches, rows, outFile, (switches*rows)/2 + creative_rows) + ) + + prompt( "Press any key to exit...") \ No newline at end of file -- cgit v1.2.3 From 8b2ce44bbe1456088f72d677a48cb230cf0991e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Mon, 1 Apr 2019 22:30:12 +0200 Subject: =?UTF-8?q?chore:=20Move=20switch=20label=20generation=20to=20own?= =?UTF-8?q?=20file=20=F0=9F=A7=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 46 +--------------------------------------------- switches.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 45 deletions(-) create mode 100644 switches.py diff --git a/main.py b/main.py index d397f1a..49f717b 100644 --- a/main.py +++ b/main.py @@ -1,48 +1,4 @@ -from gondul import fetch_gondul_switches - -switch_label_format = "%(switch_name)s-%(switch_num)s" -switch_label_layout = """ - - - -%s -""" -switch_label_page = '
%s
' - - -def generate_label(switch_name, switch_number): - return switch_label_page % switch_label_format % { - "switch_name": switch_name, - "switch_num": switch_number, - } - - -def generate_labels(switches): - labels = list(map(lambda switch: generate_label( - switch[1:].split("-")[0], switch.split("-")[1]), switches)) - - return switch_label_layout % "".join(labels) - - -def write_html_to_file(html, outfile="switch_labels.html"): - with open(outfile, "w") as f: - f.write(html) - print("Wrote labels to '{}'.\nOpen the file in your browser and print it.".format(outfile)) - - -def make_switch_labels(): - switches = fetch_gondul_switches() - labels = generate_labels(switches) - write_html_to_file(labels) +from switches import make_switch_labels if __name__ == "__main__": diff --git a/switches.py b/switches.py new file mode 100644 index 0000000..dbc7ece --- /dev/null +++ b/switches.py @@ -0,0 +1,45 @@ +from gondul import fetch_gondul_switches + +switch_label_format = "%(switch_name)s-%(switch_num)s" +switch_label_layout = """ + + + +%s +""" +switch_label_page = '
%s
' + + +def generate_label(switch_name, switch_number): + return switch_label_page % switch_label_format % { + "switch_name": switch_name, + "switch_num": switch_number, + } + + +def generate_labels(switches): + labels = list(map(lambda switch: generate_label( + switch[1:].split("-")[0], switch.split("-")[1]), switches)) + + return switch_label_layout % "".join(labels) + + +def write_html_to_file(html, outfile="switch_labels.html"): + with open(outfile, "w") as f: + f.write(html) + print("Wrote labels to '{}'.\nOpen the file in your browser and print it.".format(outfile)) + + +def make_switch_labels(): + switches = fetch_gondul_switches() + labels = generate_labels(switches) + write_html_to_file(labels) -- cgit v1.2.3 From 98e35a3fa02b598d104cde9aafb877006d8065b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Mon, 1 Apr 2019 22:43:27 +0200 Subject: =?UTF-8?q?feat:=20Add=20argparse=20to=20allow=20for=20specifying?= =?UTF-8?q?=20which=20labler=20function=20to=20run=20=F0=9F=A4=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cables.py | 2 ++ main.py | 17 ++++++++++++++++- switches.py | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 cables.py diff --git a/cables.py b/cables.py new file mode 100644 index 0000000..f32d7bc --- /dev/null +++ b/cables.py @@ -0,0 +1,2 @@ +def make_cable_labels(): + print("Generating labels for cables") diff --git a/main.py b/main.py index 49f717b..ca23f58 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,20 @@ +import argparse +import sys + from switches import make_switch_labels +from cables import make_cable_labels +parser = argparse.ArgumentParser("Label generator script 2000") +parser.add_argument("labler", type=str, + help="The label function to run. Either [c]ables or [s]witches.") if __name__ == "__main__": - make_switch_labels() + args = parser.parse_args() + + if args.labler[0] == "c": + make_cable_labels() + elif args.labler[0] == "s": + make_switch_labels() + else: + parser.print_help() + sys.exit("Invalid labler operation.") diff --git a/switches.py b/switches.py index dbc7ece..d6c80a7 100644 --- a/switches.py +++ b/switches.py @@ -40,6 +40,7 @@ def write_html_to_file(html, outfile="switch_labels.html"): def make_switch_labels(): + print("Generating labels for switches") switches = fetch_gondul_switches() labels = generate_labels(switches) write_html_to_file(labels) -- cgit v1.2.3 From 24d7c04b0a7210c6de5c83e81eb5119338685e41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Mon, 1 Apr 2019 23:37:42 +0200 Subject: =?UTF-8?q?feat:=20Implement=20label=20generation=20for=20cables?= =?UTF-8?q?=20=E2=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cables.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/cables.py b/cables.py index f32d7bc..cbc850d 100644 --- a/cables.py +++ b/cables.py @@ -1,2 +1,65 @@ -def make_cable_labels(): +from itertools import chain +import operator + +from gondul import fetch_gondul_switches + +cable_label_format = "%(switch_name)s-%(switch_num)s-%(cable_name)s" +mark_twice = True +num_tabs = 1 + + + +def generate_label(switch, cable_name): + data = { + "switch_name": switch.split("-")[0], + "switch_num": switch.split("-")[1], + "cable_name": cable_name, + } + label = cable_label_format % data + if not mark_twice: + return label + + return "{}{}{}".format(label, "\t" * num_tabs, label) + + +def generate_label_copies(switch, cable_name, copies=2): + return [generate_label(switch, cable_name) for _ in range(0, copies)] + + +def generate_labels(switches, copies=2, uplinks=3): + print("Generating {} copies of each label for {} uplinks for {} switches ({} labels)".format( + copies, uplinks, len(switches), len(switches) * uplinks * copies)) + labels = list(map(lambda switch: + [generate_label_copies(switch[1:], i + 1, copies=copies) + for i in range(0, uplinks)], + switches)) + return list(chain.from_iterable(chain.from_iterable(labels))) + + +def write_to_file(data, outfile="cable_labels.csv", filenum=1): + outfile_numbered = outfile.replace(".", "-{}.".format(filenum)) + + with open(outfile_numbered, "w") as f: + f.writelines("\n".join(data)) + + +def chunk_list(li, items): + for i in range(0, len(li), items): + yield li[i:i+items] + + +def write_csv(data, outfile="cable_labels.csv", split_per_num=100): + split_data = list(chunk_list(data, split_per_num)) + + for i in range(0, len(split_data)): + write_to_file(split_data[i], filenum=i+1) + + print("Wrote cable labels to {} files, starting from {}".format( + len(split_data), outfile.replace(".", "-1."))) + + +def make_cable_labels(uplinks=3): print("Generating labels for cables") + switches = fetch_gondul_switches() + labels = generate_labels(switches, uplinks=uplinks) + write_csv(labels) -- cgit v1.2.3 From e823b8fc077d4645a0565c8a769b1fb698b25a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Mon, 1 Apr 2019 23:47:22 +0200 Subject: =?UTF-8?q?chore:=20Make=20the=20code=20more=20readable=20and=20ex?= =?UTF-8?q?tensible=20=F0=9F=91=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cables.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/cables.py b/cables.py index cbc850d..a8f6b39 100644 --- a/cables.py +++ b/cables.py @@ -1,5 +1,4 @@ from itertools import chain -import operator from gondul import fetch_gondul_switches @@ -8,7 +7,6 @@ mark_twice = True num_tabs = 1 - def generate_label(switch, cable_name): data = { "switch_name": switch.split("-")[0], @@ -29,11 +27,18 @@ def generate_label_copies(switch, cable_name, copies=2): def generate_labels(switches, copies=2, uplinks=3): print("Generating {} copies of each label for {} uplinks for {} switches ({} labels)".format( copies, uplinks, len(switches), len(switches) * uplinks * copies)) - labels = list(map(lambda switch: - [generate_label_copies(switch[1:], i + 1, copies=copies) - for i in range(0, uplinks)], - switches)) - return list(chain.from_iterable(chain.from_iterable(labels))) + + labels = [] + for i in range(0, len(switches)): + switch = switches[i] + switch_name = switch[1:] + cable_labels = [generate_label_copies( + switch_name, uplink + 1, copies=copies) for uplink in range(0, uplinks)] + + # Destructure the list of copies into a flat list + labels.extend(chain.from_iterable(cable_labels)) + + return labels def write_to_file(data, outfile="cable_labels.csv", filenum=1): -- cgit v1.2.3 From aacc12dc9ab445e020444a5bd32b51f1fb62ddb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Mon, 1 Apr 2019 23:52:05 +0200 Subject: =?UTF-8?q?feat:=20Make=20it=20possible=20to=20supply=20a=20list?= =?UTF-8?q?=20of=20switch=20names=20for=20APs=20=F0=9F=99=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cables.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cables.py b/cables.py index a8f6b39..f2b8c79 100644 --- a/cables.py +++ b/cables.py @@ -24,9 +24,9 @@ def generate_label_copies(switch, cable_name, copies=2): return [generate_label(switch, cable_name) for _ in range(0, copies)] -def generate_labels(switches, copies=2, uplinks=3): - print("Generating {} copies of each label for {} uplinks for {} switches ({} labels)".format( - copies, uplinks, len(switches), len(switches) * uplinks * copies)) +def generate_labels(switches, aps=[], copies=2, uplinks=3): + print("Generating {} copies of each label for {} uplinks for {} switches and {} APs ({} labels)".format( + copies, uplinks, len(switches), len(aps), (len(switches) * uplinks + len(aps)) * copies)) labels = [] for i in range(0, len(switches)): @@ -38,6 +38,10 @@ def generate_labels(switches, copies=2, uplinks=3): # Destructure the list of copies into a flat list labels.extend(chain.from_iterable(cable_labels)) + if switch_name in aps: + labels.extend( + generate_label_copies(switch_name, "AP", copies=copies)) + return labels @@ -66,5 +70,5 @@ def write_csv(data, outfile="cable_labels.csv", split_per_num=100): def make_cable_labels(uplinks=3): print("Generating labels for cables") switches = fetch_gondul_switches() - labels = generate_labels(switches, uplinks=uplinks) + labels = generate_labels(switches, uplinks=uplinks, aps=[]) write_csv(labels) -- cgit v1.2.3 From 1ceefd7d700caf18f2390b94aa81d9f0207a4d85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Tue, 2 Apr 2019 00:58:09 +0200 Subject: =?UTF-8?q?chore:=20Refactor=20to=20get=20switches=20from=20gondul?= =?UTF-8?q?=20before=20executing=20labler=20=E2=99=BB=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cables.py | 5 +---- gondul.py | 16 +++++++++++++--- main.py | 25 ++++++++++++++++++++++--- switches.py | 5 +---- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/cables.py b/cables.py index f2b8c79..9226d88 100644 --- a/cables.py +++ b/cables.py @@ -1,7 +1,5 @@ from itertools import chain -from gondul import fetch_gondul_switches - cable_label_format = "%(switch_name)s-%(switch_num)s-%(cable_name)s" mark_twice = True num_tabs = 1 @@ -67,8 +65,7 @@ def write_csv(data, outfile="cable_labels.csv", split_per_num=100): len(split_data), outfile.replace(".", "-1."))) -def make_cable_labels(uplinks=3): +def make_cable_labels(switches, uplinks=3): print("Generating labels for cables") - switches = fetch_gondul_switches() labels = generate_labels(switches, uplinks=uplinks, aps=[]) write_csv(labels) diff --git a/gondul.py b/gondul.py index 65c18fa..74c1a23 100644 --- a/gondul.py +++ b/gondul.py @@ -61,6 +61,16 @@ def _sort_switches(switches): return sorted(switches, key=lambda x: (int(x[1:].split("-")[0]), x.split("-")[1])) -def fetch_gondul_switches(match="^e(.*)"): - # credentials = _generate_credentials() - return _sort_switches(_match_switches(_do_switches_request())) +def fetch_gondul_switches(api=None, endpoint=None, username=None, password=None, match="^e(.*)"): + # Use provided arg instead of environment variable if defined. + _api = api if api is not None else GONDUL_API + _endpoint = endpoint if endpoint is not None else GONDUL_SWITCHES_ENDPOINT + _username = username if username is not None else GONDUL_USERNAME + _password = password if password is not None else GONDUL_PASSWORD + credentials = _generate_credentials(_username, _password) + + return _sort_switches( + _match_switches( + _do_switches_request( + api=_api, endpoint=_endpoint, credentials=credentials), + match=match)) diff --git a/main.py b/main.py index ca23f58..90a0eb8 100644 --- a/main.py +++ b/main.py @@ -1,20 +1,39 @@ import argparse import sys -from switches import make_switch_labels from cables import make_cable_labels +from gondul import fetch_gondul_switches +from switches import make_switch_labels parser = argparse.ArgumentParser("Label generator script 2000") parser.add_argument("labler", type=str, help="The label function to run. Either [c]ables or [s]witches.") +parser.add_argument("--gondul-user", type=str, + help="Gondul username. Overrides env GONDUL_USERNAME") +parser.add_argument("--gondul-pass", type=str, + help="Gondul password. Overrides env GONDUL_PASSWORD") +parser.add_argument("--gondul-api", type=str, + help="Gondul API base. Overrides env GONDUL_API") +parser.add_argument("--gondul-switches", type=str, + help="Gondul switches endpoint. Overrides env GONDUL_SWITCHES_ENDPOINT") +parser.add_argument("--match-switches", type=str, default="^e(.*)", + help="Regex for matching switches") if __name__ == "__main__": args = parser.parse_args() + switches = fetch_gondul_switches( + api=args.gondul_api, + endpoint=args.gondul_switches, + username=args.gondul_user, + password=args.gondul_pass, + match=args.match_switches, + ) + if args.labler[0] == "c": - make_cable_labels() + make_cable_labels(switches) elif args.labler[0] == "s": - make_switch_labels() + make_switch_labels(switches) else: parser.print_help() sys.exit("Invalid labler operation.") diff --git a/switches.py b/switches.py index d6c80a7..ab444c6 100644 --- a/switches.py +++ b/switches.py @@ -1,5 +1,3 @@ -from gondul import fetch_gondul_switches - switch_label_format = "%(switch_name)s-%(switch_num)s" switch_label_layout = """ @@ -39,8 +37,7 @@ def write_html_to_file(html, outfile="switch_labels.html"): print("Wrote labels to '{}'.\nOpen the file in your browser and print it.".format(outfile)) -def make_switch_labels(): +def make_switch_labels(switches): print("Generating labels for switches") - switches = fetch_gondul_switches() labels = generate_labels(switches) write_html_to_file(labels) -- cgit v1.2.3 From eb5106409259144159c46cd4c2470621f4d6abe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Tue, 2 Apr 2019 00:59:16 +0200 Subject: =?UTF-8?q?feat:=20Make=20it=20possible=20to=20configure=20the=20l?= =?UTF-8?q?abler=20from=20the=20CLI=20=F0=9F=9A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cables.py | 22 +++++++++++++++++++--- main.py | 27 ++++++++++++++++++++++++--- switches.py | 4 ++-- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/cables.py b/cables.py index 9226d88..a7ff5de 100644 --- a/cables.py +++ b/cables.py @@ -65,7 +65,23 @@ def write_csv(data, outfile="cable_labels.csv", split_per_num=100): len(split_data), outfile.replace(".", "-1."))) -def make_cable_labels(switches, uplinks=3): +def read_aps_file(path): + aps = [] + with open(path, "r") as f: + aps = [line.replace("\n", "").strip() for line in f.readlines()] + + return aps + + +def make_cable_labels(switches, ap_file=None, aps=[], copies=2, outfile="cable_labels.csv", split_per_num=100, uplinks=3): print("Generating labels for cables") - labels = generate_labels(switches, uplinks=uplinks, aps=[]) - write_csv(labels) + + list_of_aps = aps + if ap_file: + list_of_aps.extend(read_aps_file(ap_file)) + + if len(list_of_aps): + print("Generating labels for {} APs".format(len(list_of_aps))) + + labels = generate_labels(switches, copies=copies, uplinks=uplinks, aps=aps) + write_csv(labels, outfile=outfile, split_per_num=split_per_num) diff --git a/main.py b/main.py index 90a0eb8..b4f7c53 100644 --- a/main.py +++ b/main.py @@ -5,7 +5,9 @@ from cables import make_cable_labels from gondul import fetch_gondul_switches from switches import make_switch_labels -parser = argparse.ArgumentParser("Label generator script 2000") +parser = argparse.ArgumentParser( + "Label generator script 2000", + formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("labler", type=str, help="The label function to run. Either [c]ables or [s]witches.") parser.add_argument("--gondul-user", type=str, @@ -18,6 +20,20 @@ parser.add_argument("--gondul-switches", type=str, help="Gondul switches endpoint. Overrides env GONDUL_SWITCHES_ENDPOINT") parser.add_argument("--match-switches", type=str, default="^e(.*)", help="Regex for matching switches") +parser.add_argument("--outfile", "-o", type=str, default="cable_labels.csv", + help="Output (base) file name. Might be appended with numbers for cables.") + +cables_args = parser.add_argument_group("cables") +cables_args.add_argument("--ap", type=str, action="append", + help="Name of a switch where an AP should be connected") +cables_args.add_argument("--aps-file", type=str, + help="Path to a newline-separated file with switches where an AP should be connected") +cables_args.add_argument("--copies", "-c", type=int, default=2, + help="Number of copies per label") +cables_args.add_argument("--uplinks", "-u", type=int, default=3, + help="Number of uplinks per switch") +cables_args.add_argument("--split", "-s", type=int, default=100, + help="Split into CSV files of this size") if __name__ == "__main__": args = parser.parse_args() @@ -31,9 +47,14 @@ if __name__ == "__main__": ) if args.labler[0] == "c": - make_cable_labels(switches) + make_cable_labels(switches, + aps=args.ap, + ap_file=args.aps_file, + copies=args.copies, + outfile=args.outfile, + split_per_num=args.split) elif args.labler[0] == "s": - make_switch_labels(switches) + make_switch_labels(switches, outfile=args.outfile) else: parser.print_help() sys.exit("Invalid labler operation.") diff --git a/switches.py b/switches.py index ab444c6..a218489 100644 --- a/switches.py +++ b/switches.py @@ -37,7 +37,7 @@ def write_html_to_file(html, outfile="switch_labels.html"): print("Wrote labels to '{}'.\nOpen the file in your browser and print it.".format(outfile)) -def make_switch_labels(switches): +def make_switch_labels(switches, outfile="switch_labels.html"): print("Generating labels for switches") labels = generate_labels(switches) - write_html_to_file(labels) + write_html_to_file(labels, outfile=outfile) -- cgit v1.2.3 From 7b2c213167401f8100031f38c61679b161d5d73e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Tue, 2 Apr 2019 01:18:25 +0200 Subject: =?UTF-8?q?chore:=20Update=20README=20with=20some=20instructions?= =?UTF-8?q?=20=F0=9F=93=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.rst b/README.rst index 7ea2815..c242d3c 100644 --- a/README.rst +++ b/README.rst @@ -2,3 +2,26 @@ TG label scripts ================ Scripts. + +Usage +----- + +The ``--help`` parameter is pretty helpful. + +Run the script with either ``cables`` or ``switches``, +depending if you want labels for cables or switches. +Configure the application further if needed. Consult ``--help``. + +Specify gondul credentials either using environment variables +(``GONDUL_USERNAME``, ``GONDUL_PASSWORD``) or the command line. +It's also possible to update the API root or API endpoint to use, +as well as a regex for matching switches. + +Specify the output file with the ``--outfile`` argument. + +For cables, specify the number of uplinks (``--uplinks``), +copies (``--copies``) and when to split (``--split``) +the CSV files for the label printer. Supply APs to print +labels for them to, either by identifying the switch with ``--ap`` +or by supplying a newline-separated file of switch identifiers +through ``--aps-file`` (e.g. ``33-1`` to add an AP to that switch). -- cgit v1.2.3 From cf5ca630fe3e34172ba1216e7e33d0f284d43ba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Tue, 2 Apr 2019 19:58:09 +0200 Subject: chore: Move tech support label scripts to own folder. After importing all the scripts from another repo. --- README.rst | 27 ------- cables.py | 87 ---------------------- gondul.py | 76 -------------------- main.py | 60 ---------------- old/README.txt | 29 -------- old/cable_labels.pl | 16 ----- old/switch_labels.py | 120 ------------------------------- switches.py | 43 ----------- tech-support/labels/README.rst | 27 +++++++ tech-support/labels/cables.py | 87 ++++++++++++++++++++++ tech-support/labels/gondul.py | 76 ++++++++++++++++++++ tech-support/labels/main.py | 60 ++++++++++++++++ tech-support/labels/old/README.txt | 29 ++++++++ tech-support/labels/old/cable_labels.pl | 16 +++++ tech-support/labels/old/switch_labels.py | 120 +++++++++++++++++++++++++++++++ tech-support/labels/switches.py | 43 +++++++++++ 16 files changed, 458 insertions(+), 458 deletions(-) delete mode 100644 README.rst delete mode 100644 cables.py delete mode 100644 gondul.py delete mode 100644 main.py delete mode 100644 old/README.txt delete mode 100644 old/cable_labels.pl delete mode 100644 old/switch_labels.py delete mode 100644 switches.py create mode 100644 tech-support/labels/README.rst create mode 100644 tech-support/labels/cables.py create mode 100644 tech-support/labels/gondul.py create mode 100644 tech-support/labels/main.py create mode 100644 tech-support/labels/old/README.txt create mode 100644 tech-support/labels/old/cable_labels.pl create mode 100644 tech-support/labels/old/switch_labels.py create mode 100644 tech-support/labels/switches.py diff --git a/README.rst b/README.rst deleted file mode 100644 index c242d3c..0000000 --- a/README.rst +++ /dev/null @@ -1,27 +0,0 @@ -TG label scripts -================ - -Scripts. - -Usage ------ - -The ``--help`` parameter is pretty helpful. - -Run the script with either ``cables`` or ``switches``, -depending if you want labels for cables or switches. -Configure the application further if needed. Consult ``--help``. - -Specify gondul credentials either using environment variables -(``GONDUL_USERNAME``, ``GONDUL_PASSWORD``) or the command line. -It's also possible to update the API root or API endpoint to use, -as well as a regex for matching switches. - -Specify the output file with the ``--outfile`` argument. - -For cables, specify the number of uplinks (``--uplinks``), -copies (``--copies``) and when to split (``--split``) -the CSV files for the label printer. Supply APs to print -labels for them to, either by identifying the switch with ``--ap`` -or by supplying a newline-separated file of switch identifiers -through ``--aps-file`` (e.g. ``33-1`` to add an AP to that switch). diff --git a/cables.py b/cables.py deleted file mode 100644 index a7ff5de..0000000 --- a/cables.py +++ /dev/null @@ -1,87 +0,0 @@ -from itertools import chain - -cable_label_format = "%(switch_name)s-%(switch_num)s-%(cable_name)s" -mark_twice = True -num_tabs = 1 - - -def generate_label(switch, cable_name): - data = { - "switch_name": switch.split("-")[0], - "switch_num": switch.split("-")[1], - "cable_name": cable_name, - } - label = cable_label_format % data - if not mark_twice: - return label - - return "{}{}{}".format(label, "\t" * num_tabs, label) - - -def generate_label_copies(switch, cable_name, copies=2): - return [generate_label(switch, cable_name) for _ in range(0, copies)] - - -def generate_labels(switches, aps=[], copies=2, uplinks=3): - print("Generating {} copies of each label for {} uplinks for {} switches and {} APs ({} labels)".format( - copies, uplinks, len(switches), len(aps), (len(switches) * uplinks + len(aps)) * copies)) - - labels = [] - for i in range(0, len(switches)): - switch = switches[i] - switch_name = switch[1:] - cable_labels = [generate_label_copies( - switch_name, uplink + 1, copies=copies) for uplink in range(0, uplinks)] - - # Destructure the list of copies into a flat list - labels.extend(chain.from_iterable(cable_labels)) - - if switch_name in aps: - labels.extend( - generate_label_copies(switch_name, "AP", copies=copies)) - - return labels - - -def write_to_file(data, outfile="cable_labels.csv", filenum=1): - outfile_numbered = outfile.replace(".", "-{}.".format(filenum)) - - with open(outfile_numbered, "w") as f: - f.writelines("\n".join(data)) - - -def chunk_list(li, items): - for i in range(0, len(li), items): - yield li[i:i+items] - - -def write_csv(data, outfile="cable_labels.csv", split_per_num=100): - split_data = list(chunk_list(data, split_per_num)) - - for i in range(0, len(split_data)): - write_to_file(split_data[i], filenum=i+1) - - print("Wrote cable labels to {} files, starting from {}".format( - len(split_data), outfile.replace(".", "-1."))) - - -def read_aps_file(path): - aps = [] - with open(path, "r") as f: - aps = [line.replace("\n", "").strip() for line in f.readlines()] - - return aps - - -def make_cable_labels(switches, ap_file=None, aps=[], copies=2, outfile="cable_labels.csv", split_per_num=100, uplinks=3): - print("Generating labels for cables") - - list_of_aps = aps - if ap_file: - list_of_aps.extend(read_aps_file(ap_file)) - - if len(list_of_aps): - print("Generating labels for {} APs".format(len(list_of_aps))) - - labels = generate_labels(switches, copies=copies, uplinks=uplinks, aps=aps) - write_csv(labels, outfile=outfile, split_per_num=split_per_num) diff --git a/gondul.py b/gondul.py deleted file mode 100644 index 74c1a23..0000000 --- a/gondul.py +++ /dev/null @@ -1,76 +0,0 @@ -import base64 -import json -import os -import re -import urllib.parse -import urllib.request - -GONDUL_USERNAME = os.getenv("GONDUL_USERNAME", "") -GONDUL_PASSWORD = os.getenv("GONDUL_PASSWORD", "") -GONDUL_API = os.getenv("GONDUL_API", "https://tg18.gondul.gathering.org/api") -GONDUL_SWITCHES_ENDPOINT = os.getenv( - "GONDUL_SWITCHES_ENDPOINT", "/public/switches") - - -def _generate_credentials(username, password): - return base64.standard_b64encode( - (username + ":" + password) - .encode("utf-8")).decode("utf-8") - - -def _do_switches_request( - api=GONDUL_API, - endpoint=GONDUL_SWITCHES_ENDPOINT, - credentials=_generate_credentials(GONDUL_USERNAME, GONDUL_PASSWORD)): - switches_url = api + endpoint - - # Build request - request = urllib.request.Request(switches_url) - request.add_header("Authorization", "Basic " + credentials) - resp = urllib.request.urlopen(request, timeout=5) - assert resp.status == 200, "HTTP return was not 200 OK" - - # Read response - body = resp.read().decode("utf-8") - data = json.loads(body) - assert "switches" in data, "Missing switches object from HTTP response" - - switches = data.get("switches") - print("Found {} switches in Gondul".format(len(switches))) - return switches - - -def _match_switches(switches, match="^e(.*)"): - pattern = re.compile(match) - - included_switches = [] - for switch in switches: - include = re.search(pattern, switch) - if include: - included_switches.append(switch) - - print("'{}' matches {} switches.".format(match, len(included_switches))) - return included_switches - - -def _sort_switches(switches): - # The lambda returns two values to compare on; - # * The switch number (e77-4) - picks out the number 77 - # * The number of the switch in relation to other switches on the same row - # E.g. "e77-4" will return 4 - return sorted(switches, key=lambda x: (int(x[1:].split("-")[0]), x.split("-")[1])) - - -def fetch_gondul_switches(api=None, endpoint=None, username=None, password=None, match="^e(.*)"): - # Use provided arg instead of environment variable if defined. - _api = api if api is not None else GONDUL_API - _endpoint = endpoint if endpoint is not None else GONDUL_SWITCHES_ENDPOINT - _username = username if username is not None else GONDUL_USERNAME - _password = password if password is not None else GONDUL_PASSWORD - credentials = _generate_credentials(_username, _password) - - return _sort_switches( - _match_switches( - _do_switches_request( - api=_api, endpoint=_endpoint, credentials=credentials), - match=match)) diff --git a/main.py b/main.py deleted file mode 100644 index b4f7c53..0000000 --- a/main.py +++ /dev/null @@ -1,60 +0,0 @@ -import argparse -import sys - -from cables import make_cable_labels -from gondul import fetch_gondul_switches -from switches import make_switch_labels - -parser = argparse.ArgumentParser( - "Label generator script 2000", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) -parser.add_argument("labler", type=str, - help="The label function to run. Either [c]ables or [s]witches.") -parser.add_argument("--gondul-user", type=str, - help="Gondul username. Overrides env GONDUL_USERNAME") -parser.add_argument("--gondul-pass", type=str, - help="Gondul password. Overrides env GONDUL_PASSWORD") -parser.add_argument("--gondul-api", type=str, - help="Gondul API base. Overrides env GONDUL_API") -parser.add_argument("--gondul-switches", type=str, - help="Gondul switches endpoint. Overrides env GONDUL_SWITCHES_ENDPOINT") -parser.add_argument("--match-switches", type=str, default="^e(.*)", - help="Regex for matching switches") -parser.add_argument("--outfile", "-o", type=str, default="cable_labels.csv", - help="Output (base) file name. Might be appended with numbers for cables.") - -cables_args = parser.add_argument_group("cables") -cables_args.add_argument("--ap", type=str, action="append", - help="Name of a switch where an AP should be connected") -cables_args.add_argument("--aps-file", type=str, - help="Path to a newline-separated file with switches where an AP should be connected") -cables_args.add_argument("--copies", "-c", type=int, default=2, - help="Number of copies per label") -cables_args.add_argument("--uplinks", "-u", type=int, default=3, - help="Number of uplinks per switch") -cables_args.add_argument("--split", "-s", type=int, default=100, - help="Split into CSV files of this size") - -if __name__ == "__main__": - args = parser.parse_args() - - switches = fetch_gondul_switches( - api=args.gondul_api, - endpoint=args.gondul_switches, - username=args.gondul_user, - password=args.gondul_pass, - match=args.match_switches, - ) - - if args.labler[0] == "c": - make_cable_labels(switches, - aps=args.ap, - ap_file=args.aps_file, - copies=args.copies, - outfile=args.outfile, - split_per_num=args.split) - elif args.labler[0] == "s": - make_switch_labels(switches, outfile=args.outfile) - else: - parser.print_help() - sys.exit("Invalid labler operation.") diff --git a/old/README.txt b/old/README.txt deleted file mode 100644 index fa02d44..0000000 --- a/old/README.txt +++ /dev/null @@ -1,29 +0,0 @@ -Disse filene brukes for � generere merking av kabler og switcher til The Gathering (Eller andre event med lignende behov) - -############## -switch_lables.py: -############## -Brukes til � generere lapper som henges opp p� switcher/switchstativer for enkel identifisering. - - -Howto: -Endre configen i filen (Antall rader, antall switcher, filnavn + eventuell config for Creativia), og kj�r filen med python. - -Den lager en HTML fil med valgt navn, som s� kan printes i en vanlig printer. - - -############## -cable_lables.pl -############## -Brukes til � generere teksten til lappene som settes i begge ender av alle kablene i hallen. - -CSV-filen mates inn i dymo programvaren og formatteres der. Husk at alle lapper m� skrives ut i to eksemplarer. - -Howto: -Kj�r filen med perl, sett variablene og pipe ut til csv med passende navn. - -Variablene filen spiser er f�lgende: Antall rader, antall switcher per rad, antall kabler per switch. - -Eksempel: - -perl cable_lables.pl 82 4 4 > Lapper.csv diff --git a/old/cable_labels.pl b/old/cable_labels.pl deleted file mode 100644 index b461d03..0000000 --- a/old/cable_labels.pl +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; - -my ($rows, $switches, $cables) = @ARGV; - -for my $row (1 .. $rows) { - next if (!($row & 1)); - - for my $switch (1 .. $switches) { - for my $cable (1 .. $cables) { - print join('-', ($row, $switch, $cable)) . ';' . "\n"; - } - } -} diff --git a/old/switch_labels.py b/old/switch_labels.py deleted file mode 100644 index 867c5ab..0000000 --- a/old/switch_labels.py +++ /dev/null @@ -1,120 +0,0 @@ -#!/usr/bin/python -#coding: utf-8 -# -# @version: 0.1 -# @date: 19.04.2011 -# -# @description: A quick script to output a html page that prints -# switch labels in the format: - -# i.e: 71-4. One label per page. -# -# NB! only makes odd number labels. -# -# @author: technocake -# Found at: blog.technocake.net -#-------------------------------------------- - -import sys - -###################################### -# Configuration -###################################### -rows = 84 #rows -switches = 4 #switches per row -outFile = "tg-switch-labels-print-me.html" - - -#### CREATIVIA #### -creative_rows = 12 -creative_prepend = "C" - - -output = "" - -# the top of the html page -def head(): - return """ - - - - -""" - -#the bottom of the html page -def tail(): - return "" - -#ONE switch label -def a4(s ): - return "
%s
" % (s, ) - - -def saveToFile(data, fileName): - f = open(fileName, 'w+') - f.write( data ) - f.close() - -# In python 3, raw_input is renamed to input. In python v <3. input does something else. -# this function fixes that -def prompt(text): - try: - return raw_input(text) - except: - try: - return input(text) - - except: - exit() - - -################################################### -# This is where the actual generating takes place -################################################### - - -if __name__ == "__main__": - output += head() - - - #Generating all the labels for the switches - for row in range(1, rows+1, 2): - for SWITCH in range(1, switches+1): - output += a4("%s-%s\n" % (row, SWITCH) ) - - - # Generating all the labels for the CREATIVE area - for row in range(1, creative_rows+1): - output += a4("%s-%s\n" % (creative_prepend, row)) - - - - output += tail() - - # Taking it out to the big tg-world - - if len(sys.argv) > 1: - #Printing to stdout if second argument is passed to the script - print ( output ) - else: - saveToFile(output, outFile) - #normally, this is what happens. Saving it to a new html file - - - print ( """ - Generated labels for %d switches per row and %d rows. \n - The html file is in this folder, and is named %s \n - Pages to print: %d \n\n - """ - % (switches, rows, outFile, (switches*rows)/2 + creative_rows) - ) - - prompt( "Press any key to exit...") \ No newline at end of file diff --git a/switches.py b/switches.py deleted file mode 100644 index a218489..0000000 --- a/switches.py +++ /dev/null @@ -1,43 +0,0 @@ -switch_label_format = "%(switch_name)s-%(switch_num)s" -switch_label_layout = """ - - - -%s -""" -switch_label_page = '
%s
' - - -def generate_label(switch_name, switch_number): - return switch_label_page % switch_label_format % { - "switch_name": switch_name, - "switch_num": switch_number, - } - - -def generate_labels(switches): - labels = list(map(lambda switch: generate_label( - switch[1:].split("-")[0], switch.split("-")[1]), switches)) - - return switch_label_layout % "".join(labels) - - -def write_html_to_file(html, outfile="switch_labels.html"): - with open(outfile, "w") as f: - f.write(html) - print("Wrote labels to '{}'.\nOpen the file in your browser and print it.".format(outfile)) - - -def make_switch_labels(switches, outfile="switch_labels.html"): - print("Generating labels for switches") - labels = generate_labels(switches) - write_html_to_file(labels, outfile=outfile) diff --git a/tech-support/labels/README.rst b/tech-support/labels/README.rst new file mode 100644 index 0000000..c242d3c --- /dev/null +++ b/tech-support/labels/README.rst @@ -0,0 +1,27 @@ +TG label scripts +================ + +Scripts. + +Usage +----- + +The ``--help`` parameter is pretty helpful. + +Run the script with either ``cables`` or ``switches``, +depending if you want labels for cables or switches. +Configure the application further if needed. Consult ``--help``. + +Specify gondul credentials either using environment variables +(``GONDUL_USERNAME``, ``GONDUL_PASSWORD``) or the command line. +It's also possible to update the API root or API endpoint to use, +as well as a regex for matching switches. + +Specify the output file with the ``--outfile`` argument. + +For cables, specify the number of uplinks (``--uplinks``), +copies (``--copies``) and when to split (``--split``) +the CSV files for the label printer. Supply APs to print +labels for them to, either by identifying the switch with ``--ap`` +or by supplying a newline-separated file of switch identifiers +through ``--aps-file`` (e.g. ``33-1`` to add an AP to that switch). diff --git a/tech-support/labels/cables.py b/tech-support/labels/cables.py new file mode 100644 index 0000000..a7ff5de --- /dev/null +++ b/tech-support/labels/cables.py @@ -0,0 +1,87 @@ +from itertools import chain + +cable_label_format = "%(switch_name)s-%(switch_num)s-%(cable_name)s" +mark_twice = True +num_tabs = 1 + + +def generate_label(switch, cable_name): + data = { + "switch_name": switch.split("-")[0], + "switch_num": switch.split("-")[1], + "cable_name": cable_name, + } + label = cable_label_format % data + if not mark_twice: + return label + + return "{}{}{}".format(label, "\t" * num_tabs, label) + + +def generate_label_copies(switch, cable_name, copies=2): + return [generate_label(switch, cable_name) for _ in range(0, copies)] + + +def generate_labels(switches, aps=[], copies=2, uplinks=3): + print("Generating {} copies of each label for {} uplinks for {} switches and {} APs ({} labels)".format( + copies, uplinks, len(switches), len(aps), (len(switches) * uplinks + len(aps)) * copies)) + + labels = [] + for i in range(0, len(switches)): + switch = switches[i] + switch_name = switch[1:] + cable_labels = [generate_label_copies( + switch_name, uplink + 1, copies=copies) for uplink in range(0, uplinks)] + + # Destructure the list of copies into a flat list + labels.extend(chain.from_iterable(cable_labels)) + + if switch_name in aps: + labels.extend( + generate_label_copies(switch_name, "AP", copies=copies)) + + return labels + + +def write_to_file(data, outfile="cable_labels.csv", filenum=1): + outfile_numbered = outfile.replace(".", "-{}.".format(filenum)) + + with open(outfile_numbered, "w") as f: + f.writelines("\n".join(data)) + + +def chunk_list(li, items): + for i in range(0, len(li), items): + yield li[i:i+items] + + +def write_csv(data, outfile="cable_labels.csv", split_per_num=100): + split_data = list(chunk_list(data, split_per_num)) + + for i in range(0, len(split_data)): + write_to_file(split_data[i], filenum=i+1) + + print("Wrote cable labels to {} files, starting from {}".format( + len(split_data), outfile.replace(".", "-1."))) + + +def read_aps_file(path): + aps = [] + with open(path, "r") as f: + aps = [line.replace("\n", "").strip() for line in f.readlines()] + + return aps + + +def make_cable_labels(switches, ap_file=None, aps=[], copies=2, outfile="cable_labels.csv", split_per_num=100, uplinks=3): + print("Generating labels for cables") + + list_of_aps = aps + if ap_file: + list_of_aps.extend(read_aps_file(ap_file)) + + if len(list_of_aps): + print("Generating labels for {} APs".format(len(list_of_aps))) + + labels = generate_labels(switches, copies=copies, uplinks=uplinks, aps=aps) + write_csv(labels, outfile=outfile, split_per_num=split_per_num) diff --git a/tech-support/labels/gondul.py b/tech-support/labels/gondul.py new file mode 100644 index 0000000..74c1a23 --- /dev/null +++ b/tech-support/labels/gondul.py @@ -0,0 +1,76 @@ +import base64 +import json +import os +import re +import urllib.parse +import urllib.request + +GONDUL_USERNAME = os.getenv("GONDUL_USERNAME", "") +GONDUL_PASSWORD = os.getenv("GONDUL_PASSWORD", "") +GONDUL_API = os.getenv("GONDUL_API", "https://tg18.gondul.gathering.org/api") +GONDUL_SWITCHES_ENDPOINT = os.getenv( + "GONDUL_SWITCHES_ENDPOINT", "/public/switches") + + +def _generate_credentials(username, password): + return base64.standard_b64encode( + (username + ":" + password) + .encode("utf-8")).decode("utf-8") + + +def _do_switches_request( + api=GONDUL_API, + endpoint=GONDUL_SWITCHES_ENDPOINT, + credentials=_generate_credentials(GONDUL_USERNAME, GONDUL_PASSWORD)): + switches_url = api + endpoint + + # Build request + request = urllib.request.Request(switches_url) + request.add_header("Authorization", "Basic " + credentials) + resp = urllib.request.urlopen(request, timeout=5) + assert resp.status == 200, "HTTP return was not 200 OK" + + # Read response + body = resp.read().decode("utf-8") + data = json.loads(body) + assert "switches" in data, "Missing switches object from HTTP response" + + switches = data.get("switches") + print("Found {} switches in Gondul".format(len(switches))) + return switches + + +def _match_switches(switches, match="^e(.*)"): + pattern = re.compile(match) + + included_switches = [] + for switch in switches: + include = re.search(pattern, switch) + if include: + included_switches.append(switch) + + print("'{}' matches {} switches.".format(match, len(included_switches))) + return included_switches + + +def _sort_switches(switches): + # The lambda returns two values to compare on; + # * The switch number (e77-4) - picks out the number 77 + # * The number of the switch in relation to other switches on the same row + # E.g. "e77-4" will return 4 + return sorted(switches, key=lambda x: (int(x[1:].split("-")[0]), x.split("-")[1])) + + +def fetch_gondul_switches(api=None, endpoint=None, username=None, password=None, match="^e(.*)"): + # Use provided arg instead of environment variable if defined. + _api = api if api is not None else GONDUL_API + _endpoint = endpoint if endpoint is not None else GONDUL_SWITCHES_ENDPOINT + _username = username if username is not None else GONDUL_USERNAME + _password = password if password is not None else GONDUL_PASSWORD + credentials = _generate_credentials(_username, _password) + + return _sort_switches( + _match_switches( + _do_switches_request( + api=_api, endpoint=_endpoint, credentials=credentials), + match=match)) diff --git a/tech-support/labels/main.py b/tech-support/labels/main.py new file mode 100644 index 0000000..b4f7c53 --- /dev/null +++ b/tech-support/labels/main.py @@ -0,0 +1,60 @@ +import argparse +import sys + +from cables import make_cable_labels +from gondul import fetch_gondul_switches +from switches import make_switch_labels + +parser = argparse.ArgumentParser( + "Label generator script 2000", + formatter_class=argparse.ArgumentDefaultsHelpFormatter) +parser.add_argument("labler", type=str, + help="The label function to run. Either [c]ables or [s]witches.") +parser.add_argument("--gondul-user", type=str, + help="Gondul username. Overrides env GONDUL_USERNAME") +parser.add_argument("--gondul-pass", type=str, + help="Gondul password. Overrides env GONDUL_PASSWORD") +parser.add_argument("--gondul-api", type=str, + help="Gondul API base. Overrides env GONDUL_API") +parser.add_argument("--gondul-switches", type=str, + help="Gondul switches endpoint. Overrides env GONDUL_SWITCHES_ENDPOINT") +parser.add_argument("--match-switches", type=str, default="^e(.*)", + help="Regex for matching switches") +parser.add_argument("--outfile", "-o", type=str, default="cable_labels.csv", + help="Output (base) file name. Might be appended with numbers for cables.") + +cables_args = parser.add_argument_group("cables") +cables_args.add_argument("--ap", type=str, action="append", + help="Name of a switch where an AP should be connected") +cables_args.add_argument("--aps-file", type=str, + help="Path to a newline-separated file with switches where an AP should be connected") +cables_args.add_argument("--copies", "-c", type=int, default=2, + help="Number of copies per label") +cables_args.add_argument("--uplinks", "-u", type=int, default=3, + help="Number of uplinks per switch") +cables_args.add_argument("--split", "-s", type=int, default=100, + help="Split into CSV files of this size") + +if __name__ == "__main__": + args = parser.parse_args() + + switches = fetch_gondul_switches( + api=args.gondul_api, + endpoint=args.gondul_switches, + username=args.gondul_user, + password=args.gondul_pass, + match=args.match_switches, + ) + + if args.labler[0] == "c": + make_cable_labels(switches, + aps=args.ap, + ap_file=args.aps_file, + copies=args.copies, + outfile=args.outfile, + split_per_num=args.split) + elif args.labler[0] == "s": + make_switch_labels(switches, outfile=args.outfile) + else: + parser.print_help() + sys.exit("Invalid labler operation.") diff --git a/tech-support/labels/old/README.txt b/tech-support/labels/old/README.txt new file mode 100644 index 0000000..fa02d44 --- /dev/null +++ b/tech-support/labels/old/README.txt @@ -0,0 +1,29 @@ +Disse filene brukes for � generere merking av kabler og switcher til The Gathering (Eller andre event med lignende behov) + +############## +switch_lables.py: +############## +Brukes til � generere lapper som henges opp p� switcher/switchstativer for enkel identifisering. + + +Howto: +Endre configen i filen (Antall rader, antall switcher, filnavn + eventuell config for Creativia), og kj�r filen med python. + +Den lager en HTML fil med valgt navn, som s� kan printes i en vanlig printer. + + +############## +cable_lables.pl +############## +Brukes til � generere teksten til lappene som settes i begge ender av alle kablene i hallen. + +CSV-filen mates inn i dymo programvaren og formatteres der. Husk at alle lapper m� skrives ut i to eksemplarer. + +Howto: +Kj�r filen med perl, sett variablene og pipe ut til csv med passende navn. + +Variablene filen spiser er f�lgende: Antall rader, antall switcher per rad, antall kabler per switch. + +Eksempel: + +perl cable_lables.pl 82 4 4 > Lapper.csv diff --git a/tech-support/labels/old/cable_labels.pl b/tech-support/labels/old/cable_labels.pl new file mode 100644 index 0000000..b461d03 --- /dev/null +++ b/tech-support/labels/old/cable_labels.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +my ($rows, $switches, $cables) = @ARGV; + +for my $row (1 .. $rows) { + next if (!($row & 1)); + + for my $switch (1 .. $switches) { + for my $cable (1 .. $cables) { + print join('-', ($row, $switch, $cable)) . ';' . "\n"; + } + } +} diff --git a/tech-support/labels/old/switch_labels.py b/tech-support/labels/old/switch_labels.py new file mode 100644 index 0000000..867c5ab --- /dev/null +++ b/tech-support/labels/old/switch_labels.py @@ -0,0 +1,120 @@ +#!/usr/bin/python +#coding: utf-8 +# +# @version: 0.1 +# @date: 19.04.2011 +# +# @description: A quick script to output a html page that prints +# switch labels in the format: - +# i.e: 71-4. One label per page. +# +# NB! only makes odd number labels. +# +# @author: technocake +# Found at: blog.technocake.net +#-------------------------------------------- + +import sys + +###################################### +# Configuration +###################################### +rows = 84 #rows +switches = 4 #switches per row +outFile = "tg-switch-labels-print-me.html" + + +#### CREATIVIA #### +creative_rows = 12 +creative_prepend = "C" + + +output = "" + +# the top of the html page +def head(): + return """ + + + + +""" + +#the bottom of the html page +def tail(): + return "" + +#ONE switch label +def a4(s ): + return "
%s
" % (s, ) + + +def saveToFile(data, fileName): + f = open(fileName, 'w+') + f.write( data ) + f.close() + +# In python 3, raw_input is renamed to input. In python v <3. input does something else. +# this function fixes that +def prompt(text): + try: + return raw_input(text) + except: + try: + return input(text) + + except: + exit() + + +################################################### +# This is where the actual generating takes place +################################################### + + +if __name__ == "__main__": + output += head() + + + #Generating all the labels for the switches + for row in range(1, rows+1, 2): + for SWITCH in range(1, switches+1): + output += a4("%s-%s\n" % (row, SWITCH) ) + + + # Generating all the labels for the CREATIVE area + for row in range(1, creative_rows+1): + output += a4("%s-%s\n" % (creative_prepend, row)) + + + + output += tail() + + # Taking it out to the big tg-world + + if len(sys.argv) > 1: + #Printing to stdout if second argument is passed to the script + print ( output ) + else: + saveToFile(output, outFile) + #normally, this is what happens. Saving it to a new html file + + + print ( """ + Generated labels for %d switches per row and %d rows. \n + The html file is in this folder, and is named %s \n + Pages to print: %d \n\n + """ + % (switches, rows, outFile, (switches*rows)/2 + creative_rows) + ) + + prompt( "Press any key to exit...") \ No newline at end of file diff --git a/tech-support/labels/switches.py b/tech-support/labels/switches.py new file mode 100644 index 0000000..a218489 --- /dev/null +++ b/tech-support/labels/switches.py @@ -0,0 +1,43 @@ +switch_label_format = "%(switch_name)s-%(switch_num)s" +switch_label_layout = """ + + + +%s +""" +switch_label_page = '
%s
' + + +def generate_label(switch_name, switch_number): + return switch_label_page % switch_label_format % { + "switch_name": switch_name, + "switch_num": switch_number, + } + + +def generate_labels(switches): + labels = list(map(lambda switch: generate_label( + switch[1:].split("-")[0], switch.split("-")[1]), switches)) + + return switch_label_layout % "".join(labels) + + +def write_html_to_file(html, outfile="switch_labels.html"): + with open(outfile, "w") as f: + f.write(html) + print("Wrote labels to '{}'.\nOpen the file in your browser and print it.".format(outfile)) + + +def make_switch_labels(switches, outfile="switch_labels.html"): + print("Generating labels for switches") + labels = generate_labels(switches) + write_html_to_file(labels, outfile=outfile) -- cgit v1.2.3 From 7cd4617b362b99562e265fd9715764e4976cdf3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Tue, 2 Apr 2019 20:08:09 +0200 Subject: =?UTF-8?q?fix:=20Support=20AP-less=20mode=20for=20cable=20label?= =?UTF-8?q?=20generation=20=F0=9F=99=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tech-support/labels/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tech-support/labels/main.py b/tech-support/labels/main.py index b4f7c53..a8a0ed7 100644 --- a/tech-support/labels/main.py +++ b/tech-support/labels/main.py @@ -48,7 +48,7 @@ if __name__ == "__main__": if args.labler[0] == "c": make_cable_labels(switches, - aps=args.ap, + aps=args.ap if args.ap is not None else [], ap_file=args.aps_file, copies=args.copies, outfile=args.outfile, -- cgit v1.2.3