From f8564f2ec626a6afcdbc1f7089ad3606599a8e0c Mon Sep 17 00:00:00 2001 From: blissful Date: Mon, 23 Oct 2023 23:22:09 -0400 Subject: [PATCH] push sanitize_filename into cache.py --- rose/cache.py | 26 ++++++++++++++++---------- rose/cache_test.py | 15 ++++++++++++--- rose/common.py | 8 -------- rose/virtualfs.py | 19 +++++++++---------- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/rose/cache.py b/rose/cache.py index 04f1955..66517e4 100644 --- a/rose/cache.py +++ b/rose/cache.py @@ -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 @@ -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]: @@ -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) diff --git a/rose/cache_test.py b/rose/cache_test.py index 1a6052f..47875a3 100644 --- a/rose/cache_test.py +++ b/rose/cache_test.py @@ -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") diff --git a/rose/common.py b/rose/common.py index 47988b6..d972cc1 100644 --- a/rose/common.py +++ b/rose/common.py @@ -1,4 +1,3 @@ -import re import uuid @@ -6,13 +5,6 @@ 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) diff --git a/rose/virtualfs.py b/rose/virtualfs.py index 7ef946c..4651878 100644 --- a/rose/virtualfs.py +++ b/rose/virtualfs.py @@ -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 @@ -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)