You've already forked RadioPlayer
mirror of
https://github.com/radio95-rnt/RadioPlayer.git
synced 2026-02-26 13:52:00 +01:00
modularize playlist parser
This commit is contained in:
@@ -5,6 +5,8 @@ from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
import tinytag
|
||||
|
||||
_log_out: log95.TextIO
|
||||
|
||||
@dataclass
|
||||
class Track:
|
||||
path: Path
|
||||
@@ -164,4 +166,17 @@ class InterModuleCommunication:
|
||||
Sends the data to a named module, and return its response
|
||||
"""
|
||||
if not name in self.names_modules.keys(): raise ModuleNotFoundError("No such module")
|
||||
return self.names_modules[name].imc_data(source, next((k for k, v in self.names_modules.items() if v is source), None), data, False)
|
||||
return self.names_modules[name].imc_data(source, next((k for k, v in self.names_modules.items() if v is source), None), data, False)
|
||||
|
||||
class PlaylistParser:
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
def parse(self, playlist_path: Path) -> tuple[dict[str, str], list[tuple[list[str], dict[str, str]]]]:
|
||||
"""
|
||||
This should return the following information:
|
||||
global arguments,
|
||||
list of entries:
|
||||
a entry is just a tuple of a list of strings (file paths)
|
||||
and a dictionary of str:str consistent of the arguments which affect the files given
|
||||
"""
|
||||
return {}, []
|
||||
@@ -167,4 +167,29 @@ class Module(ActiveModifier):
|
||||
if data.get("set", True): self.skip_next = not self.skip_next
|
||||
return {"status": "ok", "data": self.skip_next}
|
||||
|
||||
activemod = Module()
|
||||
activemod = Module()
|
||||
|
||||
# This is free and unencumbered software released into the public domain.
|
||||
|
||||
# Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
# distribute this software, either in source code form or as a compiled
|
||||
# binary, for any purpose, commercial or non-commercial, and by any
|
||||
# means.
|
||||
|
||||
# In jurisdictions that recognize copyright laws, the author or authors
|
||||
# of this software dedicate any and all copyright interest in the
|
||||
# software to the public domain. We make this dedication for the benefit
|
||||
# of the public at large and to the detriment of our heirs and
|
||||
# successors. We intend this dedication to be an overt act of
|
||||
# relinquishment in perpetuity of all present and future rights to this
|
||||
# software under copyright law.
|
||||
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
# OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
# For more information, please refer to <https://unlicense.org>
|
||||
@@ -127,4 +127,29 @@ class Module(PlaylistAdvisor):
|
||||
def imc_data(self, source: BaseIMCModule, source_name: str | None, data: object, broadcast: bool):
|
||||
return (self.custom_playlist, MORNING_START, DAY_END)
|
||||
|
||||
advisor = Module()
|
||||
advisor = Module()
|
||||
|
||||
# This is free and unencumbered software released into the public domain.
|
||||
|
||||
# Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
# distribute this software, either in source code form or as a compiled
|
||||
# binary, for any purpose, commercial or non-commercial, and by any
|
||||
# means.
|
||||
|
||||
# In jurisdictions that recognize copyright laws, the author or authors
|
||||
# of this software dedicate any and all copyright interest in the
|
||||
# software to the public domain. We make this dedication for the benefit
|
||||
# of the public at large and to the detriment of our heirs and
|
||||
# successors. We intend this dedication to be an overt act of
|
||||
# relinquishment in perpetuity of all present and future rights to this
|
||||
# software under copyright law.
|
||||
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
# OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
# For more information, please refer to <https://unlicense.org>
|
||||
56
modules/playlist_parser.py
Normal file
56
modules/playlist_parser.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import glob
|
||||
from . import log95, _log_out, Path
|
||||
|
||||
class PlaintextParser:
|
||||
def __init__(self): self.logger = log95.log95("PARSER", output=_log_out)
|
||||
|
||||
def _check_for_imports(self, path: Path, seen=None) -> list[str]:
|
||||
if seen is None: seen = set()
|
||||
if not path.exists():
|
||||
self.logger.error(f"Playlist not found: {path.name}")
|
||||
raise Exception("Playlist doesn't exist")
|
||||
lines = [line.strip() for line in path.read_text().splitlines() if line.strip()]
|
||||
|
||||
out = []
|
||||
for line in lines:
|
||||
if line.startswith("@"):
|
||||
target = Path(line.removeprefix("@"))
|
||||
if target not in seen:
|
||||
if not target.exists():
|
||||
self.logger.error(f"Target {target.name} of {path.name} does not exist")
|
||||
continue
|
||||
seen.add(target)
|
||||
out.extend(self._check_for_imports(target, seen))
|
||||
else: out.append(line)
|
||||
return out
|
||||
|
||||
def parse(self, playlist_path: Path) -> tuple[dict[str, str], list[tuple[list[str], dict[str, str]]]]:
|
||||
lines = self._check_for_imports(playlist_path)
|
||||
out = []
|
||||
global_arguments = {}
|
||||
for line in lines:
|
||||
arguments = {}
|
||||
line = line.strip()
|
||||
if not line or line.startswith(";") or line.startswith("#"): continue
|
||||
if "|" in line:
|
||||
if line.startswith("|"): # No file name, we're defining global arguments
|
||||
args = line.removeprefix("|").split(";")
|
||||
for arg in args:
|
||||
if "=" in arg:
|
||||
key, val = arg.split("=", 1)
|
||||
arguments[key] = val
|
||||
else:
|
||||
arguments[arg] = True
|
||||
else:
|
||||
line, args = line.split("|", 1)
|
||||
args = args.split(";")
|
||||
for arg in args:
|
||||
if "=" in arg:
|
||||
key, val = arg.split("=", 1)
|
||||
arguments[key] = val
|
||||
else:
|
||||
arguments[arg] = True
|
||||
out.append(([f for f in glob.glob(line) if Path(f).is_file()], arguments))
|
||||
return global_arguments, out
|
||||
|
||||
parser = PlaintextParser()
|
||||
@@ -1,5 +1,5 @@
|
||||
from . import PlayerModule, log95, Track
|
||||
import socket, re
|
||||
from . import PlayerModule, _log_out, log95, Track
|
||||
import socket
|
||||
|
||||
DEBUG = False
|
||||
|
||||
@@ -10,9 +10,6 @@ rds_default_artist = "radio95"
|
||||
|
||||
udp_host = ("127.0.0.1", 5000)
|
||||
|
||||
from typing import TextIO
|
||||
_log_out: TextIO
|
||||
|
||||
logger_level = log95.log95Levels.DEBUG if DEBUG else log95.log95Levels.CRITICAL_ERROR
|
||||
assert _log_out # pyright: ignore[reportUnboundVariable]
|
||||
logger = log95.log95("RDS-MODULE", logger_level, output=_log_out)
|
||||
|
||||
@@ -7,4 +7,29 @@ class Module(PlaylistModifierModule):
|
||||
if int(global_args.get("no_shuffle", 0)) == 0: random.shuffle(playlist)
|
||||
return playlist
|
||||
|
||||
playlistmod = (Module(), 0)
|
||||
playlistmod = (Module(), 0)
|
||||
|
||||
# This is free and unencumbered software released into the public domain.
|
||||
|
||||
# Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
# distribute this software, either in source code form or as a compiled
|
||||
# binary, for any purpose, commercial or non-commercial, and by any
|
||||
# means.
|
||||
|
||||
# In jurisdictions that recognize copyright laws, the author or authors
|
||||
# of this software dedicate any and all copyright interest in the
|
||||
# software to the public domain. We make this dedication for the benefit
|
||||
# of the public at large and to the detriment of our heirs and
|
||||
# successors. We intend this dedication to be an overt act of
|
||||
# relinquishment in perpetuity of all present and future rights to this
|
||||
# software under copyright law.
|
||||
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
# OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
# For more information, please refer to <https://unlicense.org>
|
||||
@@ -1,9 +1,6 @@
|
||||
from . import PlayerModule, log95, Track
|
||||
from . import PlayerModule, log95, Track, _log_out
|
||||
import os
|
||||
|
||||
from typing import TextIO
|
||||
_log_out: TextIO
|
||||
|
||||
assert _log_out # pyright: ignore[reportUnboundVariable]
|
||||
logger = log95.log95("Skipper", output=_log_out)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user