Skip to content

Commit

Permalink
cli = fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Oct 20, 2023
1 parent c134e64 commit acd8b88
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
2 changes: 1 addition & 1 deletion rose/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# Add a logging handler for stdout unless we are testing. Pytest
# captures logging output on its own.
if True: # "pytest" not in sys.modules: # pragma: no cover
if "pytest" not in sys.modules: # pragma: no cover
stream_formatter = logging.Formatter(
"[%(asctime)s] %(levelname)s: %(message)s",
datefmt="%H:%M:%S",
Expand Down
13 changes: 12 additions & 1 deletion rose/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
edit_collage_in_editor,
)
from rose.config import Config
from rose.releases import dump_releases
from rose.releases import dump_releases, toggle_release_new
from rose.virtualfs import mount_virtualfs, unmount_virtualfs
from rose.watcher import start_watchdog

Expand Down Expand Up @@ -102,6 +102,17 @@ def print1(ctx: Context) -> None:
print(dump_releases(ctx.config))


@releases.command()
@click.argument("release", type=str, nargs=1)
@click.pass_obj
def toggle_new(ctx: Context, release: str) -> None:
"""
Toggle whether a release is new. Accepts a release's UUID or virtual fs dirname (both are
accepted).
"""
toggle_release_new(ctx.config, release)


@cli.group()
def collages() -> None:
"""Manage collages."""
Expand Down
24 changes: 14 additions & 10 deletions rose/releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,26 @@ def delete_release(c: Config, release_id_or_virtual_dirname: str) -> None:
update_cache_for_collages(c, None, force=True)


def set_release_new(c: Config, release_id_or_virtual_dirname: str, new: bool) -> None:
def toggle_release_new(c: Config, release_id_or_virtual_dirname: str) -> None:
release_id, release_dirname = resolve_release_ids(c, release_id_or_virtual_dirname)
source_path = get_release_source_path_from_id(c, release_id)
if source_path is None:
logger.debug(f"Failed to lookup source path for release {release_id} ({release_dirname})")
return None

for f in source_path.iterdir():
if STORED_DATA_FILE_REGEX.match(f.name):
with f.open("rb") as fp:
data = tomllib.load(fp)
data["new"] = new
with f.open("wb") as fp:
tomli_w.dump(data, fp)
else:
logger.critical(f"Failed to find .rose.toml in {source_path}")
update_cache_for_releases(c, [source_path], force=True)
if not STORED_DATA_FILE_REGEX.match(f.name):
continue

with f.open("rb") as fp:
data = tomllib.load(fp)
data["new"] = not data["new"]
with f.open("wb") as fp:
tomli_w.dump(data, fp)
update_cache_for_releases(c, [source_path], force=True)
return

logger.critical(f"Failed to find .rose.toml in {source_path}")


def resolve_release_ids(c: Config, release_id_or_virtual_dirname: str) -> tuple[str, str]:
Expand Down
29 changes: 29 additions & 0 deletions rose/releases_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import shutil

import pytest
import tomllib

from conftest import TEST_RELEASE_1
from rose.cache import connect, update_cache
Expand All @@ -10,6 +11,7 @@
delete_release,
dump_releases,
resolve_release_ids,
toggle_release_new,
)


Expand All @@ -30,6 +32,33 @@ def test_delete_release(config: Config) -> None:
assert cursor.fetchone()[0] == 0


def test_toggle_release_new(config: Config) -> None:
shutil.copytree(TEST_RELEASE_1, config.music_source_dir / TEST_RELEASE_1.name)
update_cache(config)
with connect(config) as conn:
cursor = conn.execute("SELECT id, virtual_dirname FROM releases")
release_id = cursor.fetchone()["id"]
datafile = config.music_source_dir / TEST_RELEASE_1.name / f".rose.{release_id}.toml"

# Set not new.
toggle_release_new(config, release_id)
with datafile.open("rb") as fp:
data = tomllib.load(fp)
assert data["new"] is False
with connect(config) as conn:
cursor = conn.execute("SELECT virtual_dirname FROM releases")
assert not cursor.fetchone()["virtual_dirname"].startswith("[NEW] ")

# Set new.
toggle_release_new(config, release_id)
with datafile.open("rb") as fp:
data = tomllib.load(fp)
assert data["new"] is True
with connect(config) as conn:
cursor = conn.execute("SELECT virtual_dirname FROM releases")
assert cursor.fetchone()["virtual_dirname"].startswith("[NEW] ")


def test_resolve_release_ids(config: Config) -> None:
shutil.copytree(TEST_RELEASE_1, config.music_source_dir / TEST_RELEASE_1.name)
update_cache(config)
Expand Down
8 changes: 3 additions & 5 deletions rose/virtualfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
)
from rose.common import sanitize_filename
from rose.config import Config
from rose.releases import ReleaseDoesNotExistError, delete_release, set_release_new
from rose.releases import ReleaseDoesNotExistError, delete_release, toggle_release_new

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -293,10 +293,8 @@ def rename(self, old: str, new: str) -> None:
and op.release.removeprefix("[NEW] ") == np.release.removeprefix("[NEW] ")
and (not op.file and not np.file)
):
old_new = op.release.startswith("[NEW] ")
new_new = np.release.startswith("[NEW] ")
if old_new != new_new:
set_release_new(self.config, op.release, new_new)
if op.release.startswith("[NEW] ") != np.release.startswith("[NEW] "):
toggle_release_new(self.config, op.release)
else:
raise fuse.FuseOSError(errno.EACCES)
elif op.view == "Collages" and np.view == "Collages":
Expand Down

0 comments on commit acd8b88

Please sign in to comment.