Skip to content

Conversation

arsaboo
Copy link
Contributor

@arsaboo arsaboo commented Sep 14, 2025

Description

Introduce a --pretend option to the lastgenre plugin, allowing users to preview genre changes without making any modifications to their library. This feature enhances user control by showing potential changes before they are applied.

To Do

  • Documentation. (If you've added a new command-line flag, for example, find the appropriate page under docs/ to describe it.)
  • Changelog. (Add an entry to docs/changelog.rst to the bottom of one of the lists near the top of the document.)
  • Tests. (Very much encouraged but not strictly required.)

cc: @JOJ0

Summary by Sourcery

Introduce a --pretend option to the lastgenre plugin to allow users to preview genre assignments without altering their library files.

New Features:

  • Add --pretend flag to preview genre changes without modifying files

Enhancements:

  • Conditionally skip updating item and album genres and file writes when --pretend is enabled

Documentation:

  • Document the --pretend option in the plugin documentation and update the changelog

@arsaboo arsaboo requested a review from a team as a code owner September 14, 2025 19:41
@Copilot Copilot AI review requested due to automatic review settings September 14, 2025 19:41
Copy link
Contributor

sourcery-ai bot commented Sep 14, 2025

Reviewer's Guide

This PR introduces a new --pretend flag to the lastgenre plugin, which lets users preview genre assignments without modifying the library. It refactors the genre lookup workflow—renaming temporary variables, improving logging placeholders, and relocating store/write calls under the pretend conditional—and adds corresponding documentation and changelog entries.

Sequence diagram for lastgenre command with --pretend option

sequenceDiagram
    actor User
    participant "lastgenre plugin"
    participant Library
    User->>"lastgenre plugin": Run lastgenre command with --pretend
    "lastgenre plugin"->>Library: Fetch albums/items
    "lastgenre plugin"->>"lastgenre plugin": Lookup genre for each album/item
    "lastgenre plugin"->>User: Log/display genre suggestions
    Note over "lastgenre plugin": No changes are made to Library
Loading

Class diagram for updated lastgenre command handling

classDiagram
    class LastGenrePlugin {
        +commands()
        -_get_genre()
        -_log
        -sources
        +config
    }
    class lastgenre_func {
        +lib
        +opts
        +args
        +write
        +pretend
    }
    LastGenrePlugin <|-- lastgenre_func
    LastGenrePlugin : +commands() adds --pretend option
    lastgenre_func : +pretend flag controls whether changes are applied
    lastgenre_func : genre assignment and store/write calls are conditional on pretend
Loading

File-Level Changes

Change Details Files
Add --pretend mode to bypass all writes
  • Define -p/--pretend CLI option
  • Capture opts.pretend in the command function
  • Wrap genre assignment, store, and write calls in a not-pretend check
beetsplug/lastgenre/__init__.py
Refactor genre retrieval flow and logging
  • Rename intermediate variables to album_genre/item_genre
  • Update log messages to use {} placeholders
  • Reposition .store() and .try_write() calls after pretend checks
beetsplug/lastgenre/__init__.py
Update documentation and changelog
  • Document the --pretend option in the plugin guide
  • Add a changelog entry for the pretend feature
docs/plugins/lastgenre.rst
docs/changelog.rst

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a --pretend option to the lastgenre plugin that allows users to preview genre changes without actually applying them to their music library. This provides a safe way to test genre fetching behavior before committing to changes.

Key changes:

  • Add command-line --pretend flag to the lastgenre plugin
  • Modify genre assignment logic to conditionally apply changes based on pretend mode
  • Update logging format to show genre values even when not applied

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
docs/plugins/lastgenre.rst Documents the new --pretend flag functionality
docs/changelog.rst Adds changelog entry for the new feature
beetsplug/lastgenre/init.py Implements the pretend option with conditional genre assignment
Comments suppressed due to low confidence (1)

beetsplug/lastgenre/init.py:548

  • The conditional logic is incorrect. When pretend mode is active, the album should not be stored at all, but the current logic will still execute album.store() calls when \"track\" not in self.sources. The store operations should be moved inside the if not pretend: block.
                    if not pretend:
                        album.genre = album_genre
                        if "track" in self.sources:
                            album.store(inherit=False)
                        else:
                            album.store()

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@arsaboo arsaboo changed the title Add --pretend option to lastgenre plugin for previewing genre changes lastgenre: Add --pretend option for previewing genre changes Sep 14, 2025
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • The singleton-items branch no longer calls item.try_write(), so disk writes are missing for non-album tracks—restore the original write logic there.
  • There’s a lot of repeated “if not pretend” + store/store+write logic; consider extracting it into a helper to keep the code DRY and consistent.
  • The log messages mix {} and numbered placeholders, but Python logging expects %-style formats—please unify to the correct placeholder syntax.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The singleton-items branch no longer calls item.try_write(), so disk writes are missing for non-album tracks—restore the original write logic there.
- There’s a lot of repeated “if not pretend” + store/store+write logic; consider extracting it into a helper to keep the code DRY and consistent.
- The log messages mix `{}` and numbered placeholders, but Python logging expects `%`-style formats—please unify to the correct placeholder syntax.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@JOJ0
Copy link
Member

JOJ0 commented Sep 14, 2025

Hmmm, it's too bad we are working on the same thing at the same time. I guess that's why I made the checklist with all the things I want to bring into lastgenre (mentioning that I have them already implemented);-) I have something pretty similar ready since weeks, but I'm not too happy with it because it introduces a hell load of more if/else clutter in this already polluted plugin. I'm working on something smarte, let's hope I get it done, if not we merge this one :-)

