Skip to content

Commit

Permalink
support adding releases to collages from 3. releases - recently added…
Browse files Browse the repository at this point in the history
… and 7. other collages views

requires prefix stripping some regexes
  • Loading branch information
azuline committed Oct 29, 2023
1 parent 1f95195 commit 11653d3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
7 changes: 7 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,19 @@ def seeded_cache(config: Config) -> None:
""" # noqa: E501
)

(config.music_source_dir / "!collages").mkdir()
(config.music_source_dir / "!playlists").mkdir()

for d in dirpaths:
d.parent.mkdir(parents=True, exist_ok=True)
d.mkdir()
for f in musicpaths + imagepaths:
f.parent.mkdir(parents=True, exist_ok=True)
f.touch()
for cn in ["Rose Gold", "Ruby Red"]:
(config.music_source_dir / "!collages" / f"{cn}.toml").touch()
for pn in ["Lala Lisa", "Turtle Rabbit"]:
(config.music_source_dir / "!playlists" / f"{pn}.toml").touch()


@pytest.fixture()
Expand Down
45 changes: 37 additions & 8 deletions rose/virtualfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,20 @@ def next(self) -> int:
return self._state


# These are tight regexes for the prefixes that may be applied to releases in the virtual
# filesystem. When we want to use a release name that originated from another view (e.g. because of
# a `cp`) command, we need these regexes in order to support names that originated from views like
# Recently Added or Collages.
#
# And if these happen to match an artist name... they're probably not worth listening to anyways
# lol. Probably some vaporwave bullshit.
RELEASE_PREFIX_STRIPPERS = [
re.compile(r"^"),
re.compile(r"^\[\d{4}-\d{2}-\d{2}\] "),
re.compile(r"^\d+\. "),
]


class RoseLogicalFS:
def __init__(self, config: Config, fhgen: FileHandleGenerator):
self.config = config
Expand Down Expand Up @@ -623,14 +637,25 @@ def mkdir(self, p: VirtualPath) -> None:
create_collage(self.config, p.collage)
return
if p.collage and p.release:
try:
add_release_to_collage(self.config, p.collage, p.release)
return
except ReleaseDoesNotExistError as e:
logger.debug(
f"Failed adding release {p.release} to collage {p.collage}: release not found"
)
raise llfuse.FUSEError(errno.ENOENT) from e
err = None
# Because some releases have prefixes, attempt multiple times with each different prefix
# stripper.
seen: set[str] = set()
for prefix_stripper in RELEASE_PREFIX_STRIPPERS:
rls = prefix_stripper.sub("", p.release)
# But don't waste effort if nothing changed.
if rls in seen:
continue
seen.add(rls)
try:
add_release_to_collage(self.config, p.collage, rls)
return
except ReleaseDoesNotExistError as e:
err = e
logger.debug(
f"Failed adding release {p.release} to collage {p.collage}: release not found"
)
raise llfuse.FUSEError(errno.ENOENT) from err
if p.playlist and p.file is None:
create_playlist(self.config, p.playlist)
return
Expand Down Expand Up @@ -741,6 +766,10 @@ def open(self, p: VirtualPath, flags: int) -> int:

def read(self, fh: int, offset: int, length: int) -> bytes:
logger.debug(f"LOGICAL: Received read for {fh=} {offset=} {length=}")
if pap := self.playlist_additions_in_progress.get(fh, None):
logger.debug("Matched read to an in-progress playlist addition")
_, _, b = pap
return b[offset : offset + length]
os.lseek(fh, offset, os.SEEK_SET)
return os.read(fh, length)

Expand Down
18 changes: 18 additions & 0 deletions rose/virtualfs_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import shutil
import subprocess
import time
from collections.abc import Iterator
Expand Down Expand Up @@ -157,6 +158,23 @@ def test_virtual_filesystem_collage_actions(config: Config) -> None:
assert not (src / "!collages" / "New Jeans.toml").exists()


@pytest.mark.usefixtures("seeded_cache")
def test_virtual_filesystem_add_collage_release_prefix_stripping(config: Config) -> None:
"""Test that we can add a release from the esoteric views to a collage, regardless of prefix."""
root = config.fuse_mount_dir

dirs = [
root / "1. Releases" / "r1",
root / "3. Releases - Recently Added" / "[0000-00-01] r1",
root / "7. Collages" / "Rose Gold" / "1. r1",
]

with start_virtual_fs(config):
for d in dirs:
shutil.copytree(d, root / "7. Collages" / "Ruby Red" / "r1")
(root / "7. Collages" / "Ruby Red" / "r1").rmdir()


def test_virtual_filesystem_playlist_actions(
config: Config,
source_dir: Path, # noqa: ARG001
Expand Down

0 comments on commit 11653d3

Please sign in to comment.