File: didl_lite.py

package info (click to toggle)
python-didl-lite 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 204 kB
  • sloc: python: 1,381; makefile: 6; sh: 5
file content (1152 lines) | stat: -rw-r--r-- 35,773 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
# -*- coding: utf-8 -*-
"""DIDL-Lite (Digital Item Declaration Language) tools for Python."""
# pylint: disable=too-many-lines

import re
from typing import (
    Any,
    Dict,
    Iterable,
    List,
    Mapping,
    Optional,
    Sequence,
    Tuple,
    Type,
    TypeVar,
    Union,
)
from xml.etree import ElementTree as ET

import defusedxml.ElementTree

from .utils import (
    NAMESPACES,
    didl_property_def_key,
    didl_property_key,
    expand_namespace_tag,
    split_namespace_tag,
    to_camel_case,
)

TDO = TypeVar("TDO", bound="DidlObject")  # pylint: disable=invalid-name
TC = TypeVar("TC", bound="Container")  # pylint: disable=invalid-name
TD = TypeVar("TD", bound="Descriptor")  # pylint: disable=invalid-name
TR = TypeVar("TR", bound="Resource")  # pylint: disable=invalid-name


class DidlLiteException(Exception):
    """DIDL Lite Exception."""


# region: DidlObjects

# upnp_class to python type mapping
_upnp_class_map: Dict[str, Type["DidlObject"]] = {}
_upnp_class_map_lowercase: Dict[str, Type["DidlObject"]] = {}


