Skip to content

Commit 86f869f

Browse files
committed
feat(unimported): add a configuration option to consider ignore_subdirectories as globs
Uses the same method to go through the files than the importer, `util.sorted_walk`
1 parent 52cf741 commit 86f869f

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

beetsplug/unimported.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,43 @@
2929
class Unimported(BeetsPlugin):
3030
def __init__(self):
3131
super().__init__()
32-
self.config.add({"ignore_extensions": [], "ignore_subdirectories": []})
32+
self.config.add(
33+
{
34+
"ignore_extensions": [],
35+
"ignore_subdirectories": [],
36+
"ignore_as_globs": False,
37+
}
38+
)
39+
40+
def walk(self, lib):
41+
ignore_subdirs = self.config["ignore_subdirectories"].as_str_seq()
42+
if self.config["ignore_as_globs"].get(bool):
43+
# The way beets ignore elements in the library, using globbing,
44+
# whatever the depth
45+
for root, _, files in util.sorted_walk(
46+
lib.directory, ignore=ignore_subdirs
47+
):
48+
yield (root, files)
49+
else:
50+
# the reverse-compatible search, with ignore_subdirectories as
51+
# a direct child of the library root
52+
ignore_dirs = [
53+
os.path.join(lib.directory, x.encode()) for x in ignore_subdirs
54+
]
55+
for root, _, files in os.walk(lib.directory):
56+
# do not traverse if root is a child of an ignored directory
57+
if any(root.startswith(ignored) for ignored in ignore_dirs):
58+
continue
59+
yield (root, files)
3360

3461
def commands(self):
3562
def print_unimported(lib, opts, args):
3663
ignore_exts = [
3764
("." + x).encode()
3865
for x in self.config["ignore_extensions"].as_str_seq()
3966
]
40-
ignore_dirs = [
41-
os.path.join(lib.directory, x.encode())
42-
for x in self.config["ignore_subdirectories"].as_str_seq()
43-
]
4467
in_folder = set()
45-
for root, _, files in os.walk(lib.directory):
46-
# do not traverse if root is a child of an ignored directory
47-
if any(root.startswith(ignored) for ignored in ignore_dirs):
48-
continue
68+
for root, files in self.walk(lib):
4969
for file in files:
5070
# ignore files with ignored extensions
5171
if any(file.endswith(ext) for ext in ignore_exts):

docs/plugins/unimported.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ exclude file extensions or entire subdirectories using the configuration file::
1414
unimported:
1515
ignore_extensions: jpg png
1616
ignore_subdirectories: NonMusic data temp
17+
ignore_as_globs: false
1718

1819
The default configuration lists all unimported files, ignoring no extensions.
20+
21+
When true, the `ignore_as_globs` parameter uses the same way of parsing files
22+
as beets, using the `ignore_subdirectories` as globas whatever the depth,
23+
instead of excluding them if they are the direct child of the library root.

0 commit comments

Comments
 (0)