|
17 | 17 | """ |
18 | 18 |
|
19 | 19 | import re |
20 | | -from collections.abc import Iterator, MutableMapping, ValuesView |
| 20 | +from collections.abc import Iterator, KeysView, MutableMapping, ValuesView |
21 | 21 | from datetime import datetime |
22 | 22 | from functools import cached_property |
23 | 23 | from pathlib import Path |
|
112 | 112 |
|
113 | 113 |
|
114 | 114 | 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: |
116 | 116 | 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() |
120 | 121 |
|
121 | 122 | def __getitem__(self, key: str) -> str | None: |
122 | 123 | return self._matches.get(key, None) |
@@ -159,13 +160,13 @@ def __init__(self) -> None: |
159 | 160 | } |
160 | 161 | ) |
161 | 162 | self.fields = set(self.config["fields"].as_str_seq()) |
162 | | - # Evaluate the user patterns to expand the fields |
163 | 163 | self.file_patterns = self._user_pattern_to_regex( |
164 | 164 | self.config["patterns"]["file"].as_str_seq() |
165 | 165 | ) |
166 | 166 | self.folder_patterns = self._user_pattern_to_regex( |
167 | 167 | self.config["patterns"]["folder"].as_str_seq() |
168 | 168 | ) |
| 169 | + self.session_fields: set[str] = set() |
169 | 170 | self.register_listener("import_task_start", self.filename_task) |
170 | 171 |
|
171 | 172 | @cached_property |
@@ -209,16 +210,13 @@ def _check_missing_data(self, items: list[Item]) -> bool: |
209 | 210 | If no fields are detect that need to be processed, |
210 | 211 | return false to shortcut the plugin. |
211 | 212 | """ |
212 | | - remove = set() |
| 213 | + self.session_fields = set({}) |
213 | 214 | for field in self.fields: |
214 | 215 | # If any field is a bad field |
215 | 216 | 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) |
220 | 218 | # If all fields have been removed, there is nothing to do |
221 | | - if not len(self.fields): |
| 219 | + if not len(self.session_fields): |
222 | 220 | return False |
223 | 221 | return True |
224 | 222 |
|
@@ -335,13 +333,13 @@ def _apply_matches( |
335 | 333 | track_matches: dict[Item, FilenameMatch], |
336 | 334 | ) -> None: |
337 | 335 | """Apply all valid matched fields to all items in the match dictionary.""" |
338 | | - match = album_match |
| 336 | + match = dict(album_match._matches) |
339 | 337 | for item in track_matches: |
340 | | - match.update(track_matches[item]) |
| 338 | + match.update(track_matches[item]._matches) |
341 | 339 | found_data: dict[str, int | str] = {} |
342 | 340 | self._log.debug(f"Attempting keys: {match.keys()}") |
343 | 341 | for key in match.keys(): |
344 | | - if key in self.fields: |
| 342 | + if key in self.session_fields: |
345 | 343 | old_value = item.get(key) |
346 | 344 | new_value = match[key] |
347 | 345 | if self._bad_field(old_value) and new_value: |
@@ -445,7 +443,7 @@ def _parse_user_pattern_strings(self, text: str) -> str | None: |
445 | 443 | for f in fields: |
446 | 444 | pattern = re.sub(rf"\\\${f}", f"(?P<{f}>.+)", pattern) |
447 | 445 | self.fields.add(f) |
448 | | - return rf"{pattern}" |
| 446 | + return pattern |
449 | 447 |
|
450 | 448 | @staticmethod |
451 | 449 | def _mutate_string(text: str, span: tuple[int, int]) -> str: |
|
0 commit comments