mopidy / mopidy
1 2
import contextlib
2 2
import re
3 2
import warnings
4

5
# Messages used in deprecation warnings are collected here so we can target
6
# them easily when ignoring warnings.
7 2
_MESSAGES = {
8
    # Deprecated features in core playback:
9
    "core.playback.play:tl_track_kwargs": (
10
        'playback.play() with "tl_track" argument is pending deprecation use '
11
        '"tlid" instead'
12
    ),
13
    # Deprecated features in core tracklist:
14
    "core.tracklist.add:tracks_arg": (
15
        'tracklist.add() "tracks" argument is deprecated'
16
    ),
17
    "core.tracklist.eot_track": (
18
        "tracklist.eot_track() is pending deprecation, use "
19
        "tracklist.get_eot_tlid()"
20
    ),
21
    "core.tracklist.next_track": (
22
        "tracklist.next_track() is pending deprecation, use "
23
        "tracklist.get_next_tlid()"
24
    ),
25
    "core.tracklist.previous_track": (
26
        "tracklist.previous_track() is pending deprecation, use "
27
        "tracklist.get_previous_tlid()"
28
    ),
29
}
30

31

32 2
def warn(msg_id, pending=False):
33 2
    if pending:
34 0
        category = PendingDeprecationWarning
35
    else:
36 2
        category = DeprecationWarning
37 2
    warnings.warn(_MESSAGES.get(msg_id, msg_id), category)
38

39

40 2
@contextlib.contextmanager
41 2
def ignore(ids=None):
42 2
    with warnings.catch_warnings():
43 2
        if isinstance(ids, str):
44 0
            ids = [ids]
45

46 2
        if ids:
47 0
            for msg_id in ids:
48 0
                msg = re.escape(_MESSAGES.get(msg_id, msg_id))
49 0
                warnings.filterwarnings("ignore", msg, DeprecationWarning)
50
        else:
51 2
            warnings.filterwarnings("ignore", category=DeprecationWarning)
52 2
        yield

Read our documentation on viewing source code .

Loading