diff --git a/README.md b/README.md index 8e5de5e..12d07db 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,8 @@ Once installed, you can use the tool by following these steps: ``` 3. The tool will process the YAML file and organize your music according to the specified configuration (see below). +Check out `tonuino-cards-manager --help` for all available options. + ### Demo [![asciicast](https://asciinema.org/a/663963.svg)](https://asciinema.org/a/663963) diff --git a/tonuino_cards_manager/_clean.py b/tonuino_cards_manager/_clean.py new file mode 100644 index 0000000..bd911d5 --- /dev/null +++ b/tonuino_cards_manager/_clean.py @@ -0,0 +1,31 @@ +# SPDX-FileCopyrightText: 2024 Max Mehl +# +# SPDX-License-Identifier: GPL-3.0-only + +"""Helper functions for file operations""" + +import logging +from pathlib import Path +from shutil import rmtree + +from ._card import Card +from ._helpers import get_directories_in_directory, proper_dirname + + +def clean_unconfigured_dirs(destination: str, cards: dict[int, Card]): + """Delete directories that are not configured as cards""" + dest = Path(destination) + # Calculate which directories are handled by the configuration + handled_dirs = [proper_dirname(card) for card in cards] + # For each existing directory on the SD card, check whether it is concerned + # by the configuration + for dirpath in get_directories_in_directory(dest): + if dirpath.name in ("mp3", "advert"): + continue + if dirpath.name not in handled_dirs: + logging.info( + "The directory %s exists on the SD card although it is not configured here. " + "Deleting it because you requested it with --force", + dirpath.name, + ) + rmtree(dirpath) diff --git a/tonuino_cards_manager/_helpers.py b/tonuino_cards_manager/_helpers.py index 781e14d..80d62ea 100644 --- a/tonuino_cards_manager/_helpers.py +++ b/tonuino_cards_manager/_helpers.py @@ -53,3 +53,8 @@ def decimal_to_hex(number: int | str) -> str: def get_files_in_directory(directory: Path) -> list[Path]: """Get all files in a directory, sorted""" return sorted([f for f in directory.iterdir() if f.is_file()]) + + +def get_directories_in_directory(directory: Path) -> list[Path]: + """Get all directories in a directory, sorted""" + return sorted([f for f in directory.iterdir() if f.is_dir()]) diff --git a/tonuino_cards_manager/main.py b/tonuino_cards_manager/main.py index cc56a5e..83c6afe 100644 --- a/tonuino_cards_manager/main.py +++ b/tonuino_cards_manager/main.py @@ -8,6 +8,7 @@ import logging from . import __version__ +from ._clean import clean_unconfigured_dirs from ._config import parse_config from ._qrcode import generate_qr_codes @@ -19,6 +20,12 @@ required=True, help="The destination directory in which the data is written to", ) +parser.add_argument( + "-f", + "--force", + action="store_true", + help="Delete all song folders on the destination which are not configured by you", +) parser.add_argument("-v", "--verbose", action="store_true", help="Verbose output") parser.add_argument("--version", action="version", version="%(prog)s " + __version__) @@ -73,6 +80,10 @@ def main(): qrdata.append(f"{card_bytecode};{card.create_carddesc(cardno)}") + # Delete directories that have not been configured + if args.force: + clean_unconfigured_dirs(args.destination, config.cards) + # Create QR code generate_qr_codes(qrdata)