diff options
-rw-r--r-- | cables.py | 5 | ||||
-rw-r--r-- | gondul.py | 16 | ||||
-rw-r--r-- | main.py | 25 | ||||
-rw-r--r-- | switches.py | 5 |
4 files changed, 37 insertions, 14 deletions
@@ -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) @@ -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)) @@ -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 = """<!DOCTYPE html> <html><head> @@ -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) |