This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
__init__.py
110 lines (80 loc) · 3.07 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from __future__ import unicode_literals
from distutils.version import StrictVersion
import functools
import logging
import os
import threading
import weakref
import cffi
if StrictVersion(cffi.__version__) < StrictVersion('0.7'):
raise RuntimeError(
'pyspotify requires cffi >= 0.7, but found %s' % cffi.__version__)
__version__ = '2.0.0a1'
# Log to nowhere by default. For details, see:
# http://docs.python.org/2/howto/logging.html#library-config
logging.getLogger('spotify').addHandler(logging.NullHandler())
# Global reentrant lock to be held whenever libspotify functions are called or
# libspotify owned data is worked on. This is the heart of pyspotify's thread
# safety.
_lock = threading.RLock()
# Mapping between keys and objects that should be kept alive as long as the key
# is alive. May be used to keep objects alive when there isn't a more
# convenient place to keep a reference to it. The keys are weakrefs, so entries
# disappear from the dict when the key is garbage collected, potentially
# causing objects associated to the key to be garbage collected as well. For
# further details, refer to the CFFI docs.
#
# TODO: Try to get rid of all use of this.
weak_key_dict = weakref.WeakKeyDictionary()
# Reference to the spotify.Session instance. Used to enforce that one and only
# one session exists in each process.
session_instance = None
def serialized(f):
"""Acquires the global lock while calling the wrapped function.
Internal function.
"""
@functools.wraps(f)
def wrapper(*args, **kwargs):
with _lock:
return f(*args, **kwargs)
return wrapper
def serialize_access_to_library(lib):
"""Modify CFFI library to serialize all calls to library functions.
Internal function.
"""
for name in dir(lib):
if name.startswith('sp_') and callable(getattr(lib, name)):
setattr(lib, name, serialized(getattr(lib, name)))
def build_ffi():
"""Build CFFI instance with knowledge of all libspotify types and a library
object which wraps libspotify for use from Python.
Internal function.
"""
header_file = os.path.join(os.path.dirname(__file__), 'api.processed.h')
header = open(header_file).read()
header += '#define SPOTIFY_API_VERSION ...\n'
ffi = cffi.FFI()
ffi.cdef(header)
lib = ffi.verify(
'#include "libspotify/api.h"',
libraries=[str('spotify')],
ext_package='spotify')
serialize_access_to_library(lib)
return ffi, lib
ffi, lib = build_ffi()
from spotify.album import * # noqa
from spotify.artist import * # noqa
from spotify.audio import * # noqa
from spotify.connection import * # noqa
from spotify.error import * # noqa
from spotify.image import * # noqa
from spotify.inbox import * # noqa
from spotify.link import * # noqa
from spotify.offline import * # noqa
from spotify.playlist import * # noqa
from spotify.search import * # noqa
from spotify.session import * # noqa
from spotify.social import * # noqa
from spotify.toplist import * # noqa
from spotify.track import * # noqa
from spotify.user import * # noqa