class DidlObject:
    """DIDL Object."""

    tag: Optional[str] = None
    upnp_class: str = "object"
    didl_properties_defs: List[Tuple[str, str, str]] = [
        ("didl_lite", "@id", "R"),
        ("didl_lite", "@parentID", "R"),
        ("didl_lite", "@restricted", "R"),
        ("dc", "title", "R"),
        ("upnp", "class", "R"),
        ("dc", "creator", "O"),
        ("didl_lite", "res", "O"),
        ("upnp", "writeStatus", "O"),
    ]

    id: str
    parent_id: str
    res: List["Resource"]
    xml_el: Optional[ET.Element]
    descriptors: Sequence["Descriptor"]

    @classmethod
    def __init_subclass__(cls: Type["DidlObject"], **kwargs: Any) -> None:
        """Create mapping of upnp_class to Python type for fast lookup."""
        super().__init_subclass__(**kwargs)
        assert cls.upnp_class not in _upnp_class_map
        assert cls.upnp_class.lower() not in _upnp_class_map_lowercase
        _upnp_class_map[cls.upnp_class] = cls
        _upnp_class_map_lowercase[cls.upnp_class.lower()] = cls

    def __init__(
        self,
        id: str = "",
        parent_id: str = "",
        descriptors: Optional[Sequence["Descriptor"]] = None,
        xml_el: Optional[ET.Element] = None,
        strict: bool = True,
        **properties: Any,
    ) -> None:
        """Initialize."""
        # pylint: disable=invalid-name,redefined-builtin,too-many-arguments,too-many-positional-arguments
        properties["id"] = id
        properties["parent_id"] = parent_id
        properties["class"] = self.upnp_class
        properties["res"] = properties.get("res") or properties.get("resources") or []
        if "resources" in properties:
            del properties["resources"]
        self._ensure_required_properties(strict, properties)
        self._set_property_defaults()
        self._set_properties(properties)

        self.xml_el = xml_el
        self.descriptors = descriptors if descriptors else []

    def _ensure_required_properties(
        self, strict: bool, properties: Mapping[str, Any]
    ) -> None:
        """Check if all required properties are given."""
        if not strict:
            return

        python_property_keys = {didl_property_key(key) for key in properties}

        for property_def in self.didl_properties_defs:
            key = didl_property_def_key(property_def)
            if property_def[2] == "R" and key not in python_property_keys:
                raise DidlLiteException(key + " is mandatory")

    def _set_property_defaults(self) -> None:
        """Ensure we have default/known slots, and set them all to None."""
        for property_def in self.didl_properties_defs:
            key = didl_property_def_key(property_def)
            setattr(self, key, None)

    def _set_properties(self, properties: Mapping[str, Any]) -> None:
        """Set attributes from properties."""
        for key, value in properties.items():
            setattr(self, key, value)

    @classmethod
    def from_xml(cls: Type[TDO], xml_el: ET.Element, strict: bool = True) -> TDO:
        """
        Initialize from an XML node.

        I.e., parse XML and return instance.
        """
        # pylint: disable=too-many-locals
        properties = {}  # type: Dict[str, Any]

        # attributes
        for attr_key, attr_value in xml_el.attrib.items():
            key = to_camel_case(attr_key)
            properties[key] = attr_value

        # child-nodes
        for xml_child_node in xml_el:
            if xml_child_node.tag == expand_namespace_tag("didl_lite:res"):
                continue

            _, tag = split_namespace_tag(xml_child_node.tag)
            key = to_camel_case(tag)
            value = xml_child_node.text
            properties[key] = value

            # attributes of child nodes
            parent_key = key
            for attr_key, attr_value in xml_child_node.attrib.items():
                key = parent_key + "_" + to_camel_case(attr_key)
                properties[key] = attr_value

        # resources
        resources = []
        for res_el in xml_el.findall("./didl_lite:res", NAMESPACES):
            resource = Resource.from_xml(res_el)
            resources.append(resource)
        properties["res"] = properties["resources"] = resources

        # descriptors
        descriptors = []
        for desc_el in xml_el.findall("./didl_lite:desc", NAMESPACES):
            descriptor = Descriptor.from_xml(desc_el)
            descriptors.append(descriptor)

        return cls(xml_el=xml_el, descriptors=descriptors, strict=strict, **properties)

    def to_xml(self) -> ET.Element:
        """Convert self to XML Element."""
        assert self.tag is not None
        item_el = ET.Element(self.tag)
        elements = {"": item_el}

        # properties
        for property_def in self.didl_properties_defs:
            if "@" in property_def[1]:
                continue
            key = didl_property_def_key(property_def)

            if (
                getattr(self, key) is None or key == "res"
            ):  # no resources, handled later on
                continue

            tag = property_def[0] + ":" + property_def[1]
            property_el = ET.Element(tag, {})
            property_el.text = getattr(self, key)
            item_el.append(property_el)
            elements[property_def[1]] = property_el

        # attributes and property@attributes
        for property_def in self.didl_properties_defs:
            if "@" not in property_def[1]:
                continue

            key = didl_property_def_key(property_def)
            value = getattr(self, key)
            if value is None:
                continue

            el_name, attr_name = property_def[1].split("@")
            property_el = elements[el_name]
            property_el.attrib[attr_name] = value

        # resource
        for resource in self.resources:
            res_el = resource.to_xml()
            item_el.append(res_el)

        # descriptor
        for descriptor in self.descriptors:
            desc_el = descriptor.to_xml()
            item_el.append(desc_el)

        return item_el

    def __getattr__(self, name: str) -> Any:
        """Get attribute, modifying case as needed."""
        if name == "resources":
            return getattr(self, "res")
        if name in self.__dict__:
            return self.__dict__[name]
        cleaned_name = didl_property_key(name)
        if cleaned_name not in self.__dict__:
            raise AttributeError(name)
        return self.__dict__[cleaned_name]

    def __setattr__(self, name: str, value: Any) -> None:
        """Set attribute, modifying case as needed."""
        if name not in self.__dict__:
            # Redirect to the lower_camel_case version if it's already set,
            # which is the case for all defined didl properties.
            cleaned_name = didl_property_key(name)
            if cleaned_name in self.__dict__:
                name = cleaned_name
        self.__dict__[name] = value

    def __repr__(self) -> str:
        """Evaluatable string representation of this object."""
        class_name = type(self).__name__
        attr = ", ".join(
            f"{key}={val!r}"
            for key, val in self.__dict__.items()
            if key not in ("class", "xml_el")
        )
        return f"{class_name}({attr})"


# region: items
class Item(DidlObject):
    """DIDL Item."""

    # pylint: disable=too-few-public-methods

    tag = "item"
    upnp_class = "object.item"
    didl_properties_defs = DidlObject.didl_properties_defs + [
        ("didl_lite", "@refID", "O"),  # actually, R, but ignore for now
        ("upnp", "bookmarkID", "O"),
    ]


