Move api config options to bapsicle state for frozen packages.

This commit is contained in:
Matthew Stratford 2021-04-10 23:59:49 +01:00
parent a98388421c
commit 0faf3caa2e
8 changed files with 41 additions and 23 deletions

2
.gitignore vendored
View file

@ -24,8 +24,6 @@ build/output/
venv/ venv/
config.py
dev/welcome.mp3 dev/welcome.mp3
build/build-exe-pyinstaller-command.sh build/build-exe-pyinstaller-command.sh

View file

@ -1,3 +1,4 @@
from helpers.state_manager import StateManager
import json import json
from multiprocessing import Queue, current_process from multiprocessing import Queue, current_process
from helpers.logging_manager import LoggingManager from helpers.logging_manager import LoggingManager
@ -15,7 +16,7 @@ class APIHandler:
server_to_q: Queue server_to_q: Queue
server_from_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" process_title = "APIHandler"
setproctitle(process_title) setproctitle(process_title)
@ -24,7 +25,7 @@ class APIHandler:
self.server_from_q = server_from_q self.server_from_q = server_from_q
self.server_to_q = server_to_q self.server_to_q = server_to_q
self.logger = LoggingManager("APIHandler") self.logger = LoggingManager("APIHandler")
self.api = MyRadioAPI(self.logger) self.api = MyRadioAPI(self.logger, server_config)
self.handle() self.handle()

2
config.py Normal file
View file

@ -0,0 +1,2 @@
# BAPSicle Details
VERSION: float = 0.1

View file

@ -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"

View file

@ -19,29 +19,31 @@
from typing import Optional from typing import Optional
import requests import requests
import json import json
import config
from logging import INFO from logging import INFO
import os import os
from plan import PlanItem from plan import PlanItem
from helpers.os_environment import resolve_external_file_path from helpers.os_environment import resolve_external_file_path
from helpers.logging_manager import LoggingManager from helpers.logging_manager import LoggingManager
from helpers.state_manager import StateManager
class MyRadioAPI: class MyRadioAPI:
# TODO Config type
logger = None logger = None
def __init__(self, logger: LoggingManager): def __init__(self, logger: LoggingManager, config: StateManager):
self.logger = logger self.logger = logger
self.config = config
def get_non_api_call(self, url): 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: if "?" in url:
url += "&api_key={}".format(config.API_KEY) url += "&api_key={}".format(self.config.state["myradio_api_key"])
else: 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) self._log("Requesting non-API URL: " + url)
request = requests.get(url, timeout=10) request = requests.get(url, timeout=10)
@ -58,12 +60,12 @@ class MyRadioAPI:
def get_apiv2_call(self, url): 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: if "?" in url:
url += "&api_key={}".format(config.API_KEY) url += "&api_key={}".format(self.config.state["myradio_api_key"])
else: 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) self._log("Requesting API V2 URL: " + url)
request = requests.get(url, timeout=10) request = requests.get(url, timeout=10)

View file

@ -519,7 +519,7 @@ class Player:
custom_prefix="ALL:STATUS:") custom_prefix="ALL:STATUS:")
def __init__( 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) process_title = "Player: Channel " + str(channel)
@ -531,7 +531,7 @@ class Player:
self.logger = LoggingManager("Player" + str(channel)) self.logger = LoggingManager("Player" + str(channel))
self.api = MyRadioAPI(self.logger) self.api = MyRadioAPI(self.logger, server_config)
self.state = StateManager( self.state = StateManager(
"Player" + str(channel), "Player" + str(channel),

View file

@ -64,6 +64,9 @@ default_state = {
"num_channels": 3, "num_channels": 3,
"ser_port": None, "ser_port": None,
"ser_connected": False, "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("num_channels", int(request.form["channels"]))
state.update("ws_port", int(request.form["ws_port"])) state.update("ws_port", int(request.form["ws_port"]))
state.update("serial_port", request.form["serial_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() # stopServer()
return server_config() return server_config()
@ -508,10 +518,13 @@ def startServer():
ui_to_q.append(multiprocessing.Queue()) ui_to_q.append(multiprocessing.Queue())
websocket_to_q.append(multiprocessing.Queue()) websocket_to_q.append(multiprocessing.Queue())
controller_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( channel_p.append(
multiprocessing.Process( multiprocessing.Process(
target=player.Player, 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 # daemon=True
) )
) )
@ -521,7 +534,7 @@ def startServer():
api_to_q = multiprocessing.Queue() api_to_q = multiprocessing.Queue()
api_from_q = multiprocessing.Queue() api_from_q = multiprocessing.Queue()
api_handler = multiprocessing.Process( 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() api_handler.start()

View file

@ -24,7 +24,16 @@
<option value="{{port}}" {% if port == data.state.serial_port %}selected{% endif %}>{{port}}</option> <option value="{{port}}" {% if port == data.state.serial_port %}selected{% endif %}>{{port}}</option>
{% endfor %} {% endfor %}
</select> </select>
<hr>
<label for="myradio_base_url">MyRadio Base URL:</label>
<input type="text" id="myradio_base_url" name="myradio_base_url" class="form-control" value="{{data.state.myradio_base_url}}">
<br> <br>
<label for="myradio_api_url">MyRadio API URL:</label>
<input type="text" id="myradio_api_url" name="myradio_api_url" class="form-control" value="{{data.state.myradio_api_url}}">
<br>
<label for="myradio_api_key">MyRadio API Key:</label>
<input type="text" id="myradio_api_key" name="myradio_api_key" class="form-control" placeholder="Hidden ({% if data.state.myradio_api_key %}value set, type to replace{% else %}value not set yet{% endif %})" value="">
<hr>
<input type="submit" class="btn btn-primary" value="Save & Restart Server"> <input type="submit" class="btn btn-primary" value="Save & Restart Server">
</form> </form>
{% endif %} {% endif %}