File: unimported.py

package info (click to toggle)
beets 2.5.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 8,072 kB
  • sloc: python: 46,469; javascript: 8,018; xml: 334; sh: 245; makefile: 125
file content (66 lines) | stat: -rw-r--r-- 2,488 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# This file is part of beets.
# Copyright 2019, Joris Jensen
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""
List all files in the library folder which are not listed in the
 beets library database, including art files
"""

import os

from beets import util
from beets.plugins import BeetsPlugin
from beets.ui import Subcommand, print_

__author__ = "https://github.com/MrNuggelz"


class Unimported(BeetsPlugin):
    def __init__(self):
        super().__init__()
        self.config.add({"ignore_extensions": [], "ignore_subdirectories": []})

    def commands(self):
        def print_unimported(lib, opts, args):
            ignore_exts = [
                f".{x}".encode()
                for x in self.config["ignore_extensions"].as_str_seq()
            ]
            ignore_dirs = [
                os.path.join(lib.directory, x.encode())
                for x in self.config["ignore_subdirectories"].as_str_seq()
            ]
            in_folder = set()
            for root, _, files in os.walk(lib.directory):
                # do not traverse if root is a child of an ignored directory
                if any(root.startswith(ignored) for ignored in ignore_dirs):
                    continue
                for file in files:
                    # ignore files with ignored extensions
                    if any(file.endswith(ext) for ext in ignore_exts):
                        continue
                    in_folder.add(os.path.join(root, file))

            in_library = {x.path for x in lib.items()}
            art_files = {x.artpath for x in lib.albums()}
            for f in in_folder - in_library - art_files:
                print_(util.displayable_path(f))

        unimported = Subcommand(
            "unimported",
            help="list all files in the library folder which are not listed"
            " in the beets library database",
        )
        unimported.func = print_unimported
        return [unimported]