Skip to content

Commit

Permalink
removed music player
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienPensart committed Nov 11, 2023
1 parent ddad3f4 commit 9d2332b
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 195 deletions.
24 changes: 16 additions & 8 deletions musicbot/cli/music_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

logger = logging.getLogger(__name__)

FIELDS_NAMES = [field.name for field in fields(MusicFilter)]
JOINED_FIELDS_NAMES = ",".join(FIELDS_NAMES) # pylint: disable=not-an-iterable
FILTERS_REPRS = """\b
""" + "\n".join(
[f"{k}: {v.help_repr()}" for k, v in DEFAULT_PREFILTERS.items()]
)


def sane_music_filters(ctx: click.Context, param: click.Parameter, value: Any) -> list[MusicFilter]:
"""Convert a list of <key>=<value> to a dict"""
Expand Down Expand Up @@ -40,8 +47,15 @@ def sane_music_filters(ctx: click.Context, param: click.Parameter, value: Any) -
if len(split_val) != 2:
MusicbotObject.err(f"Error with a property, splitted value should be of length 2 : {split_val}")
raise click.Abort()
property_key = split_val[0]
property_value = split_val[1]

if property_key not in FIELDS_NAMES:
MusicbotObject.err(f"Error : unknown property {property_key} for value {property_value}")
raise click.Abort()

properties[property_key] = property_value

properties[split_val[0]] = split_val[1]
mf = MusicFilter(**properties)
and_filters.append(mf)

Expand All @@ -51,12 +65,6 @@ def sane_music_filters(ctx: click.Context, param: click.Parameter, value: Any) -
return ctx.params[param.name]


fields_names = ",".join([field.name for field in fields(MusicFilter)]) # pylint: disable=not-an-iterable
filters_reprs = """\b
""" + "\n".join(
[f"{k}: {v.help_repr()}" for k, v in DEFAULT_PREFILTERS.items()]
)

