1# Copyright (c) 2010-2024 openpyxl
2
3"""
4Utility list for top level containers that contain one type of element
5
6Provides the necessary API to read and write XML
7"""
8
9from openpyxl.xml.functions import Element
10
11
12class ElementList(list):
13
14
15 @property
16 def tagname(self):
17 raise NotImplementedError
18
19
20 @property
21 def expected_type(self):
22 raise NotImplementedError
23
24
25 @classmethod
26 def from_tree(cls, tree):
27 l = [cls.expected_type.from_tree(el) for el in tree]
28 return cls(l)
29
30
31 def to_tree(self):
32 container = Element(self.tagname)
33 for el in self:
34 container.append(el.to_tree())
35 return container
36
37
38 def append(self, value):
39 if not isinstance(value, self.expected_type):
40 raise TypeError(f"Value must of type {self.expected_type} {type(value)} provided")
41 super().append(value)