class ImageItem(Item):
    """DIDL Image Item."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.imageItem"
    didl_properties_defs = Item.didl_properties_defs + [
        ("upnp", "longDescription", "O"),
        ("upnp", "storageMedium", "O"),
        ("upnp", "rating", "O"),
        ("dc", "description", "O"),
        ("dc", "publisher", "O"),
        ("dc", "date", "O"),
        ("dc", "rights", "O"),
    ]


class Photo(ImageItem):
    """DIDL Photo."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.imageItem.photo"
    didl_properties_defs = ImageItem.didl_properties_defs + [
        ("upnp", "album", "O"),
    ]


class AudioItem(Item):
    """DIDL Audio Item."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.audioItem"
    didl_properties_defs = Item.didl_properties_defs + [
        ("upnp", "genre", "O"),
        ("dc", "description", "O"),
        ("upnp", "longDescription", "O"),
        ("dc", "publisher", "O"),
        ("dc", "language", "O"),
        ("dc", "relation", "O"),
        ("dc", "rights", "O"),
    ]


class MusicTrack(AudioItem):
    """DIDL Music Track."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.audioItem.musicTrack"
    didl_properties_defs = AudioItem.didl_properties_defs + [
        ("upnp", "artist", "O"),
        ("upnp", "album", "O"),
        ("upnp", "originalTrackNumber", "O"),
        ("upnp", "playlist", "O"),
        ("upnp", "storageMedium", "O"),
        ("dc", "contributor", "O"),
        ("dc", "date", "O"),
    ]


class AudioBroadcast(AudioItem):
    """DIDL Audio Broadcast."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.audioItem.audioBroadcast"
    didl_properties_defs = AudioItem.didl_properties_defs + [
        ("upnp", "region", "O"),
        ("upnp", "radioCallSign", "O"),
        ("upnp", "radioStationID", "O"),
        ("upnp", "radioBand", "O"),
        ("upnp", "channelNr", "O"),
        ("upnp", "signalStrength", "O"),
        ("upnp", "signalLocked", "O"),
        ("upnp", "tuned", "O"),
        ("upnp", "recordable", "O"),
    ]


class AudioBook(AudioItem):
    """DIDL Audio Book."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.audioItem.audioBook"
    didl_properties_defs = AudioItem.didl_properties_defs + [
        ("upnp", "storageMedium", "O"),
        ("upnp", "producer", "O"),
        ("dc", "contributor", "O"),
        ("dc", "date", "O"),
    ]


class VideoItem(Item):
    """DIDL Video Item."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.videoItem"
    didl_properties_defs = Item.didl_properties_defs + [
        ("upnp", "genre", "O"),
        ("upnp", "genre@id", "O"),
        ("upnp", "genre@type", "O"),
        ("upnp", "longDescription", "O"),
        ("upnp", "producer", "O"),
        ("upnp", "rating", "O"),
        ("upnp", "actor", "O"),
        ("upnp", "director", "O"),
        ("dc", "description", "O"),
        ("dc", "publisher", "O"),
        ("dc", "language", "O"),
        ("dc", "relation", "O"),
        ("upnp", "playbackCount", "O"),
        ("upnp", "lastPlaybackTime", "O"),
        ("upnp", "lastPlaybackPosition", "O"),
        ("upnp", "recordedDayOfWeek", "O"),
        ("upnp", "srsRecordScheduleID", "O"),
    ]


class Movie(VideoItem):
    """DIDL Movie."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.videoItem.movie"
    didl_properties_defs = VideoItem.didl_properties_defs + [
        ("upnp", "storageMedium", "O"),
        ("upnp", "DVDRegionCode", "O"),
        ("upnp", "channelName", "O"),
        ("upnp", "scheduledStartTime", "O"),
        ("upnp", "scheduledEndTime", "O"),
        ("upnp", "programTitle", "O"),
        ("upnp", "seriesTitle", "O"),
        ("upnp", "episodeCount", "O"),
        ("upnp", "episodeNumber", "O"),
        ("upnp", "episodeSeason", "O"),
    ]


class VideoBroadcast(VideoItem):
    """DIDL Video Broadcast."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.videoItem.videoBroadcast"
    didl_properties_defs = VideoItem.didl_properties_defs + [
        ("upnp", "icon", "O"),
        ("upnp", "region", "O"),
        ("upnp", "channelNr", "O"),
        ("upnp", "signalStrength", "O"),
        ("upnp", "signalLocked", "O"),
        ("upnp", "tuned", "O"),
        ("upnp", "recordable", "O"),
        ("upnp", "callSign", "O"),
        ("upnp", "price", "O"),
        ("upnp", "payPerView", "O"),
    ]


class MusicVideoClip(VideoItem):
    """DIDL Music Video Clip."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.videoItem.musicVideoClip"
    didl_properties_defs = VideoItem.didl_properties_defs + [
        ("upnp", "artist", "O"),
        ("upnp", "storageMedium", "O"),
        ("upnp", "album", "O"),
        ("upnp", "scheduledStartTime", "O"),
        ("upnp", "scheduledStopTime", "O"),
        # ('upnp', 'director', 'O'),  # duplicate in standard
        ("dc", "contributor", "O"),
        ("dc", "date", "O"),
    ]


