Skip to content

Commit f930875

Browse files
Fixes and refinements, 100% coverage.
1 parent 3267205 commit f930875

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

beetsplug/fromfilename.py

Lines changed: 14 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,11 +112,12 @@
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)
@@ -159,13 +160,13 @@ def __init__(self) -> None:
159160
}
160161
)
161162
self.fields = set(self.config["fields"].as_str_seq())
162-
# Evaluate the user patterns to expand the fields
163163
self.file_patterns = self._user_pattern_to_regex(
164164
self.config["patterns"]["file"].as_str_seq()
165165
)
166166
self.folder_patterns = self._user_pattern_to_regex(
167167
self.config["patterns"]["folder"].as_str_seq()
168168
)
169+
self.session_fields: set[str] = set()
169170
self.register_listener("import_task_start", self.filename_task)
170171

171172
@cached_property
@@ -209,16 +210,13 @@ def _check_missing_data(self, items: list[Item]) -> bool:
209210
If no fields are detect that need to be processed,
210211
return false to shortcut the plugin.
211212
"""
212-
remove = set()
213+
self.session_fields = set({})
213214
for field in self.fields:
214215
# If any field is a bad field
215216
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
217+
self.session_fields.add(field)
220218
# If all fields have been removed, there is nothing to do
221-
if not len(self.fields):
219+
if not len(self.session_fields):
222220
return False
223221
return True
224222

@@ -335,13 +333,13 @@ def _apply_matches(
335333
track_matches: dict[Item, FilenameMatch],
336334
) -> None:
337335
"""Apply all valid matched fields to all items in the match dictionary."""
338-
match = album_match
336+
match = dict(album_match._matches)
339337
for item in track_matches:
340-
match.update(track_matches[item])
338+
match.update(track_matches[item]._matches)
341339
found_data: dict[str, int | str] = {}
342340
self._log.debug(f"Attempting keys: {match.keys()}")
343341
for key in match.keys():
344-
if key in self.fields:
342+
if key in self.session_fields:
345343
old_value = item.get(key)
346344
new_value = match[key]
347345
if self._bad_field(old_value) and new_value:
@@ -445,7 +443,7 @@ def _parse_user_pattern_strings(self, text: str) -> str | None:
445443
for f in fields:
446444
pattern = re.sub(rf"\\\${f}", f"(?P<{f}>.+)", pattern)
447445
self.fields.add(f)
448-
return rf"{pattern}"
446+
return pattern
449447

450448
@staticmethod
451449
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)