aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHåkon Solbjørg <hlsolbjorg@gmail.com>2019-04-01 22:27:21 +0200
committerHåkon Solbjørg <hlsolbjorg@gmail.com>2019-04-02 19:55:47 +0200
commitebc46097a683a81fd9c2ad5dc3618f3f2ad17440 (patch)
treee4552fc585241f592d49a3c843e2195d41cc076d
parentf706bae1c3c2222a702e4121e1db8b7e9d45ab50 (diff)
Initial switch label scripts 🚂
-rw-r--r--README.rst4
-rw-r--r--gondul.py66
-rw-r--r--main.py49
3 files changed, 119 insertions, 0 deletions
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 = """<!DOCTYPE html>
+<html><head>
+ <style>
+ div.a4 {
+ font-size: 24em;
+ text-align: center;
+ @page size: A4 landscape;
+
+ /* this is the part that makes each div print per page. */
+ page-break-after: always;
+ }
+ </style>
+</head>
+<body>%s</body></html>
+"""
+switch_label_page = '<div class="a4">%s</div>'
+
+
+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()