Skip to content

Commit

Permalink
Event -> EventTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
AngellusMortis committed Jul 17, 2021
1 parent 33978af commit ae346e0
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .bin/lib/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions .bin/update-requirements
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------

Expand Down
12 changes: 0 additions & 12 deletions MANIFEST.in

This file was deleted.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ include = [
"HISTORY.rst",
"LICENSE",
"README.rst",
"sxm/*",
"sxm_player/*",
"tests/*",
"docs/*.rst",
"docs/conf.py",
Expand Down
2 changes: 1 addition & 1 deletion sxm_player/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

__author__ = """Christopher Bailey"""
__email__ = "[email protected]"
__version__ = "0.2.0"
__version__ = "0.2.1"
4 changes: 2 additions & 2 deletions sxm_player/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
14 changes: 9 additions & 5 deletions sxm_player/debug/worker.py
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down Expand Up @@ -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))
20 changes: 10 additions & 10 deletions sxm_player/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions sxm_player/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,15 @@ 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()
for channel in self.channels:
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
Empty file added sxm_player/py.typed
Empty file.
4 changes: 2 additions & 2 deletions sxm_player/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
DEFAULT_POLLING_TIMEOUT = 0.02


class Event(Enum):
class EventTypes(Enum):
RESET_SXM = auto()
SXM_STATUS = auto()
UPDATE_CHANNELS = auto()
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions sxm_player/workers/archiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions sxm_player/workers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = [
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
14 changes: 7 additions & 7 deletions sxm_player/workers/cli.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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),
)
)
Expand All @@ -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:
Expand Down
10 changes: 6 additions & 4 deletions sxm_player/workers/hls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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),
)
)
Expand Down Expand Up @@ -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))
6 changes: 3 additions & 3 deletions sxm_player/workers/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"""
Expand Down
8 changes: 5 additions & 3 deletions sxm_player/workers/status.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import httpx

from ..queue import Event, EventMessage
from ..queue import EventMessage, EventTypes
from .base import SXMLoopedWorker

__all__ = ["StatusWorker"]
Expand Down Expand Up @@ -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())
)

0 comments on commit ae346e0

Please sign in to comment.