| 1 |
""" |
| 2 |
List all files in the library folder which are not listed in the |
| 3 |
beets library database, including art files |
| 4 |
""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import os |
| 9 |
from typing import TYPE_CHECKING |
| 10 |
|
| 11 |
from beets import util |
| 12 |
from beets.plugins import BeetsPlugin |
| 13 |
from beets.ui import Subcommand, print_ |
| 14 |
|
| 15 |
if TYPE_CHECKING: |
| 16 |
import optparse |
| 17 |
|
| 18 |
from beets.library import Library |
| 19 |
|
| 20 |
|
| 21 |
__author__ = "https://github.com/MrNuggelz" |
| 22 |
|
| 23 |
|
| 24 |
class Unimported(BeetsPlugin): |
| 25 |
def __init__(self) -> None: |
| 26 |
super().__init__() |
| 27 |
self.config.add({"ignore_extensions": [], "ignore_subdirectories": []}) |
| 28 |
|
| 29 |
def commands(self) -> list[Subcommand]: |
| 30 |
def print_unimported( |
| 31 |
lib: Library, opts: optparse.Values, args: list[str] |
| 32 |
) -> None: |
| 33 |
ignore_exts = [ |
| 34 |
f".{x}".encode() |
| 35 |
for x in self.config["ignore_extensions"].as_str_seq() |
| 36 |
] |
| 37 |
ignore_dirs = [ |
| 38 |
os.path.join(lib.directory, x.encode()) |
| 39 |
for x in self.config["ignore_subdirectories"].as_str_seq() |
| 40 |
] |
| 41 |
in_folder = set() |
| 42 |
for root, _, files in os.walk(lib.directory): |
| 43 |
# do not traverse if root is a child of an ignored directory |
| 44 |
if any(root.startswith(ignored) for ignored in ignore_dirs): |
| 45 |
continue |
| 46 |
for file in files: |
| 47 |
# ignore files with ignored extensions |
| 48 |
if any(file.endswith(ext) for ext in ignore_exts): |
| 49 |
continue |
| 50 |
in_folder.add(os.path.join(root, file)) |
| 51 |
|
| 52 |
in_library = {x.path for x in lib.items()} |
| 53 |
art_files = {x.artpath for x in lib.albums()} |
| 54 |
for f in in_folder - in_library - art_files: |
| 55 |
print_(util.displayable_path(f)) |
| 56 |
|
| 57 |
unimported = Subcommand( |
| 58 |
"unimported", |
| 59 |
help="list all files in the library folder which are not listed" |
| 60 |
" in the beets library database", |
| 61 |
) |
| 62 |
unimported.func = print_unimported |
| 63 |
return [unimported] |