utils: Create warn and ignore deprecation warning helpers

This moves all the deprecation warnings messages to a central place so that it
is easy to match against them without having to redefine the same regex all
over the place.

Each message has been given a message id which is more or less
module.func:extra-info. This is not intended to be parsed, just used in tests
when using the ignore helper.
This commit is contained in:
Thomas Adamcik 2015-03-30 21:04:56 +02:00
parent d44e8ff6f7
commit bd1e822fea
8 changed files with 101 additions and 62 deletions

View File

@ -2,7 +2,6 @@ from __future__ import absolute_import, unicode_literals
import logging
import os
import warnings
import gobject
@ -17,7 +16,7 @@ from mopidy import exceptions
from mopidy.audio import playlists, utils
from mopidy.audio.constants import PlaybackState
from mopidy.audio.listener import AudioListener
from mopidy.utils import process
from mopidy.utils import deprecation, process
logger = logging.getLogger(__name__)
@ -606,8 +605,7 @@ class Audio(pykka.ThreadingActor):
.. deprecated:: 1.0
Use :meth:`emit_data` with a :class:`None` buffer instead.
"""
warnings.warn('audio.emit_end_of_stream() is deprecated.',
DeprecationWarning)
deprecation.warn('audio.emit_end_of_stream')
self._appsrc.push(None)
def set_about_to_finish_callback(self, callback):

View File

@ -4,10 +4,12 @@ import collections
import logging
import operator
import urlparse
import warnings
import pykka
from mopidy.utils import deprecation
logger = logging.getLogger(__name__)
@ -133,7 +135,7 @@ class LibraryController(object):
.. deprecated:: 1.0
Use :meth:`search` with ``exact`` set.
"""
warnings.warn('library.find_exact() is deprecated', DeprecationWarning)
deprecation.warn('core.library.find_exact')
return self.search(query=query, uris=uris, exact=True, **kwargs)
def lookup(self, uri=None, uris=None):
@ -163,8 +165,7 @@ class LibraryController(object):
raise ValueError("One of 'uri' or 'uris' must be set")
if uri:
warnings.warn('library.lookup() "uri" argument is deprecated.',
DeprecationWarning)
deprecation.warn('core.library.lookup:uri_arg')
if uri is not None:
uris = [uri]
@ -250,14 +251,10 @@ class LibraryController(object):
query = _normalize_query(query or kwargs)
if kwargs:
warnings.warn(
'library.search() with keyword argument query is deprecated',
DeprecationWarning)
deprecation.warn('core.library.search:kwargs_query')
if not query:
warnings.warn(
'library.search() with an empty "query" argument deprecated',
DeprecationWarning)
deprecation.warn('core.library.search:empty_query')
futures = {}
for backend, backend_uris in self._get_backends_to_uris(uris).items():

View File

@ -2,12 +2,10 @@ from __future__ import absolute_import, unicode_literals
import logging
import urlparse
import warnings
from mopidy.audio import PlaybackState
from mopidy.core import listener
from mopidy.utils.deprecation import deprecated_property
from mopidy.utils import deprecation
logger = logging.getLogger(__name__)
@ -48,7 +46,7 @@ class PlaybackController(object):
"""
self._current_tl_track = value
current_tl_track = deprecated_property(get_current_tl_track)
current_tl_track = deprecation.deprecated_property(get_current_tl_track)
"""
.. deprecated:: 1.0
Use :meth:`get_current_tl_track` instead.
@ -66,7 +64,7 @@ class PlaybackController(object):
if tl_track is not None:
return tl_track.track
current_track = deprecated_property(get_current_track)
current_track = deprecation.deprecated_property(get_current_track)
"""
.. deprecated:: 1.0
Use :meth:`get_current_track` instead.
@ -103,7 +101,7 @@ class PlaybackController(object):
self._trigger_playback_state_changed(old_state, new_state)
state = deprecated_property(get_state, set_state)
state = deprecation.deprecated_property(get_state, set_state)
"""
.. deprecated:: 1.0
Use :meth:`get_state` and :meth:`set_state` instead.
@ -117,7 +115,7 @@ class PlaybackController(object):
else:
return 0
time_position = deprecated_property(get_time_position)
time_position = deprecation.deprecated_property(get_time_position)
"""
.. deprecated:: 1.0
Use :meth:`get_time_position` instead.
@ -129,8 +127,7 @@ class PlaybackController(object):
Use :meth:`core.mixer.get_volume()
<mopidy.core.MixerController.get_volume>` instead.
"""
warnings.warn(
'playback.get_volume() is deprecated', DeprecationWarning)
deprecation.warn('core.playback.get_volume')
return self.core.mixer.get_volume()
def set_volume(self, volume):
@ -139,11 +136,10 @@ class PlaybackController(object):
Use :meth:`core.mixer.set_volume()
<mopidy.core.MixerController.set_volume>` instead.
"""
warnings.warn(
'playback.set_volume() is deprecated', DeprecationWarning)
deprecation.warn('core.playback.set_volume')
return self.core.mixer.set_volume(volume)
volume = deprecated_property(get_volume, set_volume)
volume = deprecation.deprecated_property(get_volume, set_volume)
"""
.. deprecated:: 1.0
Use :meth:`core.mixer.get_volume()
@ -158,7 +154,7 @@ class PlaybackController(object):
Use :meth:`core.mixer.get_mute()
<mopidy.core.MixerController.get_mute>` instead.
"""
warnings.warn('playback.get_mute() is deprecated', DeprecationWarning)
deprecation.warn('core.playback.get_mute')
return self.core.mixer.get_mute()
def set_mute(self, mute):
@ -167,10 +163,10 @@ class PlaybackController(object):
Use :meth:`core.mixer.set_mute()
<mopidy.core.MixerController.set_mute>` instead.
"""
warnings.warn('playback.set_mute() is deprecated', DeprecationWarning)
deprecation.warn('core.playback.set_mute')
return self.core.mixer.set_mute(mute)
mute = deprecated_property(get_mute, set_mute)
mute = deprecation.deprecated_property(get_mute, set_mute)
"""
.. deprecated:: 1.0
Use :meth:`core.mixer.get_mute()