class PlaylistItem(Item):
    """DIDL Playlist Item."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.playlistItem"
    didl_properties_defs = Item.didl_properties_defs + [
        ("upnp", "artist", "O"),
        ("upnp", "genre", "O"),
        ("upnp", "longDescription", "O"),
        ("upnp", "storageMedium", "O"),
        ("dc", "description", "O"),
        ("dc", "date", "O"),
        ("dc", "language", "O"),
    ]


class TextItem(Item):
    """DIDL Text Item."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.textItem"
    didl_properties_defs = Item.didl_properties_defs + [
        ("upnp", "author", "O"),
        ("upnp", "res@protection", "O"),
        ("upnp", "longDescription", "O"),
        ("upnp", "storageMedium", "O"),
        ("upnp", "rating", "O"),
        ("dc", "description", "O"),
        ("dc", "publisher", "O"),
        ("dc", "contributor", "O"),
        ("dc", "date", "O"),
        ("dc", "relation", "O"),
        ("dc", "language", "O"),
        ("dc", "rights", "O"),
    ]


class BookmarkItem(Item):
    """DIDL Bookmark Item."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.bookmarkItem"
    didl_properties_defs = Item.didl_properties_defs + [
        ("upnp", "bookmarkedObjectID", "R"),
        ("upnp", "neverPlayable", "O"),
        ("upnp", "deviceUDN", "R"),
        ("upnp", "serviceType", "R"),
        ("upnp", "serviceId", "R"),
        ("dc", "date", "O"),
        ("dc", "stateVariableCollection", "R"),
    ]


class EpgItem(Item):
    """DIDL EPG Item."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.epgItem"
    didl_properties_defs = Item.didl_properties_defs + [
        ("upnp", "channelGroupName", "O"),
        ("upnp", "channelGroupName@id", "O"),
        ("upnp", "epgProviderName", "O"),
        ("upnp", "serviceProvider", "O"),
        ("upnp", "channelName", "O"),
        ("upnp", "channelNr", "O"),
        ("upnp", "programTitle", "O"),
        ("upnp", "seriesTitle", "O"),
        ("upnp", "programID", "O"),
        ("upnp", "programID@type", "O"),
        ("upnp", "seriesID", "O"),
        ("upnp", "seriesID@type", "O"),
        ("upnp", "channelID", "O"),
        ("upnp", "channelID@type", "O"),
        ("upnp", "channelID@distriNetworkName", "O"),
        ("upnp", "channelID@distriNetworkID", "O"),
        ("upnp", "episodeType", "O"),
        ("upnp", "episodeCount", "O"),
        ("upnp", "episodeNumber", "O"),
        ("upnp", "episodeSeason", "O"),
        ("upnp", "programCode", "O"),
        ("upnp", "programCode@type", "O"),
        ("upnp", "rating", "O"),
        ("upnp", "rating@type", "O"),
        ("upnp", "rating@advice", "O"),
        ("upnp", "rating@equivalentAge", "O"),
        ("upnp", "recommendationID", "O"),
        ("upnp", "recommendationID@type", "O"),
        ("upnp", "genre", "O"),
        ("upnp", "genre@id", "O"),
        ("upnp", "genre@extended", "O"),
        ("upnp", "artist", "O"),
        ("upnp", "artist@role", "O"),
        ("upnp", "actor", "O"),
        ("upnp", "actor@role", "O"),
        ("upnp", "author", "O"),
        ("upnp", "author@role", "O"),
        ("upnp", "producer", "O"),
        ("upnp", "director", "O"),
        ("dc", "publisher", "O"),
        ("dc", "contributor", "O"),
        ("upnp", "callSign", "O"),
        ("upnp", "networkAffiliation", "O"),
        # ('upnp', 'serviceProvider', 'O'),  # duplicate in standard
        ("upnp", "price", "O"),
        ("upnp", "price@currency", "O"),
        ("upnp", "payPerView", "O"),
        # ('upnp', 'epgProviderName', 'O'),  # duplicate in standard
        ("dc", "description", "O"),
        ("upnp", "longDescription", "O"),
        ("upnp", "icon", "O"),
        ("upnp", "region", "O"),
        ("upnp", "rights", "O"),
        ("dc", "language", "O"),
        ("dc", "relation", "O"),
        ("upnp", "scheduledStartTime", "O"),
        ("upnp", "scheduledEndTime", "O"),
        ("upnp", "recordable", "O"),
        ("upnp", "foreignMetadata", "O"),
    ]


