Skip to content

Commit

Permalink
Merge pull request #11 from mxmehl/force-cleanup
Browse files Browse the repository at this point in the history
Add --force option
  • Loading branch information
mxmehl authored Jun 14, 2024
2 parents 0a3959d + 86d65a0 commit 063587f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions tonuino_cards_manager/_clean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# SPDX-FileCopyrightText: 2024 Max Mehl <https://mehl.mx>
#
# 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)
5 changes: 5 additions & 0 deletions tonuino_cards_manager/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()])
11 changes: 11 additions & 0 deletions tonuino_cards_manager/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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__)

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 063587f

Please sign in to comment.