From 0faf3caa2e0bf51d011e4a98e1674e22bd94cb77 Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Sat, 10 Apr 2021 23:59:49 +0100 Subject: [PATCH] Move api config options to bapsicle state for frozen packages. --- .gitignore | 2 -- api_handler.py | 5 +++-- config.py | 2 ++ config.py.example | 7 ------- helpers/myradio_api.py | 18 ++++++++++-------- player.py | 4 ++-- server.py | 17 +++++++++++++++-- templates/server.html | 9 +++++++++ 8 files changed, 41 insertions(+), 23 deletions(-) create mode 100644 config.py delete mode 100644 config.py.example diff --git a/.gitignore b/.gitignore index b1553d1..1f5a376 100644 --- a/.gitignore +++ b/.gitignore @@ -24,8 +24,6 @@ build/output/ venv/ -config.py - dev/welcome.mp3 build/build-exe-pyinstaller-command.sh diff --git a/api_handler.py b/api_handler.py index e770782..5764235 100644 --- a/api_handler.py +++ b/api_handler.py @@ -1,3 +1,4 @@ +from helpers.state_manager import StateManager import json from multiprocessing import Queue, current_process from helpers.logging_manager import LoggingManager @@ -15,7 +16,7 @@ class APIHandler: server_to_q: Queue server_from_q: Queue - def __init__(self, server_from_q: Queue, server_to_q: Queue): + def __init__(self, server_from_q: Queue, server_to_q: Queue, server_config: StateManager): process_title = "APIHandler" setproctitle(process_title) @@ -24,7 +25,7 @@ class APIHandler: self.server_from_q = server_from_q self.server_to_q = server_to_q self.logger = LoggingManager("APIHandler") - self.api = MyRadioAPI(self.logger) + self.api = MyRadioAPI(self.logger, server_config) self.handle() diff --git a/config.py b/config.py new file mode 100644 index 0000000..563451d --- /dev/null +++ b/config.py @@ -0,0 +1,2 @@ +# BAPSicle Details +VERSION: float = 0.1 diff --git a/config.py.example b/config.py.example deleted file mode 100644 index 8fc81fe..0000000 --- a/config.py.example +++ /dev/null @@ -1,7 +0,0 @@ -# BAPSicle Details -VERSION: float = 1.0 - -# API Settings -API_KEY: str = "" -MYRADIO_BASE_URL: str = "https://ury.org.uk/myradio" -MYRADIO_API_URL: str = "https://ury.org.uk/api" diff --git a/helpers/myradio_api.py b/helpers/myradio_api.py index a57c315..d2f1a0e 100644 --- a/helpers/myradio_api.py +++ b/helpers/myradio_api.py @@ -19,29 +19,31 @@ from typing import Optional import requests import json -import config from logging import INFO import os from plan import PlanItem from helpers.os_environment import resolve_external_file_path from helpers.logging_manager import LoggingManager +from helpers.state_manager import StateManager class MyRadioAPI: + # TODO Config type logger = None - def __init__(self, logger: LoggingManager): + def __init__(self, logger: LoggingManager, config: StateManager): self.logger = logger + self.config = config def get_non_api_call(self, url): - url = "{}{}".format(config.MYRADIO_BASE_URL, url) + url = "{}{}".format(self.config.state["myradio_base_url"], url) if "?" in url: - url += "&api_key={}".format(config.API_KEY) + url += "&api_key={}".format(self.config.state["myradio_api_key"]) else: - url += "?api_key={}".format(config.API_KEY) + url += "?api_key={}".format(self.config.state["myradio_api_key"]) self._log("Requesting non-API URL: " + url) request = requests.get(url, timeout=10) @@ -58,12 +60,12 @@ class MyRadioAPI: def get_apiv2_call(self, url): - url = "{}/v2{}".format(config.MYRADIO_API_URL, url) + url = "{}/v2{}".format(self.config.state["myradio_api_url"], url) if "?" in url: - url += "&api_key={}".format(config.API_KEY) + url += "&api_key={}".format(self.config.state["myradio_api_key"]) else: - url += "?api_key={}".format(config.API_KEY) + url += "?api_key={}".format(self.config.state["myradio_api_key"]) self._log("Requesting API V2 URL: " + url) request = requests.get(url, timeout=10) diff --git a/player.py b/player.py index e24b80b..9f0bf11 100644 --- a/player.py +++ b/player.py @@ -519,7 +519,7 @@ class Player: custom_prefix="ALL:STATUS:") def __init__( - self, channel: int, in_q: multiprocessing.Queue, out_q: multiprocessing.Queue + self, channel: int, in_q: multiprocessing.Queue, out_q: multiprocessing.Queue, server_config: StateManager ): process_title = "Player: Channel " + str(channel) @@ -531,7 +531,7 @@ class Player: self.logger = LoggingManager("Player" + str(channel)) - self.api = MyRadioAPI(self.logger) + self.api = MyRadioAPI(self.logger, server_config) self.state = StateManager( "Player" + str(channel), diff --git a/server.py b/server.py index eeb1101..9e28254 100644 --- a/server.py +++ b/server.py @@ -64,6 +64,9 @@ default_state = { "num_channels": 3, "ser_port": None, "ser_connected": False, + "api_key": None, + "myradio_base_url": "https://ury.org.uk/myradio", + "myradio_api_url": "https://ury.org.uk/api" } @@ -162,6 +165,13 @@ def update_server(): state.update("num_channels", int(request.form["channels"])) state.update("ws_port", int(request.form["ws_port"])) state.update("serial_port", request.form["serial_port"]) + + # Because we're not showing the api key once it's set. + if "myradio_api_key" in request.form and request.form["myradio_api_key"] != "": + state.update("myradio_api_key", request.form["myradio_api_key"]) + + state.update("myradio_base_url", request.form["myradio_base_url"]) + state.update("myradio_api_url", request.form["myradio_api_url"]) # stopServer() return server_config() @@ -508,10 +518,13 @@ def startServer(): ui_to_q.append(multiprocessing.Queue()) websocket_to_q.append(multiprocessing.Queue()) controller_to_q.append(multiprocessing.Queue()) + + # TODO Replace state with individual read-only StateManagers or something nicer? + channel_p.append( multiprocessing.Process( target=player.Player, - args=(channel, channel_to_q[-1], channel_from_q[-1]) + args=(channel, channel_to_q[-1], channel_from_q[-1], state) # daemon=True ) ) @@ -521,7 +534,7 @@ def startServer(): api_to_q = multiprocessing.Queue() api_from_q = multiprocessing.Queue() api_handler = multiprocessing.Process( - target=APIHandler, args=(api_to_q, api_from_q) + target=APIHandler, args=(api_to_q, api_from_q, state) ) api_handler.start() diff --git a/templates/server.html b/templates/server.html index 8a65f90..f6daf67 100644 --- a/templates/server.html +++ b/templates/server.html @@ -24,7 +24,16 @@ {% endfor %} +
+ +
+ + +
+ + +
{% endif %}