beetbox / beets

music library manager and MusicBrainz tagger
http://beets.io/
MIT License
12.57k stars 1.8k forks source link

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

Open Vrihub opened 2 weeks ago

Vrihub commented 2 weeks ago

Description

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.

github-actions[bot] commented 2 weeks ago

Thank you for the PR! The changelog has not been updated, so here is a friendly reminder to check if you need to add an entry.

snejus commented 2 weeks ago

Could you by any chance add some tests that show the range of filenames that can be parsed? This would be very useful going forward.

Serene-Arc commented 2 weeks ago

Second @snejus here! Thanks for the PR too. Regex is so complicated and obtuse to many people, so it's always good to have as complete a testing regimen in place as possible, both to find bugs with the provided patterns and to ensure that the same strings are matched going forward with any changes.

Vrihub commented 1 week ago

Ok, I'll have a look at how the tests machinery works and I'll give it a try.

(Feel free to pull the first commit of this PR to quickly fix the crash bug, if you agree with the proposed solution)

Vrihub commented 4 days ago

I've added tests for the plugin, covering almost all the use cases for the regexps in PATTERNS (as re-written by the previous commits in this PR).

To set up mock objects for the items to be imported I haven't used the tools in /beets/test, only plain unittest.mock, so maybe there's a more beets-esque way to implement these: suggestions are welcome.