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

mbsubmit: cleanup and completion #1779

Merged
merged 8 commits into from
Jan 2, 2016
32 changes: 19 additions & 13 deletions beetsplug/mbsubmit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
"""Aid in submitting information to MusicBrainz.

This plugin allows the user to print track information in a format that is
parseable by the MusicBrainz track parser. Programmatic submitting is not
parseable by the MusicBrainz track parser [1]. Programmatic submitting is not
implemented by MusicBrainz yet.

[1] http://wiki.musicbrainz.org/History:How_To_Parse_Track_Listings
"""

from __future__ import (division, absolute_import, print_function,
unicode_literals)


from beets.autotag import Recommendation
from beets.importer import action
from beets.plugins import BeetsPlugin
from beets.ui.commands import PromptChoice
from beetsplug.info import print_data
Expand All @@ -35,21 +36,26 @@ class MBSubmitPlugin(BeetsPlugin):
def __init__(self):
super(MBSubmitPlugin, self).__init__()

self.config.add({
'mb_format': '$track. $title - $artist ($length)',
'threshold': 'medium',
})

# validate and store threshold
Copy link
Member

Choose a reason for hiding this comment

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

We tend to format comments as "full sentences" in beets—starting with a capital letter and ending with a period.

self.threshold = self.config['threshold'].as_choice({
'none': Recommendation.none,
'low': Recommendation.low,
'medium': Recommendation.medium,
'strong': Recommendation.strong
})

self.register_listener('before_choose_candidate',
self.before_choose_candidate_event)

def before_choose_candidate_event(self, session, task):
if not task.candidates or task.rec == Recommendation.none:
return [PromptChoice('p', 'Print tracks', self.print_tracks),
PromptChoice('k', 'print tracks and sKip',
self.print_tracks_and_skip)]
if task.rec <= self.threshold:
return [PromptChoice('p', 'Print tracks', self.print_tracks)]

# Callbacks for choices.
def print_tracks(self, session, task):
for i in task.items:
print_data(None, i, '$track. $artist - $title ($length)')

def print_tracks_and_skip(self, session, task):
for i in task.items:
print_data(None, i, '$track. $artist - $title ($length)')
return action.SKIP
print_data(None, i, self.config['mb_format'].get())
72 changes: 72 additions & 0 deletions test/test_mbsubmit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
# This file is part of beets.
# Copyright 2015, Adrian Sampson and Diego Moreda.
#
# 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.

from __future__ import (division, absolute_import, print_function,
unicode_literals)

from test._common import unittest
from test.helper import capture_stdout, control_stdin, TestHelper
from test.test_importer import ImportHelper, AutotagStub
from test.test_ui_importer import TerminalImportSessionSetup


class MBSubmitPluginTest(TerminalImportSessionSetup, unittest.TestCase,
ImportHelper, TestHelper):
def setUp(self):
self.setup_beets()
self.load_plugins('mbsubmit')
self._create_import_dir(2)
self._setup_import_session()
self.matcher = AutotagStub().install()

def tearDown(self):
self.unload_plugins()
self.teardown_beets()

def test_xxxxprint_tracks_output(self):
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 reason for the xxx in the test names?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, there is a powerful reason: my sometimes absent mind! 🔨

On a more serious note, I did notice just after submitting the pull request and already fixed it on my local copy. The current code was still meant to have a final cleanup phase on my end, that will also address the issue with the "non-full sentence" comments - sorry about that.

"""Test the output of the "print tracks" choice."""
self.matcher.matching = AutotagStub.BAD

with capture_stdout() as output:
with control_stdin('\n'.join(['p', 's'])):
# Print tracks; Skip
self.importer.run()

# manually build the string for comparing the output
tracklist = ('Print tracks? '
'01. Tag Title 1 - Tag Artist (0:01)\n'
'02. Tag Title 2 - Tag Artist (0:01)')
self.assertIn(tracklist, output.getvalue())

def test_xxprint_tracks_output_as_tracks(self):
"""Test the output of the "print tracks" choice, as singletons."""
self.matcher.matching = AutotagStub.BAD

with capture_stdout() as output:
with control_stdin('\n'.join(['t', 's', 'p', 's'])):
# as Tracks; Skip; Print tracks; Skip
self.importer.run()

# manually build the string for comparing the output
tracklist = ('Print tracks? '
'02. Tag Title 2 - Tag Artist (0:01)')
self.assertIn(tracklist, output.getvalue())


def suite():
return unittest.TestLoader().loadTestsFromName(__name__)

if __name__ == b'__main__':
unittest.main(defaultTest='suite')