Skip to content

Commit

Permalink
push sanitize_filename into cache.py
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Oct 24, 2023
1 parent ff1b810 commit f8564f2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
26 changes: 16 additions & 10 deletions rose/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import uuid6

from rose.artiststr import format_artist_string
from rose.common import sanitize_filename
from rose.config import Config
from rose.tagger import AudioFile

Expand Down Expand Up @@ -1247,25 +1246,25 @@ def get_release_source_path_from_id(c: Config, uuid: str) -> Path | None:
return None


def list_artists(c: Config) -> Iterator[str]:
def list_artists(c: Config) -> Iterator[tuple[str, str]]:
with connect(c) as conn:
cursor = conn.execute("SELECT DISTINCT artist FROM releases_artists")
cursor = conn.execute("SELECT DISTINCT artist, artist_sanitized FROM releases_artists")
for row in cursor:
yield row["artist"]
yield row["artist"], row["artist_sanitized"]


def list_genres(c: Config) -> Iterator[str]:
def list_genres(c: Config) -> Iterator[tuple[str, str]]:
with connect(c) as conn:
cursor = conn.execute("SELECT DISTINCT genre FROM releases_genres")
cursor = conn.execute("SELECT DISTINCT genre, genre_sanitized FROM releases_genres")
for row in cursor:
yield row["genre"]
yield row["genre"], row["genre_sanitized"]


def list_labels(c: Config) -> Iterator[str]:
def list_labels(c: Config) -> Iterator[tuple[str, str]]:
with connect(c) as conn:
cursor = conn.execute("SELECT DISTINCT label FROM releases_labels")
cursor = conn.execute("SELECT DISTINCT label, label_sanitized FROM releases_labels")
for row in cursor:
yield row["label"]
yield row["label"], row["label_sanitized"]


def list_collages(c: Config) -> Iterator[str]:
Expand Down Expand Up @@ -1388,3 +1387,10 @@ def collage_has_release(c: Config, collage_name: str, release_virtual_dirname: s
(collage_name, release_virtual_dirname),
)
return bool(cursor.fetchone()[0])


ILLEGAL_FS_CHARS_REGEX = re.compile(r'[:\?<>\\*\|"\/]+')


def sanitize_filename(x: str) -> str:
return ILLEGAL_FS_CHARS_REGEX.sub("_", x)
15 changes: 12 additions & 3 deletions rose/cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,19 +568,28 @@ def test_get_release_source_path_dirname_from_id(config: Config) -> None:
@pytest.mark.usefixtures("seeded_cache")
def test_list_artists(config: Config) -> None:
artists = list(list_artists(config))
assert set(artists) == {"Techno Man", "Bass Man", "Violin Woman", "Conductor Woman"}
assert set(artists) == {
("Techno Man", "Techno Man"),
("Bass Man", "Bass Man"),
("Violin Woman", "Violin Woman"),
("Conductor Woman", "Conductor Woman"),
}


@pytest.mark.usefixtures("seeded_cache")
def test_list_genres(config: Config) -> None:
genres = list(list_genres(config))
assert set(genres) == {"Techno", "Deep House", "Classical"}
assert set(genres) == {
("Techno", "Techno"),
("Deep House", "Deep House"),
("Classical", "Classical"),
}


@pytest.mark.usefixtures("seeded_cache")
def test_list_labels(config: Config) -> None:
labels = list(list_labels(config))
assert set(labels) == {"Silk Music", "Native State"}
assert set(labels) == {("Silk Music", "Silk Music"), ("Native State", "Native State")}


@pytest.mark.usefixtures("seeded_cache")
Expand Down
8 changes: 0 additions & 8 deletions rose/common.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import re
import uuid


class RoseError(Exception):
pass


ILLEGAL_FS_CHARS_REGEX = re.compile(r'[:\?<>\\*\|"\/]+')


def sanitize_filename(x: str) -> str:
return ILLEGAL_FS_CHARS_REGEX.sub("_", x)


def valid_uuid(x: str) -> bool:
try:
uuid.UUID(x)
Expand Down
19 changes: 9 additions & 10 deletions rose/virtualfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
delete_release_from_collage,
rename_collage,
)
from rose.common import sanitize_filename
from rose.config import Config
from rose.releases import ReleaseDoesNotExistError, delete_release, toggle_release_new

Expand Down Expand Up @@ -166,23 +165,23 @@ def readdir(self, path: str, _: int) -> Iterator[str]:
("dir", release.source_path),
)
elif p.view == "Artists":
for artist in list_artists(self.config):
for artist, sanitized_artist in list_artists(self.config):
if artist in self.hide_artists_set:
continue
yield sanitize_filename(artist)
self.getattr_cache[path + "/" + artist] = (time.time(), ("dir",))
yield sanitized_artist
self.getattr_cache[path + "/" + sanitized_artist] = (time.time(), ("dir",))
elif p.view == "Genres":
for genre in list_genres(self.config):
for genre, sanitized_genre in list_genres(self.config):
if genre in self.hide_genres_set:
continue
yield sanitize_filename(genre)
self.getattr_cache[path + "/" + genre] = (time.time(), ("dir",))
yield sanitized_genre
self.getattr_cache[path + "/" + sanitized_genre] = (time.time(), ("dir",))
elif p.view == "Labels":
for label in list_labels(self.config):
for label, sanitized_label in list_labels(self.config):
if label in self.hide_labels_set:
continue
yield sanitize_filename(label)
self.getattr_cache[path + "/" + label] = (time.time(), ("dir",))
yield sanitized_label
self.getattr_cache[path + "/" + sanitized_label] = (time.time(), ("dir",))
elif p.view == "Collages" and p.collage:
releases = list(list_collage_releases(self.config, p.collage))
pad_size = max(len(str(r[0])) for r in releases)
Expand Down

0 comments on commit f8564f2

Please sign in to comment.