@arsaboo
Copy link
Contributor Author

arsaboo commented Sep 14, 2025

I am happy to close this one....you are much closer to the LG plugin. We can wait for your fix.

Look at the positive side....we both felt the need for this :)

@arsaboo
Copy link
Contributor Author

arsaboo commented Sep 14, 2025

@sourcery-ai review

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • There’s a lot of repeated if not pretend logic around setting genre/store/try_write—consider extracting that into a small helper or using a guard clause to DRY it up.
  • The log messages mix {0.attr} and bare {} placeholders, which can be confusing—pick one formatting style for consistency and clarity.
  • Since the parser always defines opts.pretend, you can use opts.pretend directly instead of getattr(opts, 'pretend', False).
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- There’s a lot of repeated `if not pretend` logic around setting `genre`/`store`/`try_write`—consider extracting that into a small helper or using a guard clause to DRY it up.
- The log messages mix `{0.attr}` and bare `{}` placeholders, which can be confusing—pick one formatting style for consistency and clarity.
- Since the parser always defines `opts.pretend`, you can use `opts.pretend` directly instead of `getattr(opts, 'pretend', False)`.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@snejus
Copy link
Member

snejus commented Sep 17, 2025

I was literally going to request this feature!

snejus
snejus previously requested changes Sep 17, 2025
Copy link
Member

@snejus snejus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also include a small test to prove this works?

@arsaboo
Copy link
Contributor Author

arsaboo commented Sep 17, 2025

Tests are not my strong suit....but here are the logs with pretend:

lastgenre: genre for album "Achanak Bhayanak" (original fallback): Indi Pop
lastgenre: raw last.fm tags: ['indi pop', 'pop']
lastgenre: existing genres taken into account: ['indi pop']
lastgenre: genre for album "Ye Bhi Woh Bhi" (keep + album artist, whitelist): Indi Pop
lastgenre: last.fm error: Album not found

beet info confirms that the genre tag was not updated:

$ beet info album:"Achanak Bhayanak"
/data/music/Hindi Music/A/Achanak Bhayanak (2021)/Achanak Bhayanak (2021) - Achanak Bhayanak.mp3
            album: Achanak Bhayanak
      albumartist: 7Bantai'Z
        albumtype: single
       albumtypes: single
         arranger:
              art: True
           artist: 7Bantai'Z, Bluesanova
         bitdepth: 0
          bitrate: 198329
     bitrate_mode: VBR
              bpm: 140
         channels: 2
             comp: False
             date: 2021-07-23
              day: 23
             disc: 1
        disctotal: 1
          encoder: Lavf58.29.100
     encoder_info:
 encoder_settings:
           format: MP3
            genre: Indi Pop
           genres: Indi Pop
            label: 7Bantai'Z
           length: 253.83183673469387
           lyrics:
 mb_albumartistid: 0Mz9BTzWvM56Ap38mJ5zpK
mb_albumartistids: 0Mz9BTzWvM56Ap38mJ5zpK
       mb_albumid: 1l4GQzsOX2A1LDBsZ0fWe9
      mb_artistid: 0Mz9BTzWvM56Ap38mJ5zpK
     mb_artistids: 0Mz9BTzWvM56Ap38mJ5zpK
       mb_trackid: 1dDvlh2AfyknqYQgS06h8U
            month: 7
    original_year: 0
       samplerate: 44100
            title: Achanak Bhayanak
            track: 1
       tracktotal: 1
             year: 2021

@JOJ0
Copy link
Member

JOJ0 commented Sep 18, 2025

Just a sidenote: Do not worry here about proper logging. In my opinion lines should be logged something like lastgenre: Pretend: genre for album "Achanak Bhayanak" (original fallback): Indi Pop so it is clear to the user that nothing had changed.

I will add that in #6021 anyway!

@JOJ0
Copy link
Member

JOJ0 commented Sep 18, 2025

Tests are not my strong suit....but here are the logs with pretend:

What about prompting an AI to draft a test and we work from there? Review it together here?

@snejus
Copy link
Member

snejus commented Sep 18, 2025

Tests are not my strong suit....but here are the logs with pretend:

What about prompting an AI to draft a test and we work from there? Review it together here?

That's a very good idea! What do you think @arsaboo ?

@arsaboo
Copy link
Contributor Author

arsaboo commented Sep 18, 2025

@JOJ0 @snejus updated the logging format and added a test as well. Everything is ✅

Copy link
Member

@JOJ0 JOJ0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I improved the test slightly. Removed some not really necessary asserts and added mock of try_write. Many thanks for the submission @arsaboo, this feature was on the wishlist for many of us already! :-)

@JOJ0 JOJ0 dismissed snejus’s stale review September 21, 2025 05:19

@arsaboo addressed all issues. not sure why github things these requested changes were not fullfilled yet.

@JOJ0 JOJ0 merged commit 2c1aa27 into beetbox:master Sep 21, 2025
17 checks passed
@arsaboo arsaboo deleted the pretend branch September 21, 2025 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants