From 759fc3434ffb695a5041552f7a553cf9eb2a1f8d Mon Sep 17 00:00:00 2001 From: Nathaniel Landau Date: Sun, 22 Jan 2023 19:53:23 +0000 Subject: [PATCH] fix(application): exit after committing changes --- src/obsidian_metadata/models/application.py | 23 ++++++++++++--------- src/obsidian_metadata/models/notes.py | 5 ++++- src/obsidian_metadata/models/vault.py | 17 +++------------ tests/vault_test.py | 1 - 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/obsidian_metadata/models/application.py b/src/obsidian_metadata/models/application.py index b13746e..52bf715 100644 --- a/src/obsidian_metadata/models/application.py +++ b/src/obsidian_metadata/models/application.py @@ -4,7 +4,6 @@ from typing import Any import questionary -import typer from rich import print from obsidian_metadata._config import Config @@ -132,8 +131,8 @@ class Application: if operation == "review_changes": self.review_changes() - if operation == "commit_changes": - self.commit_changes() + if operation == "commit_changes" and self.commit_changes(): + break if operation == "abort": break @@ -346,25 +345,29 @@ class Application: break changed_notes[note_to_review].print_diff() - def commit_changes(self) -> None: - """Write all changes to disk.""" + def commit_changes(self) -> bool: + """Write all changes to disk. + + Returns: + True if changes were committed, False otherwise. + """ changed_notes = self.vault.get_changed_notes() if len(changed_notes) == 0: print("\n") alerts.notice("No changes to commit.\n") - return + return False backup = questionary.confirm("Create backup before committing changes").ask() if backup is None: - return + return False if backup: self.vault.backup() if questionary.confirm(f"Commit {len(changed_notes)} changed files to disk?").ask(): self.vault.write() - alerts.success("Changes committed to disk. Exiting.") - typer.Exit() + alerts.success(f"{len(changed_notes)} changes committed to disk. Exiting") + return True - return + return False diff --git a/src/obsidian_metadata/models/notes.py b/src/obsidian_metadata/models/notes.py index 7af29ac..bee68e9 100644 --- a/src/obsidian_metadata/models/notes.py +++ b/src/obsidian_metadata/models/notes.py @@ -350,11 +350,14 @@ class Note: self.sub(current_frontmatter, new_frontmatter) - def write(self, path: Path | None = None) -> None: + def write(self, path: Path = None) -> None: """Writes the note's content to disk. Args: path (Path): Path to write the note to. Defaults to the note's path. + + Raises: + typer.Exit: If the note's path is not found. """ p = self.note_path if path is None else path diff --git a/src/obsidian_metadata/models/vault.py b/src/obsidian_metadata/models/vault.py index 5f3236c..b951e5b 100644 --- a/src/obsidian_metadata/models/vault.py +++ b/src/obsidian_metadata/models/vault.py @@ -24,7 +24,6 @@ class Vault: vault (Path): Path to the vault. dry_run (bool): Whether to perform a dry run. backup_path (Path): Path to the backup of the vault. - new_vault (Path): Path to a new vault. notes (list[Note]): List of all notes in the vault. """ @@ -32,7 +31,6 @@ class Vault: self.vault_path: Path = config.vault_path self.dry_run: bool = dry_run self.backup_path: Path = self.vault_path.parent / f"{self.vault_path.name}.bak" - self.new_vault_path: Path = self.vault_path.parent / f"{self.vault_path.name}.new" self.exclude_paths: list[Path] = [] self.metadata = VaultMetadata() for p in config.exclude_paths: @@ -60,7 +58,6 @@ class Vault: yield "vault_path", self.vault_path yield "dry_run", self.dry_run yield "backup_path", self.backup_path - yield "new_vault", self.new_vault_path yield "num_notes", self.num_notes() yield "exclude_paths", self.exclude_paths @@ -285,18 +282,10 @@ class Vault: return True return False - def write(self, new_vault: bool = False) -> None: + def write(self) -> None: """Write changes to the vault.""" log.debug("Writing changes to vault...") - if new_vault: - log.debug("Writing changes to backup") + if self.dry_run is False: for _note in self.notes: - _new_note_path: Path = Path( - self.new_vault_path / Path(_note.note_path).relative_to(self.vault_path) - ) - log.debug(f"writing to {_new_note_path}") - _note.write(path=_new_note_path) - else: - for _note in self.notes: - log.debug(f"writing to {_note.note_path}") + log.trace(f"writing to {_note.note_path}") _note.write() diff --git a/tests/vault_test.py b/tests/vault_test.py index 17398ce..461a0a4 100644 --- a/tests/vault_test.py +++ b/tests/vault_test.py @@ -16,7 +16,6 @@ def test_vault_creation(test_vault): assert vault.vault_path == vault_path assert vault.backup_path == Path(f"{vault_path}.bak") - assert vault.new_vault_path == Path(f"{vault_path}.new") assert vault.dry_run is False assert str(vault.exclude_paths[0]) == Regex(r".*\.git") assert vault.num_notes() == 2