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

Selective field updates #2231

Merged
merged 9 commits into from
Oct 23, 2016
Merged

Selective field updates #2231

merged 9 commits into from
Oct 23, 2016

Conversation

dangmai
Copy link
Contributor

@dangmai dangmai commented Oct 21, 2016

PR to fix #2229

@@ -1190,6 +1196,10 @@ def update_func(lib, opts, args):
u'-p', u'--pretend', action='store_true',
help=u"show all changes but do nothing"
)
update_cmd.parser.add_option(
u'-F', u'--field', default=None, action='append', dest='fields',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I use -F here instead of -f since the latter is already used by optparse for custom path formatting. If you want it to be something else, I'd be cool with it.

Copy link
Member

@sampsyo sampsyo left a comment

Choose a reason for hiding this comment

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

-F seems like a great spelling! Thanks for adding to the docs and tests. ✨

I have a couple of cosmetic suggestions and one substantive one: I'm not sure we need the new parameter to the move method.

@@ -342,15 +342,17 @@ def __delattr__(self, key):

# Database interaction (CRUD methods).

def store(self):
def store(self, fields_to_store=None):
"""Save the object's metadata into the library database.
"""
Copy link
Member

Choose a reason for hiding this comment

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

This is just cosmetic, but I would prefer to use (a) a simpler parameter name like fields and (b) documentation in the docstring here to explain exactly what it does.

"""Save the object's metadata into the library database.
"""
self._check_db()

# Build assignments for query.
assignments = []
subvars = []
for key in self._fields:
if fields_to_store is None:
fields_to_store = self._fields
Copy link
Member

Choose a reason for hiding this comment

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

This is again just cosmetic, but I like to put these default-value if blocks to the very top of the function to make it clear what the default values are.

def store(self):
super(LibModel, self).store()
def store(self, fields_to_store=None):
super(LibModel, self).store(fields_to_store)
Copy link
Member

Choose a reason for hiding this comment

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

(The parameter name should be consistent with the above, if it changes.)

@@ -729,7 +729,8 @@ def remove(self, delete=False, with_album=True):

self._db._memotable = {}

def move(self, copy=False, link=False, basedir=None, with_album=True):
def move(self, copy=False, link=False, basedir=None, with_album=True,
fields_to_store=None):
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure move needs the new parameter—it's a little unintuitive for clients, and the only reason we store here is to update the paths for files.

@@ -1000,7 +1001,7 @@ def move_art(self, copy=False, link=False):
util.prune_dirs(os.path.dirname(old_art),
self._db.directory)

def move(self, copy=False, link=False, basedir=None):
def move(self, copy=False, link=False, basedir=None, fields_to_store=None):
Copy link
Member

Choose a reason for hiding this comment

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

(As above.)

@@ -1108,7 +1110,7 @@ def set_art(self, path, copy=True):

plugins.send('art_set', album=self)

def store(self):
def store(self, fields_to_store=None):
"""Update the database with the album information. The album's
tracks are also updated.
"""
Copy link
Member

Choose a reason for hiding this comment

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

(This probably also needs the name change and docstring expansion.)

@@ -1081,11 +1081,16 @@ def list_func(lib, opts, args):

# update: Update library contents according to on-disk tags.

def update_items(lib, query, album, move, pretend):
def update_items(lib, query, album, move, pretend, fields):
"""For all the items matched by the query, update the library to
reflect the item's embedded tags.
Copy link
Member

Choose a reason for hiding this comment

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

fields needs a mention in the docstring.

if move and fields is not None and 'path' not in fields:
# Special case: if an item needs to be moved, the path field has to
# updated; otherwise the new path will not be reflected in the
# database
Copy link
Member

Choose a reason for hiding this comment

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

Missing a period in this comment.


# Save changes.
if not pretend:
if changed:
# Move the item if it's in the library.
if move and lib.directory in ancestry(item.path):
item.move()
item.move(fields_to_store=fields)
Copy link
Member

Choose a reason for hiding this comment

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

Because we're about to call item.store, maybe the right thing to do here is just to disable the redundant store to the database inside item.move.

@dangmai
Copy link
Contributor Author

dangmai commented Oct 21, 2016

I've pushed a fix for the small cosmetic changes you asked for. The fields param to the move method is a little bit more tricky and I'll (hopefully) tackle it this weekend. My first thought is to specify a store=True argument to move, and if it's False we won't do the automatic storing in that method. Then we can manually handle the storing in the update_items instead. And since it has a default value of True, the other part of beets that depend on that method won't be affected.

Thoughts?

@sampsyo
Copy link
Member

sampsyo commented Oct 23, 2016

Fantastic; thank you! I think the store=True argument to move makes the changes there much simpler. I'll merge this now.

Nice work! ✨

@sampsyo sampsyo merged commit 05377ee into beetbox:master Oct 23, 2016
sampsyo added a commit that referenced this pull request Oct 23, 2016
sampsyo added a commit that referenced this pull request Oct 23, 2016
@dangmai dangmai deleted the selective-update branch October 23, 2016 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

beet update for selective fields
2 participants