soco 0.31.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- soco/__init__.py +37 -0
- soco/alarms.py +603 -0
- soco/cache.py +213 -0
- soco/config.py +90 -0
- soco/core.py +3114 -0
- soco/data_structure_quirks.py +42 -0
- soco/data_structures.py +1323 -0
- soco/data_structures_entry.py +51 -0
- soco/discovery.py +767 -0
- soco/events.py +487 -0
- soco/events_asyncio.py +701 -0
- soco/events_base.py +780 -0
- soco/events_twisted.py +571 -0
- soco/exceptions.py +145 -0
- soco/groups.py +200 -0
- soco/ms_data_structures.py +682 -0
- soco/music_library.py +661 -0
- soco/music_services/__init__.py +8 -0
- soco/music_services/accounts.py +192 -0
- soco/music_services/data_structures.py +446 -0
- soco/music_services/music_service.py +1003 -0
- soco/music_services/token_store.py +108 -0
- soco/plugins/__init__.py +45 -0
- soco/plugins/example.py +44 -0
- soco/plugins/plex.py +209 -0
- soco/plugins/sharelink.py +285 -0
- soco/plugins/spotify.py +32 -0
- soco/plugins/wimp.py +525 -0
- soco/services.py +958 -0
- soco/snapshot.py +301 -0
- soco/soap.py +330 -0
- soco/utils.py +200 -0
- soco/xml.py +79 -0
- soco/zonegroupstate.py +400 -0
- soco-0.31.1.dist-info/METADATA +285 -0
- soco-0.31.1.dist-info/RECORD +40 -0
- soco-0.31.1.dist-info/WHEEL +5 -0
- soco-0.31.1.dist-info/licenses/AUTHORS.rst +39 -0
- soco-0.31.1.dist-info/licenses/LICENSE.rst +22 -0
- soco-0.31.1.dist-info/top_level.txt +1 -0
soco/cache.py
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# pylint: disable=not-context-manager,useless-object-inheritance
|
|
2
|
+
|
|
3
|
+
# NOTE: The pylint not-content-manager warning is disabled pending the fix of
|
|
4
|
+
# a bug in pylint https://github.com/PyCQA/pylint/issues/782
|
|
5
|
+
|
|
6
|
+
# NOTE: useless-object-inheritance needed for Python 2.x compatability
|
|
7
|
+
|
|
8
|
+
"""This module contains the classes underlying SoCo's caching system."""
|
|
9
|
+
|
|
10
|
+
import threading
|
|
11
|
+
from pickle import dumps
|
|
12
|
+
from time import time
|
|
13
|
+
|
|
14
|
+
from . import config
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class _BaseCache:
|
|
18
|
+
"""An abstract base class for the cache."""
|
|
19
|
+
|
|
20
|
+
# pylint: disable=no-self-use, unused-argument
|
|
21
|
+
|
|
22
|
+
def __init__(self, *args, **kwargs):
|
|
23
|
+
super().__init__()
|
|
24
|
+
self._cache = {}
|
|
25
|
+
#: `bool`: whether the cache is enabled
|
|
26
|
+
self.enabled = True
|
|
27
|
+
|
|
28
|
+
def put(self, item, *args, **kwargs):
|
|
29
|
+
"""Put an item into the cache."""
|
|
30
|
+
raise NotImplementedError
|
|
31
|
+
|
|
32
|
+
def get(self, *args, **kwargs):
|
|
33
|
+
"""Get an item from the cache."""
|
|
34
|
+
raise NotImplementedError
|
|
35
|
+
|
|
36
|
+
def delete(self, *args, **kwargs):
|
|
37
|
+
"""Delete an item from the cache."""
|
|
38
|
+
raise NotImplementedError
|
|
39
|
+
|
|
40
|
+
def clear(self):
|
|
41
|
+
"""Empty the whole cache."""
|
|
42
|
+
raise NotImplementedError
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class NullCache(_BaseCache):
|
|
46
|
+
"""A cache which does nothing.
|
|
47
|
+
|
|
48
|
+
Useful for debugging.
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
def put(self, item, *args, **kwargs):
|
|
52
|
+
"""Put an item into the cache."""
|
|
53
|
+
|
|
54
|
+
def get(self, *args, **kwargs):
|
|
55
|
+
"""Get an item from the cache."""
|
|
56
|
+
return None
|
|
57
|
+
|
|
58
|
+
def delete(self, *args, **kwargs):
|
|
59
|
+
"""Delete an item from the cache."""
|
|
60
|
+
|
|
61
|
+
def clear(self):
|
|
62
|
+
"""Empty the whole cache."""
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class TimedCache(_BaseCache):
|
|
66
|
+
"""A simple thread-safe cache for caching method return values.
|
|
67
|
+
|
|
68
|
+
The cache key is generated by from the given ``*args`` and ``**kwargs``.
|
|
69
|
+
Items are expired from the cache after a given period of time.
|
|
70
|
+
|
|
71
|
+
Example:
|
|
72
|
+
>>> from time import sleep
|
|
73
|
+
>>> cache = TimedCache()
|
|
74
|
+
>>> cache.put("item", 'some', kw='args', timeout=3)
|
|
75
|
+
>>> # Fetch the item again, by providing the same args and kwargs.
|
|
76
|
+
>>> assert cache.get('some', kw='args') == "item"
|
|
77
|
+
>>> # Providing different args or kwargs will not return the item.
|
|
78
|
+
>>> assert not cache.get('some', 'otherargs') == "item"
|
|
79
|
+
>>> # Waiting for less than the provided timeout does not cause the
|
|
80
|
+
>>> # item to expire.
|
|
81
|
+
>>> sleep(2)
|
|
82
|
+
>>> assert cache.get('some', kw='args') == "item"
|
|
83
|
+
>>> # But waiting for longer does.
|
|
84
|
+
>>> sleep(2)
|
|
85
|
+
>>> assert not cache.get('some', kw='args') == "item"
|
|
86
|
+
|
|
87
|
+
Warning:
|
|
88
|
+
At present, the cache can theoretically grow and grow, since entries
|
|
89
|
+
are not automatically purged, though in practice this is unlikely
|
|
90
|
+
since there are not that many different combinations of arguments in
|
|
91
|
+
the places where it is used in SoCo, so not that many different
|
|
92
|
+
cache entries will be created. If this becomes a problem,
|
|
93
|
+
use a thread and timer to purge the cache, or rewrite this to use
|
|
94
|
+
LRU logic!
|
|
95
|
+
"""
|
|
96
|
+
|
|
97
|
+
def __init__(self, default_timeout=0):
|
|
98
|
+
"""
|
|
99
|
+
Args:
|
|
100
|
+
default_timeout (int): The default number of seconds after
|
|
101
|
+
which items will be expired.
|
|
102
|
+
"""
|
|
103
|
+
super().__init__()
|
|
104
|
+
#: `int`: The default caching expiry interval in seconds.
|
|
105
|
+
self.default_timeout = default_timeout
|
|
106
|
+
# A thread lock for the cache
|
|
107
|
+
self._cache_lock = threading.Lock()
|
|
108
|
+
|
|
109
|
+
def get(self, *args, **kwargs):
|
|
110
|
+
"""Get an item from the cache for this combination of args and kwargs.
|
|
111
|
+
|
|
112
|
+
Args:
|
|
113
|
+
*args: any arguments.
|
|
114
|
+
**kwargs: any keyword arguments.
|
|
115
|
+
|
|
116
|
+
Returns:
|
|
117
|
+
object: The object which has been found in the cache, or `None` if
|
|
118
|
+
no unexpired item is found. This means that there is no point
|
|
119
|
+
storing an item in the cache if it is `None`.
|
|
120
|
+
|
|
121
|
+
"""
|
|
122
|
+
if not self.enabled:
|
|
123
|
+
return None
|
|
124
|
+
# Look in the cache to see if there is an unexpired item. If there is
|
|
125
|
+
# we can just return the cached result.
|
|
126
|
+
cache_key = self.make_key(args, kwargs)
|
|
127
|
+
# Lock and load
|
|
128
|
+
with self._cache_lock:
|
|
129
|
+
if cache_key in self._cache:
|
|
130
|
+
expirytime, item = self._cache[cache_key]
|
|
131
|
+
|
|
132
|
+
if expirytime >= time():
|
|
133
|
+
return item
|
|
134
|
+
else:
|
|
135
|
+
# An expired item is present - delete it
|
|
136
|
+
del self._cache[cache_key]
|
|
137
|
+
# Nothing found
|
|
138
|
+
return None
|
|
139
|
+
|
|
140
|
+
def put(self, item, *args, **kwargs):
|
|
141
|
+
"""Put an item into the cache, for this combination of args and kwargs.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
*args: any arguments.
|
|
145
|
+
**kwargs: any keyword arguments. If ``timeout`` is specified as one
|
|
146
|
+
of the keyword arguments, the item will remain available
|
|
147
|
+
for retrieval for ``timeout`` seconds. If ``timeout`` is
|
|
148
|
+
`None` or not specified, the ``default_timeout`` for this
|
|
149
|
+
cache will be used. Specify a ``timeout`` of 0 (or ensure that
|
|
150
|
+
the ``default_timeout`` for this cache is 0) if this item is
|
|
151
|
+
not to be cached.
|
|
152
|
+
"""
|
|
153
|
+
if not self.enabled:
|
|
154
|
+
return
|
|
155
|
+
# Check for a timeout keyword, store and remove it.
|
|
156
|
+
timeout = kwargs.pop("timeout", None)
|
|
157
|
+
if timeout is None:
|
|
158
|
+
timeout = self.default_timeout
|
|
159
|
+
cache_key = self.make_key(args, kwargs)
|
|
160
|
+
# Store the item, along with the time at which it will expire
|
|
161
|
+
with self._cache_lock:
|
|
162
|
+
self._cache[cache_key] = (time() + timeout, item)
|
|
163
|
+
|
|
164
|
+
def delete(self, *args, **kwargs):
|
|
165
|
+
"""Delete an item from the cache for this combination of args and
|
|
166
|
+
kwargs."""
|
|
167
|
+
cache_key = self.make_key(args, kwargs)
|
|
168
|
+
with self._cache_lock:
|
|
169
|
+
try:
|
|
170
|
+
del self._cache[cache_key]
|
|
171
|
+
except KeyError:
|
|
172
|
+
pass
|
|
173
|
+
|
|
174
|
+
def clear(self):
|
|
175
|
+
"""Empty the whole cache."""
|
|
176
|
+
with self._cache_lock:
|
|
177
|
+
self._cache.clear()
|
|
178
|
+
|
|
179
|
+
@staticmethod
|
|
180
|
+
def make_key(*args, **kwargs):
|
|
181
|
+
"""Generate a unique, hashable, representation of the args and kwargs.
|
|
182
|
+
|
|
183
|
+
Args:
|
|
184
|
+
*args: any arguments.
|
|
185
|
+
**kwargs: any keyword arguments.
|
|
186
|
+
|
|
187
|
+
Returns:
|
|
188
|
+
str: the key.
|
|
189
|
+
"""
|
|
190
|
+
# This is not entirely straightforward, since args and kwargs may
|
|
191
|
+
# contain mutable items and unicode. Possibilities include using
|
|
192
|
+
# __repr__, frozensets, and code from Py3's LRU cache. But pickle
|
|
193
|
+
# works, and although it is not as fast as some methods, it is good
|
|
194
|
+
# enough at the moment
|
|
195
|
+
cache_key = dumps((args, kwargs))
|
|
196
|
+
return cache_key
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
class Cache(NullCache):
|
|
200
|
+
"""A factory class which returns an instance of a cache subclass.
|
|
201
|
+
|
|
202
|
+
A `TimedCache` is returned, unless `config.CACHE_ENABLED` is `False`,
|
|
203
|
+
in which case a `NullCache` will be returned.
|
|
204
|
+
"""
|
|
205
|
+
|
|
206
|
+
def __new__(cls, *args, **kwargs):
|
|
207
|
+
if config.CACHE_ENABLED:
|
|
208
|
+
new_cls = TimedCache
|
|
209
|
+
else:
|
|
210
|
+
new_cls = NullCache
|
|
211
|
+
instance = super().__new__(new_cls)
|
|
212
|
+
instance.__init__(*args, **kwargs)
|
|
213
|
+
return instance
|
soco/config.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"""This module contains configuration variables.
|
|
2
|
+
|
|
3
|
+
They may be set by your code as follows::
|
|
4
|
+
|
|
5
|
+
from soco import config
|
|
6
|
+
...
|
|
7
|
+
config.VARIABLE = value
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
SOCO_CLASS = None
|
|
11
|
+
"""The class object to use when `SoCo` instances are created.
|
|
12
|
+
|
|
13
|
+
Specify the actual callable class object here, not a string. If `None`,
|
|
14
|
+
the default SoCo class will be used. Must be set before any instances are
|
|
15
|
+
created, or it will have unpredictable effects.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
CACHE_ENABLED = True
|
|
20
|
+
"""Is the cache enabled?
|
|
21
|
+
|
|
22
|
+
If `True` (the default), some caching of network requests will take place.
|
|
23
|
+
|
|
24
|
+
See also:
|
|
25
|
+
The :mod:`soco.cache` module.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
EVENT_ADVERTISE_IP = None
|
|
30
|
+
"""The IP on which to advertise to Sonos.
|
|
31
|
+
|
|
32
|
+
The default of None means that the relevant IP address will be detected
|
|
33
|
+
automatically.
|
|
34
|
+
|
|
35
|
+
See also:
|
|
36
|
+
The :mod:`soco.events_base` module.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
EVENT_LISTENER_IP = None
|
|
40
|
+
"""The IP on which the event listener listens.
|
|
41
|
+
|
|
42
|
+
The default of None means that the relevant IP address will be detected
|
|
43
|
+
automatically.
|
|
44
|
+
|
|
45
|
+
See also:
|
|
46
|
+
The :mod:`soco.events_base` module.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
EVENT_LISTENER_PORT = 1400
|
|
51
|
+
"""The port on which the event listener listens.
|
|
52
|
+
|
|
53
|
+
The default is 1400. You must set this before subscribing to any events.
|
|
54
|
+
|
|
55
|
+
See also:
|
|
56
|
+
The :mod:`soco.events_base` module.
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
EVENTS_MODULE = None
|
|
60
|
+
"""The events module to be used by the :mod:`soco.services` module.
|
|
61
|
+
|
|
62
|
+
The default of None means the :mod:`soco.events` module will be used.
|
|
63
|
+
|
|
64
|
+
See also:
|
|
65
|
+
The :mod:`soco.events` and :mod:`soco.events_twisted` modules.
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
REQUEST_TIMEOUT = 20.0
|
|
69
|
+
"""The timeout (in seconds) to be used when sending commands to a Sonos device.
|
|
70
|
+
|
|
71
|
+
A value for REQUEST_TIMEOUT *must* be set. It can be a float, an int, or None.
|
|
72
|
+
If set to 'None', calls can potentially wait indefinitely. (The default of 20.0s
|
|
73
|
+
is a long time for network operations, but it's been determined empirically to
|
|
74
|
+
be a reasonable upper limit for most circumstances.)
|
|
75
|
+
|
|
76
|
+
REQUEST_TIMEOUT can be set dynamically during program execution to adjust the
|
|
77
|
+
timeout at runtime. It can also be overridden for specific calls by using the
|
|
78
|
+
'timeout' kwarg in the relevant calling functions.
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
ZGT_EVENT_FALLBACK = True
|
|
82
|
+
"""
|
|
83
|
+
For large Sonos systems (about 20+ players) the standard method of querying a
|
|
84
|
+
player for the Sonos Zone Group Topology will fail.
|
|
85
|
+
|
|
86
|
+
By default, SoCo will then fall back to using a method based on ZGT events. If
|
|
87
|
+
you wish to disable this behaviour, set 'ZGT_EVENT_FALLBACK' to 'False'. Your
|
|
88
|
+
code should then be prepared to catch `NotSupportedException` errors when
|
|
89
|
+
using functions that interrogate system state.
|
|
90
|
+
"""
|