Skip to content

Commit beff630

Browse files
authored
refactor(ftintitle): cache config options (#6732)
Pre-factor for #6726. Make `ftintitle`'s config `@cached_property` attributes instead of passing them through the call stack. Modeled after similar recent changes in `convert`. ## Changes - Add cached plugin properties for `auto`, `drop`, `format`, `keep_in_artist`, `preserve_album_artist`, and `custom_words`, keeping the existing cached `bracket_keywords` property I forgot I added a while back. - `commands()`, `imported()`, `ft_in_title()`, and `update_metadata()` are cleaned up to read these directly. - Don't read `auto` during plugin init; the import stage remains registered and `imported()` checks `auto` when invoked.
2 parents ad03b35 + adc2056 commit beff630

1 file changed

Lines changed: 42 additions & 54 deletions

File tree

beetsplug/ftintitle.py

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,30 @@ def _album_artist_no_feat(album: Album) -> str:
139139

140140

141141
class FtInTitlePlugin(plugins.BeetsPlugin):
142+
@cached_property
143+
def auto(self) -> bool:
144+
return self.config["auto"].get(bool)
145+
146+
@cached_property
147+
def drop_feat(self) -> bool:
148+
return self.config["drop"].get(bool)
149+
150+
@cached_property
151+
def feat_format(self) -> str:
152+
return self.config["format"].as_str()
153+
154+
@cached_property
155+
def keep_in_artist_field(self) -> bool:
156+
return self.config["keep_in_artist"].get(bool)
157+
158+
@cached_property
159+
def preserve_album_artist(self) -> bool:
160+
return self.config["preserve_album_artist"].get(bool)
161+
162+
@cached_property
163+
def custom_words(self) -> list[str]:
164+
return self.config["custom_words"].as_str_seq()
165+
142166
@cached_property
143167
def bracket_keywords(self) -> list[str]:
144168
return self.config["bracket_keywords"].as_str_seq()
@@ -201,8 +225,7 @@ def __init__(self) -> None:
201225
help="drop featuring from artists and ignore title update",
202226
)
203227

204-
if self.config["auto"]:
205-
self.import_stages = [self.imported]
228+
self.import_stages = [self.imported]
206229

207230
self.album_template_fields["album_artist_no_feat"] = (
208231
_album_artist_no_feat
@@ -211,22 +234,10 @@ def __init__(self) -> None:
211234
def commands(self) -> list[ui.Subcommand]:
212235
def func(lib, opts, args):
213236
self.config.set_args(opts)
214-
drop_feat = self.config["drop"].get(bool)
215-
keep_in_artist_field = self.config["keep_in_artist"].get(bool)
216-
preserve_album_artist = self.config["preserve_album_artist"].get(
217-
bool
218-
)
219-
custom_words = self.config["custom_words"].get(list)
220237
write = ui.should_write()
221238

222239
for item in lib.items(args):
223-
if self.ft_in_title(
224-
item,
225-
drop_feat,
226-
keep_in_artist_field,
227-
preserve_album_artist,
228-
custom_words,
229-
):
240+
if self.ft_in_title(item):
230241
item.store()
231242
if write:
232243
item.try_write()
@@ -236,71 +247,50 @@ def func(lib, opts, args):
236247

237248
def imported(self, session: ImportSession, task: ImportTask) -> None:
238249
"""Import hook for moving featuring artist automatically."""
239-
drop_feat = self.config["drop"].get(bool)
240-
keep_in_artist_field = self.config["keep_in_artist"].get(bool)
241-
preserve_album_artist = self.config["preserve_album_artist"].get(bool)
242-
custom_words = self.config["custom_words"].get(list)
250+
if not self.auto:
251+
return
243252

244253
for item in task.imported_items():
245-
if self.ft_in_title(
246-
item,
247-
drop_feat,
248-
keep_in_artist_field,
249-
preserve_album_artist,
250-
custom_words,
251-
):
254+
if self.ft_in_title(item):
252255
item.store()
253256

254-
def update_metadata(
255-
self,
256-
item: Item,
257-
feat_part: str,
258-
drop_feat: bool,
259-
keep_in_artist_field: bool,
260-
custom_words: list[str],
261-
) -> None:
257+
def update_metadata(self, item: Item, feat_part: str) -> None:
262258
"""Choose how to add new artists to the title and set the new
263259
metadata. Also, print out messages about any changes that are made.
264260
If `drop_feat` is set, then do not add the artist to the title; just
265261
remove it from the artist field.
266262
"""
267263
# In case the artist is kept, do not update the artist fields.
268-
if keep_in_artist_field:
264+
if self.keep_in_artist_field:
269265
self._log.info(
270266
"artist: {.artist} (Not changing due to keep_in_artist)", item
271267
)
272268
else:
273269
track_artist, _ = split_on_feat(
274-
item.artist, custom_words=custom_words
270+
item.artist, custom_words=self.custom_words
275271
)
276272
self._log.info("artist: {0.artist} -> {1}", item, track_artist)
277273
item.artist = track_artist
278274

279275
if item.artist_sort:
280276
# Just strip the featured artist from the sort name.
281277
item.artist_sort, _ = split_on_feat(
282-
item.artist_sort, custom_words=custom_words
278+
item.artist_sort, custom_words=self.custom_words
283279
)
284280

285281
# Only update the title if it does not already contain a featured
286282
# artist and if we do not drop featuring information.
287-
if not drop_feat and not contains_feat(item.title, custom_words):
288-
feat_format = self.config["format"].as_str()
289-
formatted = feat_format.format(feat_part)
283+
if not self.drop_feat and not contains_feat(
284+
item.title, self.custom_words
285+
):
286+
formatted = self.feat_format.format(feat_part)
290287
new_title = self.insert_ft_into_title(
291288
item.title, formatted, self.bracket_keywords
292289
)
293290
self._log.info("title: {.title} -> {}", item, new_title)
294291
item.title = new_title
295292

296-
def ft_in_title(
297-
self,
298-
item: Item,
299-
drop_feat: bool,
300-
keep_in_artist_field: bool,
301-
preserve_album_artist: bool,
302-
custom_words: list[str],
303-
) -> bool:
293+
def ft_in_title(self, item: Item) -> bool:
304294
"""Look for featured artists in the item's artist fields and move
305295
them to the title.
306296
@@ -313,26 +303,24 @@ def ft_in_title(
313303
# Check whether there is a featured artist on this track and the
314304
# artist field does not exactly match the album artist field. In
315305
# that case, we attempt to move the featured artist to the title.
316-
if preserve_album_artist and albumartist and artist == albumartist:
306+
if self.preserve_album_artist and albumartist and artist == albumartist:
317307
return False
318308

319-
_, featured = split_on_feat(artist, custom_words=custom_words)
309+
_, featured = split_on_feat(artist, custom_words=self.custom_words)
320310
if not featured:
321311
return False
322312

323313
self._log.info("{.filepath}", item)
324314

325315
# Attempt to find the featured artist.
326-
feat_part = find_feat_part(artist, albumartist, custom_words)
316+
feat_part = find_feat_part(artist, albumartist, self.custom_words)
327317

328318
if not feat_part:
329319
self._log.info("no featuring artists found")
330320
return False
331321

332322
# If we have a featuring artist, move it to the title.
333-
self.update_metadata(
334-
item, feat_part, drop_feat, keep_in_artist_field, custom_words
335-
)
323+
self.update_metadata(item, feat_part)
336324
return True
337325

338326
@classmethod

0 commit comments

Comments
 (0)