Skip to content

Commit f8b9f44

Browse files
Fixes and refinements
1 parent 3267205 commit f8b9f44

3 files changed

Lines changed: 19 additions & 18 deletions

File tree

beetsplug/fromfilename.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"""
1818

1919
import re
20-
from collections.abc import Iterator, MutableMapping, ValuesView
20+
from collections.abc import Iterator, KeysView, MutableMapping, ValuesView
2121
from datetime import datetime
2222
from functools import cached_property
2323
from pathlib import Path
@@ -112,15 +112,19 @@
112112

113113

114114
class FilenameMatch(MutableMapping[str, str | None]):
115-
def __init__(self, matches: dict[str, str] = {}) -> None:
115+
def __init__(self, matches: dict[str, str] | None = None) -> None:
116116
self._matches: dict[str, str] = {}
117-
for key, value in matches.items():
118-
if value is not None:
119-
self._matches[key.lower()] = str(value).strip()
117+
if matches:
118+
for key, value in matches.items():
119+
if value is not None:
120+
self._matches[key.lower()] = str(value).strip()
120121

121122
def __getitem__(self, key: str) -> str | None:
122123
return self._matches.get(key, None)
123124

125+
def keys(self) -> KeysView[str]:
126+
return self._matches.keys()
127+
124128
def __iter__(self) -> Iterator[str]:
125129
return iter(self._matches)
126130

@@ -159,13 +163,13 @@ def __init__(self) -> None:
159163
}
160164
)
161165
self.fields = set(self.config["fields"].as_str_seq())
162-
# Evaluate the user patterns to expand the fields
163166
self.file_patterns = self._user_pattern_to_regex(
164167
self.config["patterns"]["file"].as_str_seq()
165168
)
166169
self.folder_patterns = self._user_pattern_to_regex(
167170
self.config["patterns"]["folder"].as_str_seq()
168171
)
172+
self.session_fields: set[str] = set()
169173
self.register_listener("import_task_start", self.filename_task)
170174

171175
@cached_property
@@ -209,16 +213,13 @@ def _check_missing_data(self, items: list[Item]) -> bool:
209213
If no fields are detect that need to be processed,
210214
return false to shortcut the plugin.
211215
"""
212-
remove = set()
216+
self.session_fields = set({})
213217
for field in self.fields:
214218
# If any field is a bad field
215219
if any([True for item in items if self._bad_field(item[field])]):
216-
continue
217-
else:
218-
remove.add(field)
219-
self.fields = self.fields - remove
220+
self.session_fields.add(field)
220221
# If all fields have been removed, there is nothing to do
221-
if not len(self.fields):
222+
if not len(self.session_fields):
222223
return False
223224
return True
224225

@@ -335,13 +336,13 @@ def _apply_matches(
335336
track_matches: dict[Item, FilenameMatch],
336337
) -> None:
337338
"""Apply all valid matched fields to all items in the match dictionary."""
338-
match = album_match
339+
match = dict(album_match._matches)
339340
for item in track_matches:
340-
match.update(track_matches[item])
341+
match.update(track_matches[item]._matches)
341342
found_data: dict[str, int | str] = {}
342343
self._log.debug(f"Attempting keys: {match.keys()}")
343344
for key in match.keys():
344-
if key in self.fields:
345+
if key in self.session_fields:
345346
old_value = item.get(key)
346347
new_value = match[key]
347348
if self._bad_field(old_value) and new_value:
@@ -445,7 +446,7 @@ def _parse_user_pattern_strings(self, text: str) -> str | None:
445446
for f in fields:
446447
pattern = re.sub(rf"\\\${f}", f"(?P<{f}>.+)", pattern)
447448
self.fields.add(f)
448-
return rf"{pattern}"
449+
return pattern
449450

450451
@staticmethod
451452
def _mutate_string(text: str, span: tuple[int, int]) -> str:

docs/plugins/fromfilename.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Default
6363
of the file.
6464

6565
If ``fromfilename`` can't match the entire string to the given pattern, it will
66-
falls back to the default pattern.
66+
fall back to the default pattern.
6767

6868
The following custom patterns will match this path and retrieve the specified
6969
fields.

test/plugins/test_fromfilename.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ def test_changes_missing_values(self):
746746
) as mock:
747747
f = FromFilenamePlugin()
748748
f.filename_task(task, Session())
749-
assert len(f.fields) == 1
749+
assert len(f.session_fields) == 1
750750
assert "title" in f.fields
751751
mock.assert_called()
752752

0 commit comments

Comments
 (0)