music_filters_options = add_options(
optgroup("Filter options"),
optgroup.option(
Expand All @@ -71,7 +79,7 @@ def sane_music_filters(ctx: click.Context, param: click.Parameter, value: Any) -
optgroup.option(
"--filter",
"music_filters",
help=f"Music filters (repeatable), fields: {fields_names}",
help=f"Music filters (repeatable), fields: {JOINED_FIELDS_NAMES}",
multiple=True,
callback=sane_music_filters,
),
Expand Down
77 changes: 41 additions & 36 deletions musicbot/commands/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import shutil
from dataclasses import asdict
from pathlib import Path
from typing import Any
from typing import TextIO

import click
import edgedb
import progressbar # type: ignore

# import progressbar # type: ignore
from beartype import beartype
from click_skeleton import AdvancedGroup
from rich.table import Table
Expand All @@ -25,7 +26,7 @@
syncify,
)
from musicbot.cli.file import flat_option
from musicbot.cli.music_filter import filters_reprs, music_filters_options
from musicbot.cli.music_filter import FILTERS_REPRS, music_filters_options
from musicbot.cli.musicdb import musicdb_options
from musicbot.cli.options import (
clean_option,
Expand All @@ -40,7 +41,8 @@
scan_folder_argument,
scan_folders_argument,
)
from musicbot.defaults import DEFAULT_VLC_PARAMS

# from musicbot.defaults import DEFAULT_VLC_PARAMS

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -225,7 +227,7 @@ def __call__(self, change: Change, path: str) -> bool:
pass


@cli.command(short_help="Generate a new playlist", help=filters_reprs)
@cli.command(short_help="Generate a new playlist", help=FILTERS_REPRS)
@musicdb_options
@output_option
@music_filters_options
Expand All @@ -238,8 +240,11 @@ async def playlist(
music_filters: list[MusicFilter],
playlist_options: PlaylistOptions,
musicdb: MusicDb,
out: Any,
out: click.utils.LazyFile,
) -> None:
if out.name.endswith(".m3u"):
output = "m3u"

musicdb.set_readonly()
new_playlist = await musicdb.make_playlist(
music_filters=frozenset(music_filters),
Expand Down Expand Up @@ -292,7 +297,7 @@ async def artists(
MusicbotObject.print_json([asdict(artist) for artist in all_artists])


@cli.command(short_help="Generate bests playlists with some rules", help=filters_reprs)
@cli.command(short_help="Generate bests playlists with some rules", help=FILTERS_REPRS)
@scan_folder_argument
@music_filters_options
@musicdb_options
Expand Down Expand Up @@ -334,35 +339,35 @@ async def bests(
MusicbotObject.success(f"Playlists: {len(bests)}")


@cli.command(aliases=["play"], short_help="Music player", help=filters_reprs)
@musicdb_options
@music_filters_options
@playlist_options
@click.option("--vlc-params", help="VLC params", default=DEFAULT_VLC_PARAMS, show_default=True)
@syncify
@beartype
async def player(
music_filters: list[MusicFilter],
musicdb: MusicDb,
vlc_params: str,
playlist_options: PlaylistOptions,
) -> None:
musicdb.set_readonly()
if not MusicbotObject.config.quiet or not MusicbotObject.is_test():
progressbar.streams.unwrap(stderr=True, stdout=True)
try:
new_playlist = await musicdb.make_playlist(
music_filters=frozenset(music_filters),
)
new_playlist.play(
vlc_params=vlc_params,
playlist_options=playlist_options,
)
except io.UnsupportedOperation:
logger.critical("Unable to load UI")


@cli.command(short_help="Copy selected musics with filters to destination folder", help=filters_reprs)
# @cli.command(aliases=["play"], short_help="Music player", help=FILTERS_REPRS)
# @musicdb_options
# @music_filters_options
# @playlist_options
# @click.option("--vlc-params", help="VLC params", default=DEFAULT_VLC_PARAMS, show_default=True)
# @syncify
# @beartype
# async def player(
# music_filters: list[MusicFilter],
# musicdb: MusicDb,
# vlc_params: str,
# playlist_options: PlaylistOptions,
# ) -> None:
# musicdb.set_readonly()
# if not MusicbotObject.config.quiet or not MusicbotObject.is_test():
# progressbar.streams.unwrap(stderr=True, stdout=True)
# try:
# new_playlist = await musicdb.make_playlist(
# music_filters=frozenset(music_filters),
# )
# new_playlist.play(
# vlc_params=vlc_params,
# playlist_options=playlist_options,
# )
# except io.UnsupportedOperation:
# logger.critical("Unable to load UI")


@cli.command(short_help="Copy selected musics with filters to destination folder", help=FILTERS_REPRS)
@destination_argument
@musicdb_options
@lazy_yes_option
Expand Down
2 changes: 1 addition & 1 deletion musicbot/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
MIN_INT: int = 0
MAX_INT: int = 2147483647

DEFAULT_VLC_PARAMS: str = "--vout=dummy --aout=pulse"
# DEFAULT_VLC_PARAMS: str = "--vout=dummy --aout=pulse"

KINDS_CHOICES: frozenset[str] = frozenset(["local", "remote", "remote-ssh", "local-ssh", "local-http", "remote-http", "all"])
DEFAULT_KINDS: frozenset[str] = frozenset(["local"])
Expand Down
25 changes: 17 additions & 8 deletions musicbot/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,31 @@ def effective_path(self, relative: bool = False) -> str:
def http_link(self, relative: bool = False) -> str:
return f"http://{self.ipv4}/{self.effective_path(relative)}"

def ssh_link(self) -> str:
def local_ssh_link(self) -> str:
return f"{self.username}@localhost:{self.path}"

def remote_ssh_link(self) -> str:
return f"{self.username}@{self.ipv4}:{self.path}"

def links(self, playlist_options: PlaylistOptions) -> frozenset[str]:
paths = []
if "all" in playlist_options.kinds:
return frozenset({self.ssh_link(), self.http_link(playlist_options.relative), self.effective_path(playlist_options.relative)})
return frozenset({self.local_ssh_link(), self.remote_ssh_link(), self.http_link(playlist_options.relative), self.effective_path(playlist_options.relative)})

# if "local-ssh" in playlist_options.kinds and self.ipv4 == self.public_ip():
if "local-ssh" in playlist_options.kinds:
paths.append(self.local_ssh_link())

if "local-ssh" in playlist_options.kinds and self.ipv4 == self.public_ip():
paths.append(self.ssh_link())
if "remote-ssh" in playlist_options.kinds and self.ipv4 != self.public_ip():
paths.append(self.ssh_link())
# if "remote-ssh" in playlist_options.kinds and self.ipv4 != self.public_ip():
if "remote-ssh" in playlist_options.kinds:
paths.append(self.remote_ssh_link())

if "local-http" in playlist_options.kinds and self.ipv4 == self.public_ip():
# if "local-http" in playlist_options.kinds and self.ipv4 == self.public_ip():
if "local-http" in playlist_options.kinds:
paths.append(self.http_link(playlist_options.relative))
if "remote-http" in playlist_options.kinds and self.ipv4 != self.public_ip():

# if "remote-http" in playlist_options.kinds and self.ipv4 != self.public_ip():
if "remote-http" in playlist_options.kinds:
paths.append(self.http_link(playlist_options.relative))

if "local" in playlist_options.kinds and os.path.isfile(self.path):
Expand Down
6 changes: 4 additions & 2 deletions musicbot/music.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import itertools
import logging
from dataclasses import asdict, dataclass
from typing import Any
Expand Down Expand Up @@ -41,7 +40,10 @@ def human_repr(self) -> str:

def links(self, playlist_options: PlaylistOptions | None = None) -> frozenset[str]:
playlist_options = playlist_options if playlist_options is not None else PlaylistOptions()
return frozenset(itertools.chain(*[folder.links(playlist_options) for folder in self.folders]))
folder_links = []
for folder in self.folders:
folder_links.extend(folder.links(playlist_options))
return frozenset(folder_links)

@property
def slug(self) -> str:
Expand Down
Loading

0 comments on commit 9d2332b

Please sign in to comment.