From de84fdfef46bfc3bd5de953a3ed17f9051adf30f Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Mon, 29 Apr 2013 19:52:29 +0200 Subject: [PATCH] Remove hacks required by #302 to run on older Python 2.6.x --- mopidy/__main__.py | 16 ++++------ mopidy/backends/local/translator.py | 39 +++++++++++------------ mopidy/frontends/mpd/actor.py | 6 ++-- mopidy/frontends/mpd/protocol/__init__.py | 7 +--- mopidy/frontends/mpd/translator.py | 4 +-- mopidy/models.py | 38 +++++++--------------- mopidy/scanner.py | 23 ++++++------- tests/scanner_test.py | 36 ++++++++++----------- tests/utils/jsonrpc_test.py | 4 +-- 9 files changed, 67 insertions(+), 106 deletions(-) diff --git a/mopidy/__main__.py b/mopidy/__main__.py index bc5f248d..33f23269 100644 --- a/mopidy/__main__.py +++ b/mopidy/__main__.py @@ -140,35 +140,33 @@ def parse_options(): optparse.Option.TYPES += ('config_override',) optparse.Option.TYPE_CHECKER['config_override'] = check_config_override - # NOTE First argument to add_option must be bytestrings on Python < 2.6.2 - # See https://github.com/mopidy/mopidy/issues/302 for details parser.add_option( - b'-q', '--quiet', + '-q', '--quiet', action='store_const', const=0, dest='verbosity_level', help='less output (warning level)') parser.add_option( - b'-v', '--verbose', + '-v', '--verbose', action='count', default=1, dest='verbosity_level', help='more output (debug level)') parser.add_option( - b'--save-debug-log', + '--save-debug-log', action='store_true', dest='save_debug_log', help='save debug log to "./mopidy.log"') parser.add_option( - b'--show-config', + '--show-config', action='callback', callback=show_config_callback, help='show current config') parser.add_option( - b'--show-deps', + '--show-deps', action='callback', callback=deps.show_deps_optparse_callback, help='show dependencies and their versions') parser.add_option( - b'--config', + '--config', action='store', dest='config', default=b'$XDG_CONFIG_DIR/mopidy/mopidy.conf', help='config files to use, colon seperated, later files override') parser.add_option( - b'-o', b'--option', + '-o', '--option', action='append', dest='overrides', type='config_override', help='`section/key=value` values to override config options') return parser.parse_args(args=mopidy_args)[0] diff --git a/mopidy/backends/local/translator.py b/mopidy/backends/local/translator.py index c53011f6..d2ee0d45 100644 --- a/mopidy/backends/local/translator.py +++ b/mopidy/backends/local/translator.py @@ -100,9 +100,6 @@ def _convert_mpd_data(data, tracks, music_dir): if not data: return - # NOTE kwargs dict keys must be bytestrings to work on Python < 2.6.5 - # See https://github.com/mopidy/mopidy/issues/302 for details. - track_kwargs = {} album_kwargs = {} artist_kwargs = {} @@ -110,51 +107,51 @@ def _convert_mpd_data(data, tracks, music_dir): if 'track' in data: if '/' in data['track']: - album_kwargs[b'num_tracks'] = int(data['track'].split('/')[1]) - track_kwargs[b'track_no'] = int(data['track'].split('/')[0]) + album_kwargs['num_tracks'] = int(data['track'].split('/')[1]) + track_kwargs['track_no'] = int(data['track'].split('/')[0]) else: - track_kwargs[b'track_no'] = int(data['track']) + track_kwargs['track_no'] = int(data['track']) if 'artist' in data: - artist_kwargs[b'name'] = data['artist'] - albumartist_kwargs[b'name'] = data['artist'] + artist_kwargs['name'] = data['artist'] + albumartist_kwargs['name'] = data['artist'] if 'albumartist' in data: - albumartist_kwargs[b'name'] = data['albumartist'] + albumartist_kwargs['name'] = data['albumartist'] if 'album' in data: - album_kwargs[b'name'] = data['album'] + album_kwargs['name'] = data['album'] if 'title' in data: - track_kwargs[b'name'] = data['title'] + track_kwargs['name'] = data['title'] if 'date' in data: - track_kwargs[b'date'] = data['date'] + track_kwargs['date'] = data['date'] if 'musicbrainz_trackid' in data: - track_kwargs[b'musicbrainz_id'] = data['musicbrainz_trackid'] + track_kwargs['musicbrainz_id'] = data['musicbrainz_trackid'] if 'musicbrainz_albumid' in data: - album_kwargs[b'musicbrainz_id'] = data['musicbrainz_albumid'] + album_kwargs['musicbrainz_id'] = data['musicbrainz_albumid'] if 'musicbrainz_artistid' in data: - artist_kwargs[b'musicbrainz_id'] = data['musicbrainz_artistid'] + artist_kwargs['musicbrainz_id'] = data['musicbrainz_artistid'] if 'musicbrainz_albumartistid' in data: - albumartist_kwargs[b'musicbrainz_id'] = ( + albumartist_kwargs['musicbrainz_id'] = ( data['musicbrainz_albumartistid']) if artist_kwargs: artist = Artist(**artist_kwargs) - track_kwargs[b'artists'] = [artist] + track_kwargs['artists'] = [artist] if albumartist_kwargs: albumartist = Artist(**albumartist_kwargs) - album_kwargs[b'artists'] = [albumartist] + album_kwargs['artists'] = [albumartist] if album_kwargs: album = Album(**album_kwargs) - track_kwargs[b'album'] = album + track_kwargs['album'] = album if data['file'][0] == '/': path = data['file'][1:] @@ -167,9 +164,9 @@ def _convert_mpd_data(data, tracks, music_dir): # Make sure we only pass bytestrings to path_to_uri to avoid implicit # decoding of bytestrings to unicode strings - track_kwargs[b'uri'] = path_to_uri(music_dir, path) + track_kwargs['uri'] = path_to_uri(music_dir, path) - track_kwargs[b'length'] = int(data.get('time', 0)) * 1000 + track_kwargs['length'] = int(data.get('time', 0)) * 1000 track = Track(**track_kwargs) tracks.add(track) diff --git a/mopidy/frontends/mpd/actor.py b/mopidy/frontends/mpd/actor.py index 45ed753e..f1fefae4 100644 --- a/mopidy/frontends/mpd/actor.py +++ b/mopidy/frontends/mpd/actor.py @@ -18,15 +18,13 @@ class MpdFrontend(pykka.ThreadingActor, CoreListener): hostname = network.format_hostname(config['mpd']['hostname']) port = config['mpd']['port'] - # NOTE kwargs dict keys must be bytestrings to work on Python < 2.6.5 - # See https://github.com/mopidy/mopidy/issues/302 for details. try: network.Server( hostname, port, protocol=session.MpdSession, protocol_kwargs={ - b'config': config, - b'core': core, + 'config': config, + 'core': core, }, max_connections=config['mpd']['max_connections'], timeout=config['mpd']['connection_timeout']) diff --git a/mopidy/frontends/mpd/protocol/__init__.py b/mopidy/frontends/mpd/protocol/__init__.py index 2b2260ef..1827624b 100644 --- a/mopidy/frontends/mpd/protocol/__init__.py +++ b/mopidy/frontends/mpd/protocol/__init__.py @@ -56,12 +56,7 @@ def handle_request(pattern, auth_required=True): if match is not None: mpd_commands.add( MpdCommand(name=match.group(), auth_required=auth_required)) - # NOTE Make pattern a bytestring to get bytestring keys in the dict - # returned from matches.groupdict(), which is again used as a **kwargs - # dict. This is needed to work on Python < 2.6.5. - # See https://github.com/mopidy/mopidy/issues/302 for details. - bytestring_pattern = pattern.encode('utf-8') - compiled_pattern = re.compile(bytestring_pattern, flags=re.UNICODE) + compiled_pattern = re.compile(pattern, flags=re.UNICODE) if compiled_pattern in request_handlers: raise ValueError('Tried to redefine handler for %s with %s' % ( pattern, func)) diff --git a/mopidy/frontends/mpd/translator.py b/mopidy/frontends/mpd/translator.py index e164883d..b646732f 100644 --- a/mopidy/frontends/mpd/translator.py +++ b/mopidy/frontends/mpd/translator.py @@ -139,8 +139,6 @@ def query_from_mpd_list_format(field, mpd_query): """ Converts an MPD ``list`` query to a Mopidy query. """ - # NOTE kwargs dict keys must be bytestrings to work on Python < 2.6.5 - # See https://github.com/mopidy/mopidy/issues/302 for details if mpd_query is None: return {} try: @@ -156,7 +154,7 @@ def query_from_mpd_list_format(field, mpd_query): if field == 'album': if not tokens[0]: raise ValueError - return {b'artist': [tokens[0]]} # See above NOTE + return {'artist': [tokens[0]]} # See above NOTE else: raise MpdArgError( 'should be "Album" for 3 arguments', command='list') diff --git a/mopidy/models.py b/mopidy/models.py index 4020febb..d138b490 100644 --- a/mopidy/models.py +++ b/mopidy/models.py @@ -66,15 +66,13 @@ class ImmutableObject(object): :type values: dict :rtype: new instance of the model being copied """ - # NOTE kwargs dict keys must be bytestrings to work on Python < 2.6.5 - # See https://github.com/mopidy/mopidy/issues/302 for details data = {} for key in self.__dict__.keys(): public_key = key.lstrip('_') - data[str(public_key)] = values.pop(public_key, self.__dict__[key]) + data[public_key] = values.pop(public_key, self.__dict__[key]) for key in values.keys(): if hasattr(self, key): - data[str(key)] = values.pop(key) + data[key] = values.pop(key) if values: raise TypeError( 'copy() got an unexpected keyword argument "%s"' % key) @@ -127,15 +125,13 @@ def model_json_decoder(dct): {u'a_track': Track(artists=[], name=u'name')} """ - # NOTE kwargs dict keys must be bytestrings to work on Python < 2.6.5 - # See https://github.com/mopidy/mopidy/issues/302 for details. if '__model__' in dct: model_name = dct.pop('__model__') cls = globals().get(model_name, None) if issubclass(cls, ImmutableObject): kwargs = {} for key, value in dct.items(): - kwargs[str(key)] = value + kwargs[key] = value return cls(**kwargs) return dct @@ -208,10 +204,8 @@ class Album(ImmutableObject): # actual usage of this field with more than one image. def __init__(self, *args, **kwargs): - # NOTE kwargs dict keys must be bytestrings to work on Python < 2.6.5 - # See https://github.com/mopidy/mopidy/issues/302 for details - self.__dict__[b'artists'] = frozenset(kwargs.pop('artists', [])) - self.__dict__[b'images'] = frozenset(kwargs.pop('images', [])) + self.__dict__['artists'] = frozenset(kwargs.pop('artists', [])) + self.__dict__['images'] = frozenset(kwargs.pop('images', [])) super(Album, self).__init__(*args, **kwargs) @@ -270,9 +264,7 @@ class Track(ImmutableObject): musicbrainz_id = None def __init__(self, *args, **kwargs): - # NOTE kwargs dict keys must be bytestrings to work on Python < 2.6.5 - # See https://github.com/mopidy/mopidy/issues/302 for details - self.__dict__[b'artists'] = frozenset(kwargs.pop('artists', [])) + self.__dict__['artists'] = frozenset(kwargs.pop('artists', [])) super(Track, self).__init__(*args, **kwargs) @@ -304,11 +296,9 @@ class TlTrack(ImmutableObject): track = None def __init__(self, *args, **kwargs): - # NOTE kwargs dict keys must be bytestrings to work on Python < 2.6.5 - # See https://github.com/mopidy/mopidy/issues/302 for details if len(args) == 2 and len(kwargs) == 0: - kwargs[b'tlid'] = args[0] - kwargs[b'track'] = args[1] + kwargs['tlid'] = args[0] + kwargs['track'] = args[1] args = [] super(TlTrack, self).__init__(*args, **kwargs) @@ -343,9 +333,7 @@ class Playlist(ImmutableObject): last_modified = None def __init__(self, *args, **kwargs): - # NOTE kwargs dict keys must be bytestrings to work on Python < 2.6.5 - # See https://github.com/mopidy/mopidy/issues/302 for details - self.__dict__[b'tracks'] = tuple(kwargs.pop('tracks', [])) + self.__dict__['tracks'] = tuple(kwargs.pop('tracks', [])) super(Playlist, self).__init__(*args, **kwargs) # TODO: def insert(self, pos, track): ... ? @@ -381,9 +369,7 @@ class SearchResult(ImmutableObject): albums = tuple() def __init__(self, *args, **kwargs): - # NOTE kwargs dict keys must be bytestrings to work on Python < 2.6.5 - # See https://github.com/mopidy/mopidy/issues/302 for details - self.__dict__[b'tracks'] = tuple(kwargs.pop('tracks', [])) - self.__dict__[b'artists'] = tuple(kwargs.pop('artists', [])) - self.__dict__[b'albums'] = tuple(kwargs.pop('albums', [])) + self.__dict__['tracks'] = tuple(kwargs.pop('tracks', [])) + self.__dict__['artists'] = tuple(kwargs.pop('artists', [])) + self.__dict__['albums'] = tuple(kwargs.pop('albums', [])) super(SearchResult, self).__init__(*args, **kwargs) diff --git a/mopidy/scanner.py b/mopidy/scanner.py index fc80f95b..61257add 100644 --- a/mopidy/scanner.py +++ b/mopidy/scanner.py @@ -90,14 +90,12 @@ def main(): def parse_options(): parser = optparse.OptionParser( version='Mopidy %s' % versioning.get_version()) - # NOTE First argument to add_option must be bytestrings on Python < 2.6.2 - # See https://github.com/mopidy/mopidy/issues/302 for details parser.add_option( - b'-q', '--quiet', + '-q', '--quiet', action='store_const', const=0, dest='verbosity_level', help='less output (warning level)') parser.add_option( - b'-v', '--verbose', + '-v', '--verbose', action='count', default=1, dest='verbosity_level', help='more output (debug level)') return parser.parse_args(args=mopidy_args)[0] @@ -109,12 +107,9 @@ def translator(data): artist_kwargs = {} track_kwargs = {} - # NOTE kwargs dict keys must be bytestrings to work on Python < 2.6.5 - # See https://github.com/mopidy/mopidy/issues/302 for details. - def _retrieve(source_key, target_key, target): if source_key in data: - target[str(target_key)] = data[source_key] + target[target_key] = data[source_key] _retrieve(gst.TAG_ALBUM, 'name', album_kwargs) _retrieve(gst.TAG_TRACK_COUNT, 'num_tracks', album_kwargs) @@ -127,7 +122,7 @@ def translator(data): except ValueError: pass # Ignore invalid dates else: - track_kwargs[b'date'] = date.isoformat() + track_kwargs['date'] = date.isoformat() _retrieve(gst.TAG_TITLE, 'name', track_kwargs) _retrieve(gst.TAG_TRACK_NUMBER, 'track_no', track_kwargs) @@ -141,12 +136,12 @@ def translator(data): 'musicbrainz-albumartistid', 'musicbrainz_id', albumartist_kwargs) if albumartist_kwargs: - album_kwargs[b'artists'] = [Artist(**albumartist_kwargs)] + album_kwargs['artists'] = [Artist(**albumartist_kwargs)] - track_kwargs[b'uri'] = data['uri'] - track_kwargs[b'length'] = data[gst.TAG_DURATION] - track_kwargs[b'album'] = Album(**album_kwargs) - track_kwargs[b'artists'] = [Artist(**artist_kwargs)] + track_kwargs['uri'] = data['uri'] + track_kwargs['length'] = data[gst.TAG_DURATION] + track_kwargs['album'] = Album(**album_kwargs) + track_kwargs['artists'] = [Artist(**artist_kwargs)] return Track(**track_kwargs) diff --git a/tests/scanner_test.py b/tests/scanner_test.py index 63383806..75fc60fd 100644 --- a/tests/scanner_test.py +++ b/tests/scanner_test.py @@ -34,40 +34,36 @@ class TranslatorTest(unittest.TestCase): 'musicbrainz-albumartistid': 'mbalbumartistid', } - # NOTE: kwargs are explicitly made bytestrings to work on Python - # 2.6.0/2.6.1. See https://github.com/mopidy/mopidy/issues/302 for - # details. - self.album = { - b'name': 'albumname', - b'num_tracks': 2, - b'musicbrainz_id': 'mbalbumid', + 'name': 'albumname', + 'num_tracks': 2, + 'musicbrainz_id': 'mbalbumid', } self.artist = { - b'name': 'name', - b'musicbrainz_id': 'mbartistid', + 'name': 'name', + 'musicbrainz_id': 'mbartistid', } self.albumartist = { - b'name': 'albumartistname', - b'musicbrainz_id': 'mbalbumartistid', + 'name': 'albumartistname', + 'musicbrainz_id': 'mbalbumartistid', } self.track = { - b'uri': 'uri', - b'name': 'trackname', - b'date': '2006-01-01', - b'track_no': 1, - b'length': 4531, - b'musicbrainz_id': 'mbtrackid', + 'uri': 'uri', + 'name': 'trackname', + 'date': '2006-01-01', + 'track_no': 1, + 'length': 4531, + 'musicbrainz_id': 'mbtrackid', } def build_track(self): if self.albumartist: - self.album[b'artists'] = [Artist(**self.albumartist)] - self.track[b'album'] = Album(**self.album) - self.track[b'artists'] = [Artist(**self.artist)] + self.album['artists'] = [Artist(**self.albumartist)] + self.track['album'] = Album(**self.album) + self.track['artists'] = [Artist(**self.artist)] return Track(**self.track) def check(self): diff --git a/tests/utils/jsonrpc_test.py b/tests/utils/jsonrpc_test.py index f60bdccd..5dccbe05 100644 --- a/tests/utils/jsonrpc_test.py +++ b/tests/utils/jsonrpc_test.py @@ -218,12 +218,10 @@ class JsonRpcSingleCommandTest(JsonRpcTestBase): self.assertEqual(self.core.playback.get_volume().get(), 37) def test_call_methods_with_named_params(self): - # NOTE kwargs dict keys must be bytestrings to work on Python < 2.6.5 - # See https://github.com/mopidy/mopidy/issues/302 for details. request = { 'jsonrpc': '2.0', 'method': 'core.playback.set_volume', - 'params': {b'volume': 37}, + 'params': {'volume': 37}, 'id': 1, } response = self.jrw.handle_data(request)