class AudioProgram(EpgItem):
    """DIDL Audio Program."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.epgItem.audioProgram"
    didl_properties_defs = Item.didl_properties_defs + [
        ("upnp", "radioCallSign", "O"),
        ("upnp", "radioStationID", "O"),
        ("upnp", "radioBand", "O"),
    ]


class VideoProgram(EpgItem):
    """DIDL Video Program."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.item.epgItem.videoProgram"
    didl_properties_defs = Item.didl_properties_defs + [
        ("upnp", "price", "O"),
        ("upnp", "price@currency", "O"),
        ("upnp", "payPerView", "O"),
    ]


# endregion


# region: containers
class Container(DidlObject, list):
    """DIDL Container."""

    # pylint: disable=too-few-public-methods

    tag = "container"
    upnp_class = "object.container"
    didl_properties_defs = DidlObject.didl_properties_defs + [
        ("didl_lite", "@childCount", "O"),
        ("upnp", "createClass", "O"),
        ("upnp", "searchClass", "O"),
        ("didl_lite", "@searchable", "O"),
        ("didl_lite", "@neverPlayable", "O"),
    ]

    def __init__(
        self,
        id: str = "",
        parent_id: str = "",
        descriptors: Optional[Sequence["Descriptor"]] = None,
        xml_el: Optional[ET.Element] = None,
        strict: bool = True,
        children: Iterable[DidlObject] = (),
        **properties: Any,
    ) -> None:
        """Initialize."""
        # pylint: disable=redefined-builtin,too-many-arguments,too-many-positional-arguments
        super().__init__(id, parent_id, descriptors, xml_el, strict, **properties)
        self.extend(children)

    @classmethod
    def from_xml(cls: Type[TC], xml_el: ET.Element, strict: bool = True) -> TC:
        """
        Initialize from an XML node.

        I.e., parse XML and return instance.
        """
        instance = super().from_xml(xml_el, strict)

        # add all children
        didl_objects = from_xml_el(xml_el, strict)
        instance.extend(didl_objects)  # pylint: disable=no-member

        return instance

    def to_xml(self) -> ET.Element:
        """Convert self to XML Element."""
        container_el = super().to_xml()

        for didl_object in self:
            didl_object_el = didl_object.to_xml()
            container_el.append(didl_object_el)

        return container_el

    def __repr__(self) -> str:
        """Evaluatable string representation of this object."""
        class_name = type(self).__name__
        attr = ", ".join(
            f"{key}={val!r}"
            for key, val in self.__dict__.items()
            if key not in ("class", "xml_el")
        )
        children_repr = ", ".join(repr(child) for child in self)
        return f"{class_name}({attr}, children=[{children_repr}])"


class Person(Container):
    """DIDL Person."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.person"
    didl_properties_defs = Container.didl_properties_defs + [
        ("dc", "language", "O"),
    ]


class MusicArtist(Person):
    """DIDL Music Artist."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.person.musicArtist"
    didl_properties_defs = Container.didl_properties_defs + [
        ("upnp", "genre", "O"),
        ("upnp", "artistDiscographyURI", "O"),
    ]


class PlaylistContainer(Container):
    """DIDL Playlist Container."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.playlistContainer"
    didl_properties_defs = Container.didl_properties_defs + [
        ("upnp", "artist", "O"),
        ("upnp", "genre", "O"),
        ("upnp", "longDescription", "O"),
        ("upnp", "producer", "O"),
        ("upnp", "storageMedium", "O"),
        ("dc", "description", "O"),
        ("dc", "contributor", "O"),
        ("dc", "date", "O"),
        ("dc", "language", "O"),
        ("dc", "rights", "O"),
    ]


class Album(Container):
    """DIDL Album."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.album"
    didl_properties_defs = Container.didl_properties_defs + [
        ("upnp", "storageMedium", "O"),
        ("dc", "longDescription", "O"),
        ("dc", "description", "O"),
        ("dc", "publisher", "O"),
        ("dc", "contributor", "O"),
        ("dc", "date", "O"),
        ("dc", "relation", "O"),
        ("dc", "rights", "O"),
    ]