View File

@ -2,14 +2,12 @@ from __future__ import absolute_import, unicode_literals
import logging
import urlparse
import warnings
import pykka
from mopidy.core import listener
from mopidy.models import Playlist
from mopidy.utils.deprecation import deprecated_property
from mopidy.utils import deprecation
logger = logging.getLogger(__name__)
@ -81,8 +79,7 @@ class PlaylistsController(object):
.. deprecated:: 1.0
Use :meth:`as_list` and :meth:`get_items` instead.
"""
warnings.warn(
'playlists.get_playlists() is deprecated', DeprecationWarning)
deprecation.warn('core.playlists.get_playlists')
playlist_refs = self.as_list()
@ -97,7 +94,7 @@ class PlaylistsController(object):
return [
Playlist(uri=r.uri, name=r.name) for r in playlist_refs]
playlists = deprecated_property(get_playlists)
playlists = deprecation.deprecated_property(get_playlists)
"""
.. deprecated:: 1.0
Use :meth:`as_list` and :meth:`get_items` instead.
@ -170,7 +167,7 @@ class PlaylistsController(object):
.. deprecated:: 1.0
Use :meth:`as_list` and filter yourself.
"""
warnings.warn('playlists.filter() is deprecated', DeprecationWarning)
deprecation.warn('core.playlists.filter')
criteria = criteria or kwargs
matches = self.playlists

View File

@ -3,13 +3,11 @@ from __future__ import absolute_import, unicode_literals
import collections
import logging
import random
import warnings
from mopidy import compat
from mopidy.core import listener
from mopidy.models import TlTrack
from mopidy.utils.deprecation import deprecated_property
from mopidy.utils import deprecation
logger = logging.getLogger(__name__)
@ -31,7 +29,7 @@ class TracklistController(object):
"""Get tracklist as list of :class:`mopidy.models.TlTrack`."""
return self._tl_tracks[:]
tl_tracks = deprecated_property(get_tl_tracks)
tl_tracks = deprecation.deprecated_property(get_tl_tracks)
"""
.. deprecated:: 1.0
Use :meth:`get_tl_tracks` instead.
@ -41,7 +39,7 @@ class TracklistController(object):
"""Get tracklist as list of :class:`mopidy.models.Track`."""
return [tl_track.track for tl_track in self._tl_tracks]
tracks = deprecated_property(get_tracks)
tracks = deprecation.deprecated_property(get_tracks)
"""
.. deprecated:: 1.0
Use :meth:`get_tracks` instead.
@ -51,7 +49,7 @@ class TracklistController(object):
"""Get length of the tracklist."""
return len(self._tl_tracks)
length = deprecated_property(get_length)
length = deprecation.deprecated_property(get_length)
"""
.. deprecated:: 1.0
Use :meth:`get_length` instead.
@ -71,7 +69,7 @@ class TracklistController(object):
self.core.playback._on_tracklist_change()
self._trigger_tracklist_changed()
version = deprecated_property(get_version)
version = deprecation.deprecated_property(get_version)
"""
.. deprecated:: 1.0
Use :meth:`get_version` instead.
@ -99,7 +97,7 @@ class TracklistController(object):
self._trigger_options_changed()
return setattr(self, '_consume', value)
consume = deprecated_property(get_consume, set_consume)
consume = deprecation.deprecated_property(get_consume, set_consume)
"""
.. deprecated:: 1.0
Use :meth:`get_consume` and :meth:`set_consume` instead.
@ -131,7 +129,7 @@ class TracklistController(object):
random.shuffle(self._shuffled)
return setattr(self, '_random', value)
random = deprecated_property(get_random, set_random)
random = deprecation.deprecated_property(get_random, set_random)
"""
.. deprecated:: 1.0
Use :meth:`get_random` and :meth:`set_random` instead.
@ -164,7 +162,7 @@ class TracklistController(object):
self._trigger_options_changed()
return setattr(self, '_repeat', value)
repeat = deprecated_property(get_repeat, set_repeat)
repeat = deprecation.deprecated_property(get_repeat, set_repeat)
"""
.. deprecated:: 1.0
Use :meth:`get_repeat` and :meth:`set_repeat` instead.
@ -194,7 +192,7 @@ class TracklistController(object):
self._trigger_options_changed()
return setattr(self, '_single', value)
single = deprecated_property(get_single, set_single)
single = deprecation.deprecated_property(get_single, set_single)
"""
.. deprecated:: 1.0
Use :meth:`get_single` and :meth:`set_single` instead.
@ -336,12 +334,10 @@ class TracklistController(object):
# TODO: assert that tracks are track instances
if tracks:
warnings.warn('tracklist.add() "tracks" argument is deprecated.',
DeprecationWarning)
deprecation.warn('core.tracklist.add:tracks_arg')
if uri:
warnings.warn('tracklist.add() "uri" argument is deprecated.',
DeprecationWarning)
deprecation.warn('core.tracklist.add:uri_arg')
if tracks is None:
if uri is not None:

View File

@ -1,8 +1,7 @@
from __future__ import absolute_import, unicode_literals
import warnings
from mopidy.mpd import exceptions, protocol, translator
from mopidy.utils import deprecation
@protocol.commands.add('add')
@ -163,8 +162,7 @@ def playlist(context):
Do not use this, instead use ``playlistinfo``.
"""
warnings.warn(
'Do not use this, instead use playlistinfo', DeprecationWarning)
deprecation.warn('mpd.protocol.current_playlist.playlist')
return playlistinfo(context)
@ -354,8 +352,7 @@ def swap(context, songpos1, songpos2):
# TODO: do we need a tracklist.replace()
context.core.tracklist.clear()
with warnings.catch_warnings():
warnings.filterwarnings('ignore', 'tracklist.add.*"tracks".*')
with deprecation.ignore('core.tracklist.add:tracks_arg'):
context.core.tracklist.add(tracks=tracks).get()

