fromfilename plugin: proposed fix for #5218, some improvements (#5311)

### b5216a0 is a proposed fix for #5218

We don't assume anymore that if the pattern lacks the "artist" group it
also has the "title" group (this was causing the "title" KeyError, when
the pattern containg only the `?P<track>` group is used).
Instead, we check for existence of "title" before assigning
"title_field", otherwise we let the last pattern (which will always
match) to assign it. The only slight drawback is that files like
"01.mp3" will also get "01" as title (besides the "1" track number),
instead of having no title, but I guess that is not going to cause any
major problem in the rest of the import procedure (e.g. track matching
after a search). On the other hand, I guess allowing a file to have no
title would require a substantial rewriting of the plugin, since the
empty string is in BAD_TITLE_PATTERNS.

### 0966042 adds a log message about the pattern being tried
This is useful while debugging changes to the regexps in PATTERNS.
I used loglevel "debug" for this message, should we use "debug" also for
other messages (currently using level "info")?

### e6b7735 refactors the regexps in PATTERNS

I reviewed the regexps (especially after the changes I made some time
ago in 84cf336) and tried to make them cleaner.
- Allow " - " and "-" as separator between track/artist/title fields:
that should cover two common filename conventions: using spaces (e.g.
`01 - Artist Name - Title of the song`) or not using spaces (e.g.
`01-artist_name-title_of_the_song`). Some regexp groups are non-greedy,
to avoid capturing unwanted leading/trailing spaces in artist/titles; we
could think about some other enhancements, e.g. automatically converting
underscores to spaces in artist/title names, but I guess the main use of
the plugin is not to produce a perfectly formatted title, but a
sufficient meaningful one to be used as reference in the following
stages of the update procedure.
- Allow `?P<tag>` groups by making them optional in the first two
patterns (instead of having two versions of the pattern, with or without
tag). BTW I'm not sure what these groups are about: we optionally check
for them in the plugin, but we never use them to assign any metadata.
- Allow optional "." after the track number
- In the third pattern: also allow "_" as separator (e.g. for file names
like `01_some_tack.mp3`)
This commit is contained in:
Šarūnas Nejus 2025-10-07 23:39:48 +01:00 committed by GitHub
commit d4a0c02ca3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 118 additions and 14 deletions

View file

