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