View File

@ -1,9 +1,8 @@
from __future__ import absolute_import, unicode_literals
import warnings
from mopidy.core import PlaybackState
from mopidy.mpd import exceptions, protocol
from mopidy.utils import deprecation
@protocol.commands.add('consume', state=protocol.BOOL)
@ -134,9 +133,7 @@ def pause(context, state=None):
- Calls ``pause`` without any arguments to toogle pause.
"""
if state is None:
warnings.warn(
'The use of pause command w/o the PAUSE argument is deprecated.',
DeprecationWarning)
deprecation.warn('mpd.protocol.playback.pause:state_arg')
if (context.core.playback.state.get() == PlaybackState.PLAYING):
context.core.playback.pause()

View File

@ -1,5 +1,66 @@
from __future__ import unicode_literals
import contextlib
import re
import warnings
# Messages used in deprecation warnings are collected here so we can target
# them easily when ignoring warnings.
_MESSAGES = {
# Deprecated features mpd:
'mpd.protocol.playback.pause:state_arg':
'The use of pause command w/o the PAUSE argument is deprecated.',
'mpd.protocol.current_playlist.playlist':
'Do not use this, instead use playlistinfo',
# Deprecated features in audio:
'audio.emit_end_of_stream': 'audio.emit_end_of_stream() is deprecated',
# Deprecated features in core libary:
'core.library.find_exact': 'library.find_exact() is deprecated',
'core.library.lookup:uri_arg':
'library.lookup() "uri" argument is deprecated',
'core.library.search:kwargs_query':
'library.search() with keyword argument query is deprecated',
'core.library.search:empty_query':
'library.search() with an empty "query" argument deprecated',
# Deprecated features in core playback:
'core.playback.get_mute': 'playback.get_mute() is deprecated',
'core.playback.set_mute': 'playback.set_mute() is deprecated',
'core.playback.get_volume': 'playback.get_volume() is deprecated',
'core.playback.set_volume': 'playback.set_volume() is deprecated',
# Deprecated features in core playlists:
'core.playlists.filter': 'playlists.filter() is deprecated',
'core.playlists.get_playlists': 'playlists.get_playlists() is deprecated',
# Deprecated features in core tracklist:
'core.tracklist.add:tracks_arg':
'tracklist.add() "tracks" argument is deprecated',
'core.tracklist.add:uri_arg':
'tracklist.add() "uri" argument is deprecated',
}
def warn(msg_id):
warnings.warn(_MESSAGES.get(msg_id, msg_id), DeprecationWarning)
@contextlib.contextmanager
def ignore(ids=None):
with warnings.catch_warnings():
if isinstance(ids, basestring):
ids = [ids]
if ids:
for msg_id in ids:
msg = re.escape(_MESSAGES.get(msg_id, msg_id))
warnings.filterwarnings('ignore', msg, DeprecationWarning)
else:
warnings.filterwarnings('ignore', category=DeprecationWarning)
yield
def deprecated_property(
getter=None, setter=None, message='Property is deprecated'):