class MusicAlbum(Album):
    """DIDL Music Album."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.album.musicAlbum"
    didl_properties_defs = Container.didl_properties_defs + [
        ("upnp", "artist", "O"),
        ("upnp", "genre", "O"),
        ("upnp", "producer", "O"),
        ("upnp", "albumArtURI", "O"),
        ("upnp", "toc", "O"),
    ]


class PhotoAlbum(Album):
    """DIDL Photo Album."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.album.photoAlbum"
    didl_properties_defs = Container.didl_properties_defs + []


class Genre(Container):
    """DIDL Genre."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.genre"
    didl_properties_defs = Container.didl_properties_defs + [
        ("upnp", "genre", "O"),
        ("upnp", "longDescription", "O"),
        ("dc", "description", "O"),
    ]


class MusicGenre(Genre):
    """DIDL Music Genre."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.genre.musicGenre"
    didl_properties_defs = Container.didl_properties_defs + []


class MovieGenre(Genre):
    """DIDL Movie Genre."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.genre.movieGenre"
    didl_properties_defs = Container.didl_properties_defs + []


class ChannelGroup(Container):
    """DIDL Channel Group."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.channelGroup"
    didl_properties_defs = Container.didl_properties_defs + [
        ("upnp", "channelGroupName", "O"),
        ("upnp", "channelGroupName@id", "O"),
        ("upnp", "epgProviderName", "O"),
        ("upnp", "serviceProvider", "O"),
        ("upnp", "icon", "O"),
        ("upnp", "region", "O"),
    ]


class AudioChannelGroup(ChannelGroup):
    """DIDL Audio Channel Group."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.channelGroup.audioChannelGroup"
    didl_properties_defs = Container.didl_properties_defs + []


class VideoChannelGroup(ChannelGroup):
    """DIDL Video Channel Group."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.channelGroup.videoChannelGroup"
    didl_properties_defs = Container.didl_properties_defs + []


class EpgContainer(Container):
    """DIDL EPG Container."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.epgContainer"
    didl_properties_defs = Container.didl_properties_defs + [
        ("upnp", "channelGroupName", "O"),
        ("upnp", "channelGroupName@id", "O"),
        ("upnp", "epgProviderName", "O"),
        ("upnp", "serviceProvider", "O"),
        ("upnp", "channelName", "O"),
        ("upnp", "channelNr", "O"),
        ("upnp", "channelID", "O"),
        ("upnp", "channelID@type", "O"),
        ("upnp", "radioCallSign", "O"),
        ("upnp", "radioStationID", "O"),
        ("upnp", "radioBand", "O"),
        ("upnp", "callSign", "O"),
        ("upnp", "networkAffiliation", "O"),
        # ('upnp', 'serviceProvider', 'O'),  # duplicate in standard
        ("upnp", "price", "O"),
        ("upnp", "price@currency", "O"),
        ("upnp", "payPerView", "O"),
        # ('upnp', 'epgProviderName', 'O'),  # duplicate in standard
        ("upnp", "icon", "O"),
        ("upnp", "region", "O"),
        ("dc", "language", "O"),
        ("dc", "relation", "O"),
        ("upnp", "dateTimeRange", "O"),
    ]


class StorageSystem(Container):
    """DIDL Storage System."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.storageSystem"
    didl_properties_defs = Container.didl_properties_defs + [
        ("upnp", "storageTotal", "R"),
        ("upnp", "storageUsed", "R"),
        ("upnp", "storageFree", "R"),
        ("upnp", "storageMaxPartition", "R"),
        ("upnp", "storageMedium", "R"),
    ]


class StorageVolume(Container):
    """DIDL Storage Volume."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.storageVolume"
    didl_properties_defs = Container.didl_properties_defs + [
        ("upnp", "storageTotal", "R"),
        ("upnp", "storageUsed", "R"),
        ("upnp", "storageFree", "R"),
        ("upnp", "storageMedium", "R"),
    ]


class StorageFolder(Container):
    """DIDL Storage Folder."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.storageFolder"
    didl_properties_defs = Container.didl_properties_defs + [
        ("upnp", "storageUsed", "R"),
    ]


