Skip to content

Commit

Permalink
(squash) easy & fast code fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
doronbehar committed Dec 13, 2023
1 parent 26e5ae7 commit 79271b2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
21 changes: 9 additions & 12 deletions beetsplug/importhistory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import re

Check failure on line 8 in beetsplug/importhistory.py

View workflow job for this annotation

GitHub Actions / lint

F401 're' imported but unused
from shutil import rmtree

from beets import library
from beets.plugins import BeetsPlugin
from beets.ui import colorize as colorize_text
from beets.ui import input_options, input_yn
from beets.util import displayable_path


class ImportHistPlugin(BeetsPlugin):
Expand Down Expand Up @@ -43,6 +45,7 @@ def import_stage(self, _, task):
def suggest_removal(self, item):
"""Prompts the user to delete the original path the item was imported from."""
if "source_path" not in item:
# TODO: Maybe switch to a less arbitrary choice of item fields?
try:
self._log.warn(
"Item without a source_path was found: {0.title} by {0.artist}",
Expand All @@ -59,17 +62,18 @@ def suggest_removal(self, item):
or item.mb_albumid in self.stop_suggestions_for_albums
):
return
# TODO: Consider whether it is even possible for this to happen
if os.path.isdir(item["source_path"]):
# We ask the user whether they'd like to delete the item's source
# directory
delete = input_yn(
"The item:\n{path}\nis originated in the directory:\n{source}\n"
"Would you like to delete the source directory of this item?".format(
path=colorize_text(
"text_warning", item.path.decode("utf-8")
"text_warning", displayable_path(item.path)
),
source=colorize_text(
"text_warning", item["source_path"].decode("utf-8")
"text_warning", displayable_path(item["source_path"])
),
),
require=True,
Expand Down Expand Up @@ -105,23 +109,16 @@ def suggest_removal(self, item):
)
if resp == "d":
self._log.info(
"Deleting the item's source file: %s", item["source_path"]
"Deleting the item's source file: {}", item["source_path"]
)
os.remove(item["source_path"])
elif resp == "r":
source_dir = os.path.dirname(item["source_path"])
self._log.info(
"Searching for other items with a source_path attr containing: %s",
"Searching for other items with a source_path attr containing: {}",
source_dir,
)
# NOTE: I'm not sure why, but we need to escape it twice
# otherwise the search fails
escaped_source_dir = re.escape(
re.escape(source_dir.decode("utf-8"))
)
source_dir_query = "source_path::^{}/*".format(
escaped_source_dir
)
source_dir_query = library.PathQuery("source_path", source_dir)
print(
"Doing so will delete the following items' sources as well:"
)
Expand Down
10 changes: 5 additions & 5 deletions docs/plugins/importhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ to every item imported to your library. It is useful when you import many
directories in a bulk, and you want to keep track of the directories you
already imported.

Another feature of the plugin, is suggesting the user to delete the source
Another feature of the plugin is suggesting the user to delete the source
paths of items you remove from your beets library. This is useful if you keep
backups of your imports in their original source paths by using `beet import
--copy`, you want to delete their backup when you regret the imports
alltogether.
--copy`, and you want to delete their backup when you regret the imports
altogether.

To use the ``importhistory`` plugin, enable it in your configuration (see
:ref:`using-plugins`).

`source_path` Usage
-------------------

The first use case of the `source_path` field, is in the following scenario: You
The first use case of the `source_path` field is in the following scenario: You
imported all of the directories in your current `$PWD`::

beet import --flat --copy */
Expand All @@ -37,7 +37,7 @@ You can of course pipe this command to other standard UNIX utilities::
sort -u

The above will print only the directories you successfully finished importing
with `beet import --flat --copy */`
with `beet import --flat --copy */`.

Removal Suggestion Usage
------------------------
Expand Down

0 comments on commit 79271b2

Please sign in to comment.