@ -12,8 +12,8 @@
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
"""If the title is empty, try to extract track and title from the
filename.
"""If the title is empty, try to extract it from the filename
(possibly also extract track and artist)
"""
import os
@ -25,12 +25,12 @@ from beets.util import displayable_path
# Filename field extraction patterns.
PATTERNS = [
# Useful patterns.
r"^(?P<artist>.+)[\-_](?P<title>.+)[\-_](?P<tag>.*)$",
r"^(?P<track>\d+)[\s.\-_]+(?P<artist>.+)[\-_](?P<title>.+)[\-_](?P<tag>.*)$",
r"^(?P<artist>.+)[\-_](?P<title>.+)$",
r"^(?P<track>\d+)[\s.\-_]+(?P<artist>.+)[\-_](?P<title>.+)$",
r"^(?P<track>\d+)[\s.\-_]+(?P<title>.+)$",
r"^(?P<track>\d+)\s+(?P<title>.+)$",
(
r"^(?P<track>\d+)\.?\s*-\s*(?P<artist>.+?)\s*-\s*(?P<title>.+?)"
r"(\s*-\s*(?P<tag>.*))?$"
),
r"^(?P<artist>.+?)\s*-\s*(?P<title>.+?)(\s*-\s*(?P<tag>.*))?$",
r"^(?P<track>\d+)\.?[\s_-]+(?P<title>.+)$",
r"^(?P<title>.+) by (?P<artist>.+)$",
r"^(?P<track>\d+).*$",
r"^(?P<title>.+)$",
@ -98,6 +98,7 @@ def apply_matches(d, log):
# Given both an "artist" and "title" field, assume that one is
# *actually* the artist, which must be uniform, and use the other
# for the title. This, of course, won't work for VA albums.
# Only check for "artist": patterns containing it, also contain "title"
if "artist" in keys:
if equal_fields(d, "artist"):
artist = some_map["artist"]
@ -113,15 +114,16 @@ def apply_matches(d, log):
if not item.artist:
item.artist = artist
log.info("Artist replaced with: {.artist}", item)
# No artist field: remaining field is the title.
else:
# otherwise, if the pattern contains "title", use that for title_field
elif "title" in keys:
title_field = "title"
else:
title_field = None
# Apply the title and track.
# Apply the title and track, if any.
for item in d:
if bad_title(item.title):
item.title = str(d[item].get(title_field, ""))
if title_field and bad_title(item.title):
item.title = str(d[item][title_field])
log.info("Title replaced with: {.title}", item)
if "track" in d[item] and item.track == 0:
@ -160,6 +162,7 @@ class FromFilenamePlugin(plugins.BeetsPlugin):
# Look for useful information in the filenames.
for pattern in PATTERNS:
self._log.debug(f"Trying pattern: {pattern}")
d = all_matches(names, pattern)
if d:
apply_matches(d, self._log)

View file

@ -41,6 +41,8 @@ Bug fixes:
artists but not labels. :bug:`5366`
- :doc:`plugins/chroma` :doc:`plugins/bpsync` Fix plugin loading issue caused by
an import of another :class:`beets.plugins.BeetsPlugin` class. :bug:`6033`
- :doc:`/plugins/fromfilename`: Fix :bug:`5218`, improve the code (refactor
regexps, allow for more cases, add some logging), add tests.
For packagers:

View file

@ -0,0 +1,99 @@
# This file is part of beets.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
"""Tests for the fromfilename plugin."""
import pytest
from beetsplug import fromfilename
class Session:
pass
class Item:
def __init__(self, path):
self.path = path
self.track = 0
self.artist = ""
self.title = ""
class Task:
def __init__(self, items):
self.items = items
self.is_album = True
@pytest.mark.parametrize(
"song1, song2",
[
(
(
"/tmp/01 - The Artist - Song One.m4a",
1,
"The Artist",
"Song One",
),
(
"/tmp/02. - The Artist - Song Two.m4a",
2,
"The Artist",
"Song Two",
),
),
(
("/tmp/01-The_Artist-Song_One.m4a", 1, "The_Artist", "Song_One"),
("/tmp/02.-The_Artist-Song_Two.m4a", 2, "The_Artist", "Song_Two"),
),
(
("/tmp/01 - Song_One.m4a", 1, "", "Song_One"),
("/tmp/02. - Song_Two.m4a", 2, "", "Song_Two"),
),
(
("/tmp/Song One by The Artist.m4a", 0, "The Artist", "Song One"),
("/tmp/Song Two by The Artist.m4a", 0, "The Artist", "Song Two"),
),
(("/tmp/01.m4a", 1, "", "01"), ("/tmp/02.m4a", 2, "", "02")),
(
("/tmp/Song One.m4a", 0, "", "Song One"),
("/tmp/Song Two.m4a", 0, "", "Song Two"),
),
],
)
def test_fromfilename(song1, song2):
"""
Each "song" is a tuple of path, expected track number, expected artist,
expected title.
We use two songs for each test for two reasons:
- The plugin needs more than one item to look for uniform strings in paths
in order to guess if the string describes an artist or a title.
- Sometimes we allow for an optional "." after the track number in paths.
"""
session = Session()
item1 = Item(song1[0])
item2 = Item(song2[0])
task = Task([item1, item2])
f = fromfilename.FromFilenamePlugin()
f.filename_task(task, session)
assert task.items[0].track == song1[1]
assert task.items[0].artist == song1[2]
assert task.items[0].title == song1[3]
assert task.items[1].track == song2[1]
assert task.items[1].artist == song2[2]
assert task.items[1].title == song2[3]