class BookmarkFolder(Container):
    """DIDL Bookmark Folder."""

    # pylint: disable=too-few-public-methods

    upnp_class = "object.container.bookmarkFolder"
    didl_properties_defs = Container.didl_properties_defs + [
        ("upnp", "genre", "O"),
        ("upnp", "longDescription", "O"),
        ("dc", "description", "O"),
    ]


# endregion


class Resource:
    """DIDL Resource."""

    # pylint: disable=too-few-public-methods,too-many-instance-attributes

    def __init__(
        self,
        uri: Optional[str],
        protocol_info: Optional[str],
        import_uri: Optional[str] = None,
        size: Optional[str] = None,
        duration: Optional[str] = None,
        bitrate: Optional[str] = None,
        sample_frequency: Optional[str] = None,
        bits_per_sample: Optional[str] = None,
        nr_audio_channels: Optional[str] = None,
        resolution: Optional[str] = None,
        color_depth: Optional[str] = None,
        protection: Optional[str] = None,
        xml_el: Optional[ET.Element] = None,
    ) -> None:
        """Initialize."""
        # pylint: disable=too-many-arguments,too-many-positional-arguments
        self.uri = uri
        self.protocol_info = protocol_info
        self.import_uri = import_uri
        self.size = size
        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
        self.xml_el = xml_el

    @classmethod
    def from_xml(cls: Type[TR], xml_el: ET.Element) -> TR:
        """Initialize from an XML node."""
        uri = xml_el.text
        protocol_info = xml_el.attrib.get("protocolInfo")
        import_uri = xml_el.attrib.get("importUri")
        size = xml_el.attrib.get("size")
        duration = xml_el.attrib.get("duration")
        bitrate = xml_el.attrib.get("bitrate")
        sample_frequency = xml_el.attrib.get("sampleFrequency")
        bits_per_sample = xml_el.attrib.get("bitsPerSample")
        nr_audio_channels = xml_el.attrib.get("nrAudioChannels")
        resolution = xml_el.attrib.get("resolution")
        color_depth = xml_el.attrib.get("colorDepth")
        protection = xml_el.attrib.get("protection")
        return cls(
            uri,
            protocol_info=protocol_info,
            import_uri=import_uri,
            size=size,
            duration=duration,
            bitrate=bitrate,
            sample_frequency=sample_frequency,
            bits_per_sample=bits_per_sample,
            nr_audio_channels=nr_audio_channels,
            resolution=resolution,
            color_depth=color_depth,
            protection=protection,
            xml_el=xml_el,
        )

    def to_xml(self) -> ET.Element:
        """Convert self to XML."""
        attribs = {
            "protocolInfo": self.protocol_info or "",
        }
        res_el = ET.Element("res", attribs)
        res_el.text = self.uri
        return res_el

    def __repr__(self) -> str:
        """Evaluatable string representation of this object."""
        class_name = type(self).__name__
        attr = ", ".join(
            f"{key}={val!r}"
            for key, val in self.__dict__.items()
            if val is not None and key != "xml_el"
        )
        return f"{class_name}({attr})"


class Descriptor:
    """DIDL Descriptor."""

    def __init__(
        self,
        id: str,
        name_space: str,
        type: Optional[str] = None,
        text: Optional[str] = None,
        xml_el: Optional[ET.Element] = None,
    ) -> None:
        """Initialize."""
        # pylint: disable=invalid-name,redefined-builtin,too-many-arguments,too-many-positional-arguments
        self.id = id
        self.name_space = name_space
        self.type = type
        self.text = text
        self.xml_el = xml_el

    @classmethod
    def from_xml(cls: Type[TD], xml_el: ET.Element) -> TD:
        """Initialize from an XML node."""
        id_ = xml_el.attrib["id"]
        name_space = xml_el.attrib["nameSpace"]
        type_ = xml_el.attrib.get("type")
        text = xml_el.text
        return cls(id_, name_space, type=type_, text=text, xml_el=xml_el)

    def to_xml(self) -> ET.Element:
        """Convert self to XML."""
        attribs = {
            "id": self.id,
            "nameSpace": self.name_space,
        }
        if self.type is not None:
            attribs["type"] = self.type
        desc_el = ET.Element("desc", attribs)
        desc_el.text = self.text
        return desc_el

    def __getattr__(self, name: str) -> Any:
        """Get attribute."""
        if name not in self.__dict__:
            raise AttributeError(name)
        return self.__dict__[name]

    def __repr__(self) -> str:
        """Evaluatable string representation of this object."""
        class_name = type(self).__name__
        attr = ", ".join(
            f"{key}={val!r}"
            for key, val in self.__dict__.items()
            if val is not None and key != "xml_el"
        )
        return f"{class_name}({attr})"


