Repository: beetbox/beets

Issue:
`unimported`'s `ignore_subdirectories` doesn't work
### Problem


```sh
beet unimported
```

Leads to directories specified in `ignore_subdirectories` still being listed

### Setup

* OS: Arch Linux
* Python version: 3.11.7 
* beets version: 1.6.1
* Turning off plugins made problem go away (yes/no): n/a

My configuration (output of `beet config`) is:

```yaml
unimported:
  ignore_extensions: jpg png txt md org mod
  ignore_subdirectories: Unsorted import
```
`ignore_extensions` works as expected though




Discussion:
post your output of `beet -vv unimported`

also, the full path of the directory being listed and your full library path

need this to recreate your directory structure to debug

edit: also "Unsorted" and "import" are directory names just to be on the same page
I don't want to share half of my music collection so I removed most of the tracks and playlists from the output leaving just a few examples.
Full paths to both the music lib and included dirs are in the output
And yes, "Unsorted" and "import" are directory names as you can see below.

`beet -vv unimported`
```
user configuration: /home/artem/.config/beets/config.yaml
data directory: /home/artem/.config/beets
plugin paths:
lyrics: Disabling google source: no API key configured.
fetchart: google: Disabling art source due to missing key
fetchart: lastfm: Disabling art source due to missing key
Sending event: pluginload
library database: /home/artem/.config/beets/library.db
library directory: /home/artem/music
Sending event: library_opened
/home/artem/music/import/fjh/FJH_P-Era - SynkaPath v1.mp3
/home/artem/music/import/fjh/Nilow - Chinese Dreams (FJH Dubstep Remix).mp3
/home/artem/music/Pakito/Living on Video.mp3
/home/artem/music/Unsorted/en/Just Another Night.mp3
/home/artem/music/Unsorted/en/Pin-Occhio.mp3
/home/artem/music/Unsorted/en/No More Pain.mp3
/home/artem/music/import/ost/Games/Neverwinter/City Boss Battle (Neverwinter Nights Soundtrack.mp3
/home/artem/music/import/chase-and-status/Chase & Status () - 07 Track 7.mp3
/home/artem/music/Unsorted/en/Playing the villain.mp3
Sending event: cli_exit
playlist: Updating playlist: /home/artem/.config/mpd/playlists/classical.m3u
playlist: Updating playlist: /home/artem/.config/mpd/playlists/instrument-violin.m3u
playlist: Updating playlist: /home/artem/.config/mpd/playlists/instrument-piano.m3u
playlist: Updating playlist: /home/artem/.config/mpd/playlists/jazz.m3u
playlist: Updating playlist: /home/artem/.config/mpd/playlists/lofi-girl.m3u
playlist: Updating playlist: /home/artem/.config/mpd/playlists/cocktail.m3u
playlist: Updating playlist: /home/artem/.config/mpd/playlists/electro-swing.m3u
playlist: Updating playlist: /home/artem/.config/mpd/playlists/chillout.m3u
playlist: Updating playlist: /home/artem/.config/mpd/playlists/christmas.m3u
playlist: Updating playlist: /home/artem/.config/mpd/playlists/lofi.m3u
playlist: Updating playlist: /home/artem/.config/mpd/playlists/smooth-jazz.m3u
playlist: Updating playlist: /home/artem/.config/mpd/playlists/blues.m3u
```

Resolving patch:
diff --git a/beetsplug/unimported.py b/beetsplug/unimported.py
--- a/beetsplug/unimported.py
+++ b/beetsplug/unimported.py
@@ -41,15 +41,17 @@ def print_unimported(lib, opts, args):
                 os.path.join(lib.directory, x.encode())
                 for x in self.config["ignore_subdirectories"].as_str_seq()
             ]
-            in_folder = {
-                os.path.join(r, file)
-                for r, d, f in os.walk(lib.directory)
-                for file in f
-                if not any(
-                    [file.endswith(ext) for ext in ignore_exts]
-                    + [r in ignore_dirs]
-                )
-            }
+            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:


Test patch: