From ae346e0cf8c4b682f2b8eb7cc89e4b2d8d35b57d Mon Sep 17 00:00:00 2001 From: Christopher Bailey Date: Sat, 17 Jul 2021 14:52:30 -0400 Subject: [PATCH] Event -> EventTypes --- .bin/lib/common.sh | 2 +- .bin/update-requirements | 2 ++ HISTORY.rst | 6 ++++++ MANIFEST.in | 12 ------------ pyproject.toml | 2 +- sxm_player/__init__.py | 2 +- sxm_player/cli.py | 4 ++-- sxm_player/debug/worker.py | 14 +++++++++----- sxm_player/handlers.py | 20 ++++++++++---------- sxm_player/models.py | 4 ++-- sxm_player/py.typed | 0 sxm_player/queue.py | 4 ++-- sxm_player/workers/archiver.py | 4 ++-- sxm_player/workers/base.py | 12 ++++++------ sxm_player/workers/cli.py | 14 +++++++------- sxm_player/workers/hls.py | 10 ++++++---- sxm_player/workers/server.py | 6 +++--- sxm_player/workers/status.py | 8 +++++--- 18 files changed, 65 insertions(+), 61 deletions(-) delete mode 100644 MANIFEST.in create mode 100644 sxm_player/py.typed diff --git a/.bin/lib/common.sh b/.bin/lib/common.sh index a2e07ec..8a5e40e 100644 --- a/.bin/lib/common.sh +++ b/.bin/lib/common.sh @@ -28,7 +28,7 @@ function setLintModules() { if [[ -f "$ROOT_PATH/conftest.py" ]]; then echo "Found \`conftest.py\`" BANDIT_MODULES="conftest" - PYLINT_MODULES="contest.py" + PYLINT_MODULES="conftest.py" fi if [[ -d "$ROOT_PATH/tests" ]]; then diff --git a/.bin/update-requirements b/.bin/update-requirements index 47f790f..0aeb42b 100755 --- a/.bin/update-requirements +++ b/.bin/update-requirements @@ -13,6 +13,8 @@ pushd "$BASE_DIR" 2>&1 >/dev/null pip-compile --upgrade --output-file=requirements.txt pyproject.toml pip-compile --upgrade --extra=dev --extra=doc --extra=test --output-file=dev-requirements.txt pyproject.toml +pip install -r dev-requirements.txt +cd ../sxm-player && flit install --symlink || true flit install --symlink popd 2>&1 >/dev/null diff --git a/HISTORY.rst b/HISTORY.rst index 4dac6df..ad9d185 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,12 @@ History ======= +0.2.1 (2021-08-17) +------------------ + +* Adds missing py.typed to add stubs +* Renames `queue.Event` enum to `queue.EventTypes` + 0.2.0 (2021-08-15) ------------------ diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index b600a5c..0000000 --- a/MANIFEST.in +++ /dev/null @@ -1,12 +0,0 @@ -include AUTHORS.rst -include CONTRIBUTING.rst -include HISTORY.rst -include LICENSE -include README.rst -include reqs/*.in - -recursive-include tests * -recursive-exclude * __pycache__ -recursive-exclude * *.py[co] - -recursive-include docs *.rst conf.py Makefile make.bat *.jpg *.png *.gif diff --git a/pyproject.toml b/pyproject.toml index 8ae37ee..d6d534e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -85,7 +85,7 @@ include = [ "HISTORY.rst", "LICENSE", "README.rst", - "sxm/*", + "sxm_player/*", "tests/*", "docs/*.rst", "docs/conf.py", diff --git a/sxm_player/__init__.py b/sxm_player/__init__.py index 0aa99a2..9dcbde8 100644 --- a/sxm_player/__init__.py +++ b/sxm_player/__init__.py @@ -4,4 +4,4 @@ __author__ = """Christopher Bailey""" __email__ = "cbailey@mort.is" -__version__ = "0.2.0" +__version__ = "0.2.1" diff --git a/sxm_player/cli.py b/sxm_player/cli.py index 7811165..0244fb9 100644 --- a/sxm_player/cli.py +++ b/sxm_player/cli.py @@ -23,7 +23,7 @@ from sxm_player.command import validate_player from sxm_player.models import PlayerState from sxm_player.players import BasePlayer -from sxm_player.queue import Event, EventMessage +from sxm_player.queue import EventMessage, EventTypes from sxm_player.runner import Runner from sxm_player.utils import ACTIVE_PROCESS_STATUSES from sxm_player.workers import ServerWorker, StatusWorker @@ -168,7 +168,7 @@ def event_loop(runner: Runner, state: PlayerState, **kwargs): ) state.sxm_running = True - handlers.sxm_status_event(runner, Event.SXM_STATUS, state.sxm_running) + handlers.sxm_status_event(runner, EventTypes.SXM_STATUS, state.sxm_running) check_player(runner, state) diff --git a/sxm_player/debug/worker.py b/sxm_player/debug/worker.py index 502d1d3..ff39216 100644 --- a/sxm_player/debug/worker.py +++ b/sxm_player/debug/worker.py @@ -1,7 +1,7 @@ import os from bdb import BdbQuit -from sxm_player.queue import Event, EventMessage +from sxm_player.queue import EventMessage, EventTypes from sxm_player.workers.base import InterruptableWorker __all__ = ["DebugWorker"] @@ -32,21 +32,25 @@ def play_channel(self, channel_id: str, protocol: str = "udp"): self.push_event( EventMessage( self.name, - Event.DEBUG_START_PLAYER, + EventTypes.DEBUG_START_PLAYER, (player_name, channel_id, filename, protocol), ) ) def stop_player(self, player_name, kill_hls=True): - self.push_event(EventMessage(self.name, Event.DEBUG_STOP_PLAYER, player_name)) + self.push_event( + EventMessage(self.name, EventTypes.DEBUG_STOP_PLAYER, player_name) + ) if kill_hls: self.kill_hls() def trigger_hls(self, channel_id, protocol="udp"): self.push_event( - EventMessage(self.name, Event.TRIGGER_HLS_STREAM, (channel_id, protocol)) + EventMessage( + self.name, EventTypes.TRIGGER_HLS_STREAM, (channel_id, protocol) + ) ) def kill_hls(self): - self.push_event(EventMessage(self.name, Event.KILL_HLS_STREAM, None)) + self.push_event(EventMessage(self.name, EventTypes.KILL_HLS_STREAM, None)) diff --git a/sxm_player/handlers.py b/sxm_player/handlers.py index 2508683..6a1fee0 100644 --- a/sxm_player/handlers.py +++ b/sxm_player/handlers.py @@ -2,7 +2,7 @@ from typing import Optional from sxm_player.models import PlayerState -from sxm_player.queue import Event, EventMessage +from sxm_player.queue import EventMessage, EventTypes from sxm_player.runner import Runner, Worker from sxm_player.workers import ( ArchiveWorker, @@ -14,24 +14,24 @@ def hls_start_event(runner: Runner, stream_data: tuple, src: Optional[str] = None): - hls_event(runner, Event.HLS_STREAM_STARTED, stream_data, src=src) + hls_event(runner, EventTypes.HLS_STREAM_STARTED, stream_data, src=src) def hls_kill_event(runner: Runner, src: Optional[str] = None): - hls_event(runner, Event.KILL_HLS_STREAM, None, src=src) + hls_event(runner, EventTypes.KILL_HLS_STREAM, None, src=src) def hls_metadata_event(runner: Runner, live_data: tuple, src: Optional[str] = None): - hls_event(runner, Event.UPDATE_METADATA, live_data, src=src) + hls_event(runner, EventTypes.UPDATE_METADATA, live_data, src=src) def hls_channels_event( runner: Runner, channels: Optional[list], src: Optional[str] = None ): - hls_event(runner, Event.UPDATE_CHANNELS, channels, src=src) + hls_event(runner, EventTypes.UPDATE_CHANNELS, channels, src=src) -def hls_event(runner: Runner, event: Event, data, src: Optional[str] = None): +def hls_event(runner: Runner, event: EventTypes, data, src: Optional[str] = None): for worker in runner.workers.values(): if worker.hls_stream_queue is not None: if src is None: @@ -51,7 +51,7 @@ def hls_event(runner: Runner, event: Event, data, src: Optional[str] = None): def sxm_status_event( - runner: Runner, event: Event, status: bool, src: Optional[str] = None + runner: Runner, event: EventTypes, status: bool, src: Optional[str] = None ): for worker in runner.workers.values(): if worker.sxm_status_queue is not None: @@ -107,7 +107,7 @@ def handle_reset_sxm_event( del runner.workers[ServerWorker.NAME] state.sxm_running = False - sxm_status_event(runner, Event.SXM_STATUS, state.sxm_running) + sxm_status_event(runner, EventTypes.SXM_STATUS, state.sxm_running) def handle_trigger_hls_stream_event( @@ -134,13 +134,13 @@ def handle_trigger_hls_stream_event( src_worker, "hls_stream_queue", EventMessage( - event.msg_src, Event.HLS_STREAM_STARTED, state.stream_data + event.msg_src, EventTypes.HLS_STREAM_STARTED, state.stream_data ), ) runner.log.info( f"Could not start new {HLSWorker.NAME}, one is " "already running passing " - f"{Event.HLS_STREAM_STARTED} instead" + f"{EventTypes.HLS_STREAM_STARTED} instead" ) else: runner.log.warning( diff --git a/sxm_player/models.py b/sxm_player/models.py index 915a6a9..f5886dd 100644 --- a/sxm_player/models.py +++ b/sxm_player/models.py @@ -303,7 +303,7 @@ def mark_failure(self) -> float: self._last_failure = time.monotonic() return self._cooldown - def get_channel(self, name: str) -> Union[XMChannel, None]: + def get_channel(self, name: str) -> Optional[XMChannel]: """Returns channel from list of `channels` with given name""" name = name.lower() @@ -311,7 +311,7 @@ def get_channel(self, name: str) -> Union[XMChannel, None]: if ( channel.name.lower() == name or channel.id.lower() == name - or channel.channel_number == name + or str(channel.channel_number) == name ): return channel return None diff --git a/sxm_player/py.typed b/sxm_player/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/sxm_player/queue.py b/sxm_player/queue.py index 591c352..c017a98 100644 --- a/sxm_player/queue.py +++ b/sxm_player/queue.py @@ -8,7 +8,7 @@ DEFAULT_POLLING_TIMEOUT = 0.02 -class Event(Enum): +class EventTypes(Enum): RESET_SXM = auto() SXM_STATUS = auto() UPDATE_CHANNELS = auto() @@ -25,7 +25,7 @@ class EventMessage: id: float # noqa: A003 msg_src: str msg_relay: str - msg_type: Event + msg_type: EventTypes msg: Any def __init__(self, msg_src, msg_type, msg, msg_relay=None): diff --git a/sxm_player/workers/archiver.py b/sxm_player/workers/archiver.py index 03130e6..e1b2be8 100644 --- a/sxm_player/workers/archiver.py +++ b/sxm_player/workers/archiver.py @@ -3,7 +3,7 @@ from time import monotonic from typing import Dict, Optional, Tuple, Union -from sxm_player.queue import Event, EventMessage +from sxm_player.queue import EventMessage, EventTypes from sxm_player.utils import create_fs_datetime, get_files, splice_file from sxm_player.workers.base import HLSLoopedWorker @@ -77,7 +77,7 @@ def _validate_name(self, stream_file) -> bool: def _validate_size(self, stream_file) -> bool: if not self._check_size(stream_file): self._log.error("archive not increasing, resetting channel") - self.push_event(EventMessage(self.name, Event.KILL_HLS_STREAM, None)) + self.push_event(EventMessage(self.name, EventTypes.KILL_HLS_STREAM, None)) return False return True diff --git a/sxm_player/workers/base.py b/sxm_player/workers/base.py index 23bca89..9bfb33e 100644 --- a/sxm_player/workers/base.py +++ b/sxm_player/workers/base.py @@ -5,7 +5,7 @@ from typing import List, Optional, Tuple from ..models import PlayerState -from ..queue import Event, EventMessage, Queue +from ..queue import EventMessage, EventTypes, Queue from ..signals import default_signal_handler, init_signals, interupt_signal_handler __all__ = [ @@ -161,7 +161,7 @@ def __init__(self, sxm_status: bool, *args, **kwargs): self._event_queues = [self.sxm_status_queue] def _handle_event(self, event: EventMessage): - if event.msg_type == Event.SXM_STATUS: + if event.msg_type == EventTypes.SXM_STATUS: self._state.sxm_running = event.msg else: self._log.warning( @@ -198,13 +198,13 @@ def __init__( self._state.set_raw_live(raw_live_data) def _handle_event(self, event: EventMessage): - if event.msg_type == Event.HLS_STREAM_STARTED: + if event.msg_type == EventTypes.HLS_STREAM_STARTED: self._state.update_stream_data(event.msg) - elif event.msg_type == Event.UPDATE_METADATA: + elif event.msg_type == EventTypes.UPDATE_METADATA: self._state.set_raw_live(event.msg) - elif event.msg_type == Event.UPDATE_CHANNELS: + elif event.msg_type == EventTypes.UPDATE_CHANNELS: self._state.update_channels(event.msg) - elif event.msg_type == Event.KILL_HLS_STREAM: + elif event.msg_type == EventTypes.KILL_HLS_STREAM: self.local_shutdown_event.set() else: self._log.warning( diff --git a/sxm_player/workers/cli.py b/sxm_player/workers/cli.py index 1fab053..102b2f7 100644 --- a/sxm_player/workers/cli.py +++ b/sxm_player/workers/cli.py @@ -1,7 +1,7 @@ import time from typing import Optional -from ..queue import Event, EventMessage +from ..queue import EventMessage, EventTypes from ..utils import FFmpeg from .base import ComboLoopedWorker @@ -58,7 +58,7 @@ def _invalid_stream_loop(self): self.push_event( EventMessage( self.name, - Event.TRIGGER_HLS_STREAM, + EventTypes.TRIGGER_HLS_STREAM, (self.channel_id, self.stream_protocol), ) ) @@ -71,15 +71,15 @@ def cleanup(self): self._state.update_stream_data((None, None)) def _handle_event(self, event: EventMessage): - if event.msg_type == Event.SXM_STATUS: + if event.msg_type == EventTypes.SXM_STATUS: self._state.sxm_running = event.msg - elif event.msg_type == Event.HLS_STREAM_STARTED: + elif event.msg_type == EventTypes.HLS_STREAM_STARTED: self._state.update_stream_data(event.msg) - elif event.msg_type == Event.UPDATE_METADATA: + elif event.msg_type == EventTypes.UPDATE_METADATA: self._state.set_raw_live(event.msg) - elif event.msg_type == Event.UPDATE_CHANNELS: + elif event.msg_type == EventTypes.UPDATE_CHANNELS: self._state.update_channels(event.msg) - elif event.msg_type == Event.KILL_HLS_STREAM: + elif event.msg_type == EventTypes.KILL_HLS_STREAM: self._log.info("stream is stopping, killing ffmpeg") self.cleanup() else: diff --git a/sxm_player/workers/hls.py b/sxm_player/workers/hls.py index b572ebe..d00ef2e 100644 --- a/sxm_player/workers/hls.py +++ b/sxm_player/workers/hls.py @@ -3,7 +3,7 @@ import time from typing import Optional, Tuple -from ..queue import Event, EventMessage +from ..queue import EventMessage, EventTypes from ..utils import FFmpeg from .base import SXMLoopedWorker @@ -101,7 +101,7 @@ def setup(self): self.push_event( EventMessage( self.name, - Event.HLS_STREAM_STARTED, + EventTypes.HLS_STREAM_STARTED, (self.channel_id, self.playback_url), ) ) @@ -130,9 +130,11 @@ def loop(self): if len(lines) > 0: self._log.debug(f"adding {len(lines)} of stderr to shared memory") - self.push_event(EventMessage(self.name, Event.HLS_STDERROR_LINES, lines)) + self.push_event( + EventMessage(self.name, EventTypes.HLS_STDERROR_LINES, lines) + ) def cleanup(self): self.stop_ffmpeg() - self.push_event(EventMessage(self.name, Event.KILL_HLS_STREAM, None)) + self.push_event(EventMessage(self.name, EventTypes.KILL_HLS_STREAM, None)) diff --git a/sxm_player/workers/server.py b/sxm_player/workers/server.py index 8c6f060..4019b20 100644 --- a/sxm_player/workers/server.py +++ b/sxm_player/workers/server.py @@ -4,7 +4,7 @@ from aiohttp import web from sxm import QualitySize, RegionChoice, SXMClient, make_http_handler -from ..queue import Event, EventMessage +from ..queue import EventMessage, EventTypes from ..signals import TerminateInterrupt from .base import InterruptableWorker @@ -52,14 +52,14 @@ def _make_update_handler(self) -> Callable[[dict], None]: `SXMClient.get_playlist` when a HLS playlist updates""" def update_handler(data: dict) -> None: - self.push_event(EventMessage(self.name, Event.UPDATE_METADATA, data)) + self.push_event(EventMessage(self.name, EventTypes.UPDATE_METADATA, data)) return update_handler def send_channel_list(self): channels = self.sxm.get_channels() - self.push_event(EventMessage(self.name, Event.UPDATE_CHANNELS, channels)) + self.push_event(EventMessage(self.name, EventTypes.UPDATE_CHANNELS, channels)) def run(self) -> None: """Runs SXM proxy server""" diff --git a/sxm_player/workers/status.py b/sxm_player/workers/status.py index 66d09fb..874b366 100644 --- a/sxm_player/workers/status.py +++ b/sxm_player/workers/status.py @@ -1,6 +1,6 @@ import httpx -from ..queue import Event, EventMessage +from ..queue import EventMessage, EventTypes from .base import SXMLoopedWorker __all__ = ["StatusWorker"] @@ -40,11 +40,13 @@ def check_sxm(self): self._failures += 1 if self._failures > 3: self.push_event( - EventMessage(self.name, Event.RESET_SXM, "bad status check") + EventMessage( + self.name, EventTypes.RESET_SXM, "bad status check" + ) ) else: self._delay = 30.0 self._failures = 0 self.push_event( - EventMessage(self.name, Event.UPDATE_CHANNELS, r.json()) + EventMessage(self.name, EventTypes.UPDATE_CHANNELS, r.json()) )