# endregion


def to_xml_string(*objects: DidlObject) -> bytes:
    """Convert items to DIDL-Lite XML string."""
    root_el = ET.Element("DIDL-Lite", {})
    root_el.attrib["xmlns"] = NAMESPACES["didl_lite"]
    root_el.attrib["xmlns:dc"] = NAMESPACES["dc"]
    root_el.attrib["xmlns:upnp"] = NAMESPACES["upnp"]
    root_el.attrib["xmlns:sec"] = NAMESPACES["sec"]

    for didl_object in objects:
        didl_object_el = didl_object.to_xml()
        root_el.append(didl_object_el)

    return ET.tostring(root_el)


def from_xml_string(
    xml_string: str, strict: bool = True
) -> List[Union[DidlObject, Descriptor]]:
    """Parse DIDL-Lite XML string."""
    if not strict:
        # Find all prefixes used in tags, e.g., <prefix:tag ...>
        used_prefixes = set(re.findall(r"<([a-zA-Z0-9]+):", xml_string))

        # Find all defined namespaces, e.g., xmlns:prefix=...
        defined_prefixes = set(re.findall(r"xmlns:([a-zA-Z0-9]+)=", xml_string))

        # Identify prefixes used but not defined.
        missing_prefixes = (
            used_prefixes - defined_prefixes - {"DIDL-Lite", "dc", "upnp", "dlna"}
        )

        # Remove the "if missing_prefixes:" line and just keep the for loop
        for prefix in missing_prefixes:
            dlna_ns = 'xmlns:dlna="urn:schemas-dlna-org:metadata-1-0/"'
            if dlna_ns in xml_string:
                replacement = f'{dlna_ns} xmlns:{prefix}="http://tempuri.org/{prefix}/"'
                xml_string = xml_string.replace(dlna_ns, replacement)

    # Proceed with parsing using the (potentially) patched xml_string
    xml_el = defusedxml.ElementTree.fromstring(xml_string)
    return from_xml_el(xml_el, strict)


def from_xml_el(
    xml_el: ET.Element, strict: bool = True
) -> List[Union[DidlObject, Descriptor]]:
    """Convert XML Element to DIDL Objects."""
    didl_objects = []  # type: List[Union[DidlObject, Descriptor]]

    # items and containers, in order
    for child_el in xml_el:
        if child_el.tag != expand_namespace_tag(
            "didl_lite:item"
        ) and child_el.tag != expand_namespace_tag("didl_lite:container"):
            continue

        # construct item
        upnp_class = child_el.find("./upnp:class", NAMESPACES)
        if upnp_class is None or not upnp_class.text:
            if strict:
                continue
            # WiiM Pro and possibly other Linkplay devices emit
            # upnp_class above the item element instead of inside it
            upnp_class = xml_el.find("./upnp:class", NAMESPACES)
            if upnp_class is None or not upnp_class.text:
                continue
        didl_object_type = type_by_upnp_class(upnp_class.text, strict)
        if didl_object_type is None:
            if strict:
                raise DidlLiteException(f"upnp:class {upnp_class.text} is unknown")
            continue
        didl_object = didl_object_type.from_xml(child_el, strict)
        didl_objects.append(didl_object)

    # descriptors
    for desc_el in xml_el.findall("./didl_lite:desc", NAMESPACES):
        desc = Descriptor.from_xml(desc_el)
        didl_objects.append(desc)

    return didl_objects


# upnp_class to python type mapping
def type_by_upnp_class(
    upnp_class: str, strict: bool = True
) -> Optional[Type[DidlObject]]:
    """Get DidlObject-type by upnp_class.

    When strict is False, the upnp_class lookup will be done ignoring string
    case.
    """
    if strict:
        return _upnp_class_map.get(upnp_class)
    return _upnp_class_map_lowercase.get(upnp_class.lower())