From 5d3d936b80e23f0597a3c256797407832e9a44fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Fri, 1 Nov 2024 19:04:05 +0100 Subject: [PATCH] feat: added apy backup refer: #102 --- src/apyanki/cli.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/apyanki/cli.py b/src/apyanki/cli.py index 5104790..cbd88ee 100644 --- a/src/apyanki/cli.py +++ b/src/apyanki/cli.py @@ -12,6 +12,7 @@ from apyanki.config import cfg, cfg_file from apyanki.console import console from apyanki.note import Note +from apyanki.utilities import suppress_stdout CONTEXT_SETTINGS = {"help_option_names": ["-h", "--help"]} @@ -529,6 +530,38 @@ def reposition(position: int, query: str) -> None: a.modified = True +@main.command() +@click.argument( + "target-file", type=click.Path(exists=False, resolve_path=True, path_type=Path) +) +@click.option( + "-m", "--include-media", is_flag=True, help="Include media files in backup." +) +@click.option( + "-l", + "--legacy", + is_flag=True, + help="Support older Anki versions (slower/larger files)", +) +def backup(target_file: Path, include_media: bool, legacy: bool) -> None: + """Backup Anki database to specified target file.""" + with Anki(**cfg) as a: + target_filename = str(target_file) + + if not target_filename.endswith(".colpkg"): + console.print("[yellow]Warning: Target should have .colpkg extension!") + raise click.Abort() + + if target_file.exists(): + console.print("[yellow]Warning: Target file already exists!") + console.print(f"[yellow] {target_file}") + if not console.confirm("Do you want to overwrite it?"): + raise click.Abort() + + with suppress_stdout(): + a.col.export_collection_package(target_filename, include_media, legacy) + + if __name__ == "__main__": # pylint: disable=no-value-for-parameter main()