Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

edit plugin: make temporary file reflect --set CLI arguments #5056

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
26 changes: 24 additions & 2 deletions beetsplug/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import yaml

from beets import plugins, ui, util
from beets import plugins, ui, util, config
from beets.dbcore import types
from beets.importer import action
from beets.ui.commands import PromptChoice, _do_query
Expand Down Expand Up @@ -155,9 +155,14 @@ def __init__(self):
}
)

self.readonly_fields = {}

self.register_listener(
"before_choose_candidate", self.before_choose_candidate_listener
)
self.register_listener(
"import_begin", self.import_begin_listener
)

def commands(self):
edit_command = ui.Subcommand("edit", help="interactively edit metadata")
Expand Down Expand Up @@ -236,11 +241,22 @@ def edit_objects(self, objs, fields):
# Get the content to edit as raw data structures.
old_data = [flatten(o, fields) for o in objs]

# take set fields into account
if self.readonly_fields:
old_str = "# note: the following fields will be reset to their current values:\n"
for key in self.readonly_fields:
old_str += f"# - {key}\n"
for obj in old_data:
# those values will be enforced later anyway
obj.update(self.readonly_fields)
else:
old_str = ""

# Set up a temporary file with the initial data for editing.
new = NamedTemporaryFile(
mode="w", suffix=".yaml", delete=False, encoding="utf-8"
)
old_str = dump(old_data)
old_str += dump(old_data)
new.write(old_str)
new.close()

Expand Down Expand Up @@ -358,6 +374,12 @@ def before_choose_candidate_listener(self, session, task):

return choices

def import_begin_listener(self, session):
"""Load data from import session into self"""
self.readonly_fields = {}
for field, view in config["import"]["set_fields"].items():
self.readonly_fields[field] = view.get()
Copy link
Member

Choose a reason for hiding this comment

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

Is there a specific reason that you used a separate event handler here? It also seems like we could fetch the list of fields directly in the edit_object method.

Copy link
Author

Choose a reason for hiding this comment

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

no specific reason, just bad planning (I initially thought that the set fields were only accessible in the session object, which is out of scope in the edit_object method, and then didn't simplify the code when I found out it wasn't the case)


def importer_edit(self, session, task):
"""Callback for invoking the functionality during an interactive
import session on the *original* item tags.
Expand Down