-
Notifications
You must be signed in to change notification settings - Fork 229
/
data_structures.py
executable file
·1340 lines (1078 loc) · 46.7 KB
/
data_structures.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# pylint: disable=star-args, too-many-arguments, fixme, import-outside-toplevel
# Disable while we have Python 2.x compatability
# pylint: disable=useless-object-inheritance,bad-mcs-classmethod-argument
"""
This module contains classes for handling DIDL-Lite metadata.
`DIDL`_ is the Digital Item Declaration Language , an XML schema which is
part of MPEG21. `DIDL-Lite`_ is a cut-down version of the schema which is part
of the UPnP ContentDirectory specification. It is the XML schema used by Sonos
for carrying metadata representing many items such as tracks, playlists,
composers, albums etc. Although Sonos uses
ContentDirectory v1, the `document for v2 [pdf]`_ is more
helpful.
.. _DIDL: http://xml.coverpages.org/mpeg21-didl.html
.. _DIDL-Lite: http://www.upnp.org/schemas/av/didl-lite-v2.xsd
.. _document for v2 [pdf]: _http://upnp.org/specs/av/UPnP
-av-ContentDirectory-v2-Service
"""
# It tries to follow the class hierarchy provided by the DIDL-Lite schema
# described in the UPnP Spec, especially that for the ContentDirectory Service
# Although Sonos uses ContentDirectory v1, the document for v2 is more helpful:
# http://upnp.org/specs/av/UPnP-av-ContentDirectory-v2-Service.pdf
import textwrap
import warnings
from .exceptions import DIDLMetadataError
from .utils import really_unicode, first_cap
from .xml import XML, ns_tag
from .data_structure_quirks import apply_resource_quirks
# Due to cyclic import problems, we only import from_didl_string at runtime.
# from data_structures_entry import from_didl_string
_FROM_DIDL_STRING_FUNCTION = None
###############################################################################
# MISC HELPER FUNCTIONS #
###############################################################################
def to_didl_string(*args):
"""Convert any number of `DidlObjects <DidlObject>` to a unicode xml
string.
Args:
*args (DidlObject): One or more `DidlObject` (or subclass) instances.
Returns:
str: A unicode string representation of DIDL-Lite XML in the form
``'<DIDL-Lite ...>...</DIDL-Lite>'``.
"""
didl = XML.Element(
"DIDL-Lite",
{
"xmlns": "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/",
"xmlns:dc": "http://purl.org/dc/elements/1.1/",
"xmlns:upnp": "urn:schemas-upnp-org:metadata-1-0/upnp/",
"xmlns:r": "urn:schemas-rinconnetworks-com:metadata-1-0/",
},
)
for arg in args:
didl.append(arg.to_element())
return XML.tostring(didl, encoding="unicode")
def didl_class_to_soco_class(didl_class):
"""Translate a DIDL-Lite class to the corresponding SoCo data structures class"""
# Certain music services have been observed to sub-class via a .# or # syntax.
# We simply remove these subclasses.
for separator in (".#", "#"):
if separator in didl_class:
didl_class = didl_class[: didl_class.find(separator)]
try:
cls = _DIDL_CLASS_TO_CLASS[didl_class]
except KeyError:
# Unknown class, automatically create subclass
new_class_name = form_name(didl_class)
base_class = didl_class_to_soco_class(".".join(didl_class.split(".")[:-1]))
cls = type(
new_class_name,
(base_class,),
{
"item_class": didl_class,
__doc__: "Class that represents a {}".format(didl_class),
},
)
_DIDL_CLASS_TO_CLASS[didl_class] = cls
return cls
_OFFICIAL_CLASSES = {
"object",
"object.item",
"object.item.audioItem",
"object.item.audioItem.musicTrack",
"object.item.audioItem.audioBroadcast",
"object.item.audioItem.audioBook",
"object.container",
"object.container.person",
"object.container.person.musicArtist",
"object.container.playlistContainer",
"object.container.album",
"object.container.musicAlbum",
"object.container.genre",
"object.container.musicGenre",
}
def form_name(didl_class):
"""Return an improvised name for vendor extended classes"""
if not didl_class.startswith("object."):
raise DIDLMetadataError("Unknown UPnP class: %s" % didl_class)
# We know that the string starts with "object." so -1 indexing is safe
parts = didl_class.split(".")
# If it is a Sonos favorite, form the name as the class component
# before with "Favorite" added. So:
# object.item.audioItem.audioBroadcast.sonos-favorite
# turns into
# DidlAudioBroadcastFavorite
if parts[-1] == "sonos-favorite" and len(parts) >= 2:
return "Didl" + first_cap(parts[-2]) + "Favorite"
# For any other class, for the name as the concatenation of all
# the class components that are not UPnP core classes. So:
# object.container.playlistContainer.sameArtist
# Turns into:
# DidlSameArtist
search_parts = parts[:]
new_parts = []
# Strip the components one by one and check whether the base is known
while search_parts:
new_parts.append(search_parts[-1])
search_parts = search_parts[:-1]
search_class = ".".join(search_parts)
if search_class in _OFFICIAL_CLASSES:
break
# For class path last parts that contain the word list, capitalize it
if new_parts[0].endswith("list"):
new_parts[0] = new_parts[0].replace("list", "List")
new_parts = reversed(new_parts)
return "Didl" + "".join(first_cap(s) for s in new_parts)
###############################################################################
# DIDL RESOURCE #
###############################################################################
class DidlResource:
"""Identifies a resource, typically some type of a binary asset, such as a
song.
It is represented in XML by a ``<res>`` element, which contains a uri that
identifies the resource.
"""
# Adapted from a class taken from the Python Brisa project - MIT licence.
# pylint: disable=too-many-instance-attributes
def __init__(
self,
uri,
protocol_info,
import_uri=None,
size=None,
duration=None,
bitrate=None,
sample_frequency=None,
bits_per_sample=None,
nr_audio_channels=None,
resolution=None,
color_depth=None,
protection=None,
):
"""
Args:
uri (str): value of the ``<res>`` tag, typically a URI. It
**must** be properly escaped (percent encoded) as
described in :rfc:`3986`
protocol_info (str): a string in the form a:b:c:d that
identifies the streaming or transport protocol for
transmitting the resource. A value is required. For more
information see section 2.5.2 of the `UPnP specification [
pdf]
<http://upnp.org/specs/av/UPnP-av-ConnectionManager-v1-
Service.pdf>`_
import_uri (str, optional): uri locator for resource update.
size (int, optional): size in bytes.
duration (str, optional): duration of the playback of the res
at normal speed (``H*:MM:SS:F*`` or ``H*:MM:SS:F0/F1``)
bitrate (int, optional): bitrate in bytes/second.
sample_frequency (int, optional): sample frequency in Hz.
bits_per_sample (int, optional): bits per sample.
nr_audio_channels (int, optional): number of audio channels.
resolution (str, optional): resolution of the resource (X*Y).
color_depth (int, optional): color depth in bits.
protection (str, optional): statement of protection type.
Note:
Not all of the parameters are used by Sonos. In general, only
``uri``, ``protocol_info`` and ``duration`` seem to be important.
"""
# Of these attributes, only uri, protocol_info and duration have been
# spotted 'in the wild'
#: (str): a percent encoded URI
self.uri = uri
# Protocol info is in the form a:b:c:d - see
# sec 2.5.2 at
# http://upnp.org/specs/av/UPnP-av-ConnectionManager-v1-Service.pdf
#: (str): protocol information.
self.protocol_info = protocol_info
self.import_uri = import_uri
self.size = size
#: str: playback duration
self.duration = duration
self.bitrate = bitrate
self.sample_frequency = sample_frequency
self.bits_per_sample = bits_per_sample
self.nr_audio_channels = nr_audio_channels
self.resolution = resolution
self.color_depth = color_depth
self.protection = protection
@classmethod
def from_element(cls, element):
"""Set the resource properties from a ``<res>`` element.
Args:
element (~xml.etree.ElementTree.Element): The ``<res>``
element
"""
def _int_helper(name):
"""Try to convert the name attribute to an int, or None."""
result = element.get(name)
if result is not None:
try:
return int(result)
except ValueError as error:
raise DIDLMetadataError(
"Could not convert {} to an integer".format(name)
) from error
else:
return None
# Check for and fix non-spec compliant behavior in the incoming data
element = apply_resource_quirks(element)
content = {}
# required
content["protocol_info"] = element.get("protocolInfo")
if content["protocol_info"] is None:
raise DIDLMetadataError(
"Could not create Resource from Element: "
"protocolInfo not found (required)."
)
# Optional
content["import_uri"] = element.get("importUri")
content["size"] = _int_helper("size")
content["duration"] = element.get("duration")
content["bitrate"] = _int_helper("bitrate")
content["sample_frequency"] = _int_helper("sampleFrequency")
content["bits_per_sample"] = _int_helper("bitsPerSample")
content["nr_audio_channels"] = _int_helper("nrAudioChannels")
content["resolution"] = element.get("resolution")
content["color_depth"] = _int_helper("colorDepth")
content["protection"] = element.get("protection")
content["uri"] = element.text
return cls(**content)
def __repr__(self):
return "<{} '{}' at {}>".format(
self.__class__.__name__, self.uri, hex(id(self))
)
def __str__(self):
return self.__repr__()
def to_element(self):
"""Return an ElementTree Element based on this resource.
Returns:
~xml.etree.ElementTree.Element: an Element.
"""
if not self.protocol_info:
raise DIDLMetadataError(
"Could not create Element for this"
"resource:"
"protocolInfo not set (required)."
)
root = XML.Element("res")
# Required
root.attrib["protocolInfo"] = self.protocol_info
# Optional
if self.import_uri is not None:
root.attrib["importUri"] = self.import_uri
if self.size is not None:
root.attrib["size"] = str(self.size)
if self.duration is not None:
root.attrib["duration"] = self.duration
if self.bitrate is not None:
root.attrib["bitrate"] = str(self.bitrate)
if self.sample_frequency is not None:
root.attrib["sampleFrequency"] = str(self.sample_frequency)
if self.bits_per_sample is not None:
root.attrib["bitsPerSample"] = str(self.bits_per_sample)
if self.nr_audio_channels is not None:
root.attrib["nrAudioChannels"] = str(self.nr_audio_channels)
if self.resolution is not None:
root.attrib["resolution"] = self.resolution
if self.color_depth is not None:
root.attrib["colorDepth"] = str(self.color_depth)
if self.protection is not None:
root.attrib["protection"] = self.protection
root.text = self.uri
return root
def to_dict(self, remove_nones=False):
"""Return a dict representation of the `DidlResource`.
Args:
remove_nones (bool, optional): Optionally remove dictionary
elements when their value is `None`.
Returns:
dict: a dict representing the `DidlResource`
"""
content = {
"uri": self.uri,
"protocol_info": self.protocol_info,
"import_uri": self.import_uri,
"size": self.size,
"duration": self.duration,
"bitrate": self.bitrate,
"sample_frequency": self.sample_frequency,
"bits_per_sample": self.bits_per_sample,
"nr_audio_channels": self.nr_audio_channels,
"resolution": self.resolution,
"color_depth": self.color_depth,
"protection": self.protection,
}
if remove_nones:
# delete any elements that have a value of None to optimize size
# of the returned structure
nones = [k for k in content if content[k] is None]
for k in nones:
del content[k]
return content
@classmethod
def from_dict(cls, content):
"""Create an instance from a dict.
An alternative constructor. Equivalent to ``DidlResource(**content)``.
Args:
content (dict): a dict containing metadata information. Required.
Valid keys are the same as the parameters for
`DidlResource`.
"""
return cls(**content)
def __eq__(self, resource):
"""Compare with another ``DidlResource``.
Returns:
(bool): `True` if all items are equal, else `False`.
"""
if not isinstance(resource, DidlResource):
return False
return self.to_dict() == resource.to_dict()
###############################################################################
# BASE OBJECTS #
###############################################################################
# a mapping which will be used to look up the relevant class from the
# DIDL item class
_DIDL_CLASS_TO_CLASS = {}
class DidlMetaClass(type):
"""Meta class for all Didl objects."""
def __new__(cls, name, bases, attrs):
"""Create a new instance.
Args:
name (str): Name of the class.
bases (tuple): Base classes.
attrs (dict): attributes defined for the class.
"""
new_cls = super().__new__(cls, name, bases, attrs)
# Register all subclasses with the global _DIDL_CLASS_TO_CLASS mapping
item_class = attrs.get("item_class", None)
if item_class is not None:
_DIDL_CLASS_TO_CLASS[item_class] = new_cls
return new_cls
class DidlObject(metaclass=DidlMetaClass):
"""Abstract base class for all DIDL-Lite items.
You should not need to instantiate this. Its XML representation looks
like this:
.. code-block:: xml
<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:r="urn:schemas-rinconnetworks-com:metadata-1-0/"
xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/">
<item id="...self.item_id..." parentID="...cls.parent_id..."
restricted="true">
<dc:title>...self.title...</dc:title>
<upnp:class>...self.item_class...</upnp:class>
<desc id="cdudn"
nameSpace="urn:schemas-rinconnetworks-com:metadata-1-0/">
RINCON_AssociatedZPUDN
</desc>
</item>
</DIDL-Lite>
"""
# the DIDL Lite class for this object.
item_class = "object"
tag = "item"
# key: attribute_name: (ns, tag)
_translation = {
"creator": ("dc", "creator"),
"write_status": ("upnp", "writeStatus"),
}
def __init__(
self,
title,
parent_id,
item_id,
restricted=True,
resources=None,
desc="RINCON_AssociatedZPUDN",
**kwargs
):
"""
Args:
title (str): the title for the item.
parent_id (str): the parent ID for the item.
item_id (str): the ID for the item.
restricted (bool): whether the item can be modified. Default `True`
resources (list, optional): a list of resources for this object.
Default `None`.
desc (str): A DIDL descriptor, default
``'RINCON_AssociatedZPUDN'``. This is not the same as
"description". It is used for identifying the relevant
third party music service.
**kwargs: Extra metadata. What is allowed depends on the
``_translation`` class attribute, which in turn depends on the
DIDL class.
.. autoattribute:: item_class
str - the DIDL Lite class for this object.
.. autoattribute:: tag
str - the XML element tag name used for this instance.
.. autoattribute:: _translation
dict - A dict used to translate between instance attribute
names and XML tags/namespaces. It also serves to define the
allowed tags/attributes for this instance. Each key an attribute
name and each key is a ``(namespace, tag)`` tuple.
"""
# All didl objects *must* have a title, a parent_id and an item_id
# so we specify these as required args in the constructor signature
# to ensure that we get them. Other didl object properties are
# optional, so can be passed as kwargs.
# The content of _translation is adapted from the list in table C at
# http://upnp.org/specs/av/UPnP-av-ContentDirectory-v2-Service.pdf
# Not all properties referred to there are catered for, since Sonos
# does not use some of them.
# pylint: disable=super-on-old-class
super().__init__()
self.title = title
self.parent_id = parent_id
self.item_id = item_id
# Restricted is a compulsory attribute, but is almost always True for
# Sonos. (Only seen it 'false' when browsing favorites)
self.restricted = restricted
# Resources is multi-valued, and dealt with separately
self.resources = [] if resources is None else resources
# According to the spec, there may be one or more desc values. Sonos
# only seems to use one, so we won't bother with a list
self.desc = desc
for key, value in kwargs.items():
# For each attribute, check to see if this class allows it
if key not in self._translation:
raise ValueError(
"The key '{}' is not allowed as an argument. Only "
"these keys are allowed: parent_id, item_id, title, "
"restricted, resources, desc"
" {}".format(key, ", ".join(self._translation.keys()))
)
# It is an allowed attribute. Set it as an attribute on self, so
# that it can be accessed as Classname.attribute in the normal
# way.
setattr(self, key, value)
# pylint: disable=too-many-locals, too-many-branches
@classmethod
def from_element(cls, element): # pylint: disable=R0914
"""Create an instance of this class from an ElementTree xml Element.
An alternative constructor. The element must be a DIDL-Lite <item> or
<container> element, and must be properly namespaced.
Args:
xml (~xml.etree.ElementTree.Element): An
:class:`~xml.etree.ElementTree.Element` object.
"""
# We used to check here that we have the right sort of element,
# ie a container or an item. But Sonos seems to use both
# indiscriminately, eg a playlistContainer can be an item or a
# container. So we now just check that it is one or the other.
tag = element.tag
if not (tag.endswith("item") or tag.endswith("container")):
raise DIDLMetadataError(
"Wrong element. Expected <item> or <container>,"
" got <{}> for class {}'".format(tag, cls.item_class)
)
# and that the upnp matches what we are expecting
item_class = element.find(ns_tag("upnp", "class")).text
# In case this class has an # specified unofficial
# subclass, ignore it by stripping it from item_class
for separator in (".#", "#"):
if separator in item_class:
item_class = item_class[: item_class.find(separator)]
if item_class != cls.item_class:
raise DIDLMetadataError(
"UPnP class is incorrect. Expected '{}',"
" got '{}'".format(cls.item_class, item_class)
)
# parent_id, item_id and restricted are stored as attributes on the
# element
item_id = element.get("id", None)
if item_id is None:
raise DIDLMetadataError("Missing id attribute")
item_id = really_unicode(item_id)
parent_id = element.get("parentID", None)
if parent_id is None:
raise DIDLMetadataError("Missing parentID attribute")
parent_id = really_unicode(parent_id)
# CAUTION: This implementation deviates from the spec.
# Elements are normally required to have a `restricted` tag, but
# Spotify Direct violates this. To make it work, a missing restricted
# tag is interpreted as `restricted = True`.
restricted = element.get("restricted", None)
restricted = restricted not in [0, "false", "False"]
# Similarily, all elements should have a title tag, but Spotify Direct
# does not comply
title_elt = element.find(ns_tag("dc", "title"))
if title_elt is None or not title_elt.text:
title = ""
else:
title = really_unicode(title_elt.text)
# Deal with any resource elements
resources = []
for res_elt in element.findall(ns_tag("", "res")):
# Not all Favorits have resources, so in case the "res"
# tage has no attributes, just skip it
if cls is DidlFavorite and not res_elt.attrib:
continue
resources.append(DidlResource.from_element(res_elt))
# and the desc element (There is only one in Sonos)
desc = element.findtext(ns_tag("", "desc"))
# Get values of the elements listed in _translation and add them to
# the content dict
content = {}
for key, value in cls._translation.items():
result = element.findtext(ns_tag(*value))
if result is not None:
# We store info as unicode internally.
content[key] = really_unicode(result)
# Convert type for original track number
if content.get("original_track_number") is not None:
content["original_track_number"] = int(content["original_track_number"])
# Now pass the content dict we have just built to the main
# constructor, as kwargs, to create the object
return cls(
title=title,
parent_id=parent_id,
item_id=item_id,
restricted=restricted,
resources=resources,
desc=desc,
**content
)
@classmethod
def from_dict(cls, content):
"""Create an instance from a dict.
An alternative constructor. Equivalent to ``DidlObject(**content)``.
Args:
content (dict): a dict containing metadata information. Required.
Valid keys are the same as the parameters for `DidlObject`.
"""
# Do we really need this constructor? Could use DidlObject(**content)
# instead. -- We do now
if "resources" in content:
content["resources"] = [
DidlResource.from_dict(x) for x in content["resources"]
]
return cls(**content)
def __eq__(self, playable_item):
"""Compare with another ``playable_item``.
Returns:
(bool): `True` if all items are equal, else `False`.
"""
if not isinstance(playable_item, DidlObject):
return False
return self.to_dict() == playable_item.to_dict()
def __ne__(self, playable_item):
"""Compare with another ``playable_item``.
Returns:
(bool): `True` if any items is unequal, else `False`.
"""
if not isinstance(playable_item, DidlObject):
return True
return self.to_dict() != playable_item.to_dict()
def __repr__(self):
"""Get the repr value for the item.
Returns:
str: A string representation of the instance in the form
``<class_name 'middle_part[0:40]' at id_in_hex>`` where
middle_part is either the title item in content, if it is set, or
``str(content)``. The output is also cleared of non-ascii
characters.
"""
# 40 originates from terminal width (78) - (15) for address part and
# (19) for the longest class name and a little left for buffer
if self.title is not None:
middle = self.title.encode("ascii", "replace")[0:40]
else:
middle = str(self.to_dict).encode("ascii", "replace")[0:40]
return "<{} '{}' at {}>".format(self.__class__.__name__, middle, hex(id(self)))
def __str__(self):
"""Get the str value for the item.
Returns:
str: a string representation in the form
``<class_name 'middle_part[0:40]' at id_in_hex>`` where
middle_part is either the title item in content, if it is set, or
``str(content)``. The output is also cleared of non-ascii
characters.
"""
return self.__repr__()
def to_dict(self, remove_nones=False):
"""Return the dict representation of the instance.
Args:
remove_nones (bool, optional): Optionally remove dictionary
elements when their value is `None`.
Returns:
dict: a dict representation of the `DidlObject`.
"""
content = {}
# Get the value of each attribute listed in _translation, and add it
# to the content dict
for key in self._translation:
if hasattr(self, key):
content[key] = getattr(self, key)
# also add parent_id, item_id, restricted, title and resources because
# they are not listed in _translation
content["parent_id"] = self.parent_id
content["item_id"] = self.item_id
content["restricted"] = self.restricted
content["title"] = self.title
if self.resources != []:
content["resources"] = [
resource.to_dict(remove_nones=remove_nones)
for resource in self.resources
]
content["desc"] = self.desc
return content
def to_element(self, include_namespaces=False):
"""Return an ElementTree Element representing this instance.
Args:
include_namespaces (bool, optional): If True, include xml
namespace attributes on the root element
Return:
~xml.etree.ElementTree.Element: an Element.
"""
elt_attrib = {}
if include_namespaces:
elt_attrib.update(
{
"xmlns": "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/",
"xmlns:dc": "http://purl.org/dc/elements/1.1/",
"xmlns:upnp": "urn:schemas-upnp-org:metadata-1-0/upnp/",
}
)
elt_attrib.update(
{
"parentID": self.parent_id,
"restricted": "true" if self.restricted else "false",
"id": self.item_id,
}
)
elt = XML.Element(self.tag, elt_attrib)
# Add the title, which should always come first, according to the spec
XML.SubElement(elt, "dc:title").text = self.title
# Add in any resources
for resource in self.resources:
elt.append(resource.to_element())
# Add the rest of the metadata attributes (i.e all those listed in
# _translation) as sub-elements of the item element.
for key, value in self._translation.items():
if hasattr(self, key):
# Some attributes have a namespace of '', which means they
# are in the default namespace. We need to handle those
# carefully
tag = "%s:%s" % value if value[0] else "%s" % value[1]
XML.SubElement(elt, tag).text = "%s" % getattr(self, key)
# Now add in the item class
XML.SubElement(elt, "upnp:class").text = self.item_class
# And the desc element
desc_attrib = {
"id": "cdudn",
"nameSpace": "urn:schemas-rinconnetworks-com:metadata-1-0/",
}
desc_elt = XML.SubElement(elt, "desc", desc_attrib)
desc_elt.text = self.desc
return elt
def get_uri(self, resource_nr=0):
"""Return the uri to use for playing this item.
Args:
resource_nr (int): The index of the resource. Note that there is no
known object with more than one resource, so you can probably
keep the default value (0).
Returns:
str: The uri.
"""
return self.resources[resource_nr].uri
def set_uri(self, uri, resource_nr=0, protocol_info=None):
"""Set a resource uri for this instance. If no resource exists, create
a new one with the given protocol info.
Args:
uri (str): The resource uri.
resource_nr (int): The index of the resource on which to set the
uri. If it does not exist, a new resource is added to the list.
Note that by default, only the uri of the first resource is
used for playing the item.
protocol_info (str): Protocol info for the resource. If none is
given and the resource does not exist yet, a default protocol
info is constructed as ``'[uri prefix]:*:*:*'``.
"""
try:
self.resources[resource_nr].uri = uri
if protocol_info is not None:
self.resources[resource_nr].protocol_info = protocol_info
except IndexError:
if protocol_info is None:
# create default protcol info
protocol_info = uri[: uri.index(":")] + ":*:*:*"
self.resources.append(DidlResource(uri, protocol_info))
###############################################################################
# OBJECT.ITEM HIERARCHY #
###############################################################################
class DidlItem(DidlObject):
"""A basic content directory item."""
# The spec allows for an option 'refID' attribute, but we do not handle it
# the DIDL Lite class for this object.
item_class = "object.item"
# _translation = DidlObject._translation.update({ ...})
# does not work, but doing it in two steps does
_translation = DidlObject._translation.copy()
_translation.update(
{
"stream_content": ("r", "streamContent"),
"radio_show": ("r", "radioShowMd"),
"album_art_uri": ("upnp", "albumArtURI"),
}
)
class DidlAudioItem(DidlItem):
"""An audio item."""
# the DIDL Lite class for this object.
item_class = "object.item.audioItem"
_translation = DidlItem._translation.copy()
_translation.update(
{
"genre": ("upnp", "genre"),
"description": ("dc", "description"),
"long_description": ("upnp", "longDescription"),
"publisher": ("dc", "publisher"),
"language": ("dc", "language"),
"relation": ("dc", "relation"),
"rights": ("dc", "rights"),
}
)
class DidlMusicTrack(DidlAudioItem):
"""Class that represents a music library track."""
# the DIDL Lite class for this object.
item_class = "object.item.audioItem.musicTrack"
# name: (ns, tag)
_translation = DidlAudioItem._translation.copy()
_translation.update(
{
"artist": ("upnp", "artist"),
"album": ("upnp", "album"),
"original_track_number": ("upnp", "originalTrackNumber"),
"playlist": ("upnp", "playlist"),
"contributor": ("dc", "contributor"),
"date": ("dc", "date"),
}
)
class DidlAudioBook(DidlAudioItem):
"""Class that represents an audio book."""
# the DIDL Lite class for this object.
item_class = "object.item.audioItem.audioBook"
# name: (ns, tag)
_translation = DidlAudioItem._translation.copy()
_translation.update(
{
"storageMedium": ("upnp", "storageMedium"),
"producer": ("upnp", "producer"),
"contributor": ("dc", "contributor"),
"date": ("dc", "date"),
}
)
class DidlAudioBroadcast(DidlAudioItem):
"""Class that represents an audio broadcast."""
# the DIDL Lite class for this object.
item_class = "object.item.audioItem.audioBroadcast"
_translation = DidlAudioItem._translation.copy()
_translation.update(
{
"region": ("upnp", "region"),
"radio_call_sign": ("upnp", "radioCallSign"),
"radio_station_id": ("upnp", "radioStationID"),
"channel_nr": ("upnp", "channelNr"),
}
)
class DidlRecentShow(DidlMusicTrack):
"""Class that represents a recent radio show/podcast."""
# the DIDL Lite class for this object.
item_class = "object.item.audioItem.musicTrack.recentShow"
class DidlAudioBroadcastFavorite(DidlAudioBroadcast):
"""Class that represents an audio broadcast Sonos favorite."""
# Note: The sonos-favorite part of the class spec obviously isn't part of
# the DIDL spec, so just assume that it has the same definition as the
# regular object.item.audioItem.audioBroadcast
# the DIDL Lite class for this object.
item_class = "object.item.audioItem.audioBroadcast.sonos-favorite"
class DidlFavorite(DidlItem):
"""Class that represents a Sonos favorite.
Note that the favorite itself isn't playable in all cases, please use the
object returned by :attr:`favorite.reference` instead."""
# the DIDL Lite class for this object.
item_class = "object.itemobject.item.sonos-favorite"
_translation = DidlItem._translation.copy()
_translation.update(
{
"type": ("r", "type"),
"description": ("r", "description"),
"favorite_nr": ("r", "ordinal"),
"resource_meta_data": ("r", "resMD"),
}
)
# The resMD tag contains the metadata of the Didl object referenced by this
# favorite. For user convenience, we will parse this metadata and make the
# object available via the 'reference' property.
@property
def reference(self):
"""The Didl object this favorite refers to."""
# Import from_didl_string if it isn't present already. The import
# happens here because it would cause cyclic import errors if the
# import happened at load time.
global _FROM_DIDL_STRING_FUNCTION # pylint: disable=global-statement
if not _FROM_DIDL_STRING_FUNCTION:
from . import data_structures_entry
_FROM_DIDL_STRING_FUNCTION = data_structures_entry.from_didl_string
ref = _FROM_DIDL_STRING_FUNCTION(getattr(self, "resource_meta_data"))[0]
# The resMD metadata lacks a <res> tag, so we use the resources from
# the favorite to make 'reference' playable.
ref.resources = self.resources
return ref
@reference.setter
def reference(self, value):
setattr(self, "resource_meta_data", to_didl_string(value))
self.resources = value.resources
###############################################################################
# OBJECT.CONTAINER HIERARCHY #
###############################################################################