Skip to content

Bulk asynchronous downloader for multiple file hosts

feat: add config subcommand

NTFSvolume3c4601af1676
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0a762840f..4092337e5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New `--database-file` option
- New `--cache-file` option
- New `download` subcommand (replaces the `--download` argument)
+- New `config` subcommand
- New `interactive` subcommand
- New `hash` subcommand
- New `report` subcommand
diff --git a/cyberdrop_dl/cli.py b/cyberdrop_dl/cli.py
index ca7375efe..2af7a8596 100644
--- a/cyberdrop_dl/cli.py
+++ b/cyberdrop_dl/cli.py
@@ -4,6 +4,7 @@ from cyclopts.help import DefaultFormatter
from cyberdrop_dl import __version__
from cyberdrop_dl.commands.clean_up import app as cleanup
+from cyberdrop_dl.commands.config import app as config
from cyberdrop_dl.commands.database import app as database
from cyberdrop_dl.commands.hash import compute_hashes
from cyberdrop_dl.commands.report import report
@@ -28,7 +29,7 @@ def show() -> None:
app.console.print(table)
-for cmd in download, database, interactive, cleanup, report:
+for cmd in download, database, interactive, cleanup, report, config:
app.command(cmd)
diff --git a/cyberdrop_dl/commands/config.py b/cyberdrop_dl/commands/config.py
new file mode 100644
index 000000000..d70685fc8
--- /dev/null
+++ b/cyberdrop_dl/commands/config.py
@@ -0,0 +1,36 @@
+import logging
+
+from cyclopts import App
+
+from cyberdrop_dl.config import Config
+from cyberdrop_dl.config.appdata import AppData
+from cyberdrop_dl.prompts import ask_should_create_config
+
+app = App(name="config", help="Config file operations")
+logger = logging.getLogger(__name__)
+
+
+def file() -> None:
+ "Show the default config file path"
+ app.console.print(AppData.default().config_file)
+
+
+def edit() -> None:
+ "Open the default config file on a text editor"
+ from cyberdrop_dl.utils import text_editor
+
+ file = AppData.default().config_file
+ if not file.exists():
+ if not ask_should_create_config(file):
+ return
+ Config().save_to(file)
+
+ text_editor.open(file)
+
+
+def new() -> None:
+ "Create a new config with default options and print it"
+ app.console.print(Config().dump_yaml())
diff --git a/cyberdrop_dl/config/__init__.py b/cyberdrop_dl/config/__init__.py
index 4cf39e932..d1e35f8ff 100644
--- a/cyberdrop_dl/config/__init__.py
+++ b/cyberdrop_dl/config/__init__.py
@@ -118,10 +118,12 @@ class Config(ConfigModel, title="cyberdrop-dl config"):
def source(self) -> Path | None:
return self._source
+ def dump_yaml(self) -> str:
+ return yaml.safe_dump(self.model_dump(mode="json"), default_flow_style=False)
+
def save_to(self, file: Path) -> None:
file.parent.mkdir(parents=True, exist_ok=True)
- with file.open("w", encoding="utf8") as file_io:
- yaml.safe_dump(self.model_dump(mode="json"), file_io, default_flow_style=False)
+ file.write_text(self.dump_yaml(), encoding="utf8")
@staticmethod
def from_file(file: Path, *, _save_if_not_found: bool = False) -> Config:
diff --git a/cyberdrop_dl/program_ui.py b/cyberdrop_dl/program_ui.py
index 2a39d927c..a68104321 100644
--- a/cyberdrop_dl/program_ui.py
+++ b/cyberdrop_dl/program_ui.py
@@ -9,13 +9,19 @@ from rich.markdown import Markdown
from cyberdrop_dl import __version__, aio, stats
from cyberdrop_dl.hasher import Hasher, hash_directory
from cyberdrop_dl.progress import hyperlink
-from cyberdrop_dl.prompts import ask_choices, ask_confirmation, ask_dir, console, enter_to_continue
+from cyberdrop_dl.prompts import (
+ ask_choices,
+ ask_confirmation,
+ ask_dir,
+ ask_should_create_config,
+ console,
+ enter_to_continue,
+)
from cyberdrop_dl.sorter import Sorter
from cyberdrop_dl.utils import text_editor
if TYPE_CHECKING:
from collections.abc import Callable
- from pathlib import Path
from cyberdrop_dl.manager import Manager
@@ -69,16 +75,11 @@ def _sort_files(manager: Manager) -> None:
enter_to_continue()
-def _should_create_config(file: Path) -> bool:
- console.warning("A default config file does not exists")
- return ask_confirmation(f"Do you want to create it at '{file}'?")
-
-
def _edit_config(manager: Manager) -> None:
file = manager.config.source
if file is None:
file = manager.appdata.config_file
- if not _should_create_config(file):
+ if not ask_should_create_config(file):
return
type(manager.config)().save_to(file)
diff --git a/cyberdrop_dl/prompts.py b/cyberdrop_dl/prompts.py
index c83476e40..77f783ae4 100644
--- a/cyberdrop_dl/prompts.py
+++ b/cyberdrop_dl/prompts.py
@@ -115,3 +115,8 @@ def ask_path(
console.error(repr(e))
else:
return path.resolve()
+
+
+def ask_should_create_config(file: Path) -> bool:
+ console.warning("A default config file does not exists")
+ return ask_confirmation(f"Do you want to create it at '{file}'?")