]>
Commit | Line | Data |
---|---|---|
998c57d2 JB |
1 | #!/usr/bin/env python3 |
2 | ||
3 | # Copyright (C) 2021 Igalia S.L. | |
4 | # | |
5 | # This library is free software; you can redistribute it and/or | |
6 | # modify it under the terms of the GNU Lesser General Public | |
7 | # License as published by the Free Software Foundation; either | |
8 | # version 2.1 of the License, or (at your option) any later version. | |
9 | # | |
10 | # This library is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | # Lesser General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU Lesser General Public | |
16 | # License along with this library; if not, write to the Free Software | |
17 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 | |
18 | # USA | |
19 | ||
20 | import collections.abc | |
21 | ||
22 | from ..module import get_introspection_module | |
23 | from ..overrides import override | |
24 | ||
25 | Soup = get_introspection_module('Soup') | |
26 | ||
27 | __all__ = [] | |
28 | ||
29 | if Soup._version == '3.0': | |
30 | ||
31 | class MessageHeadersIter(Soup.MessageHeadersIter): | |
32 | ||
33 | def __next__(self): | |
34 | ret, key, value = self.next() | |
35 | if ret is True: | |
36 | return key, value | |
37 | else: | |
38 | raise StopIteration | |
39 | ||
40 | @override | |
41 | class MessageHeaders(Soup.MessageHeaders): | |
42 | ||
43 | def __getitem__(self, key): | |
44 | if not isinstance(key, str): | |
45 | raise TypeError | |
46 | ||
47 | value = self.get_one(key) | |
48 | if value is None: | |
49 | raise KeyError | |
50 | ||
51 | return value | |
52 | ||
53 | def __delitem__(self, key): | |
54 | if not isinstance(key, str): | |
55 | raise TypeError | |
56 | ||
57 | self.remove(key) | |
58 | ||
59 | def __setitem__(self, key, value): | |
60 | if not isinstance(key, str) or not isinstance(value, str): | |
61 | raise TypeError | |
62 | ||
63 | self.replace(key, value) | |
64 | ||
65 | def __contains__(self, item): | |
66 | if not isinstance(item, str): | |
67 | raise TypeError | |
68 | ||
69 | return self.get_one(item) is not None | |
70 | ||
71 | def __iter__(self): | |
72 | return MessageHeadersIter.init(self) | |
73 | ||
74 | def __len__(self): | |
75 | return len(self.keys()) | |
76 | ||
77 | def keys(self): | |
78 | return [k for k, _ in self] | |
79 | ||
80 | def values(self): | |
81 | return [v for _, v in self] | |
82 | ||
83 | def items(self): | |
84 | return {k: v for k, v in self} | |
85 | ||
86 | def get(self, default=None): | |
87 | return collections.abc.Mapping.get(self, default) | |
88 | ||
89 | def pop(self, key): | |
90 | return collections.abc.MutableMapping.pop(self, key) | |
91 | ||
92 | def update(self, key, value): | |
93 | return collections.abc.MutableMapping.update(self, key, value) | |
94 | ||
95 | def setdefault(self, key, default=None): | |
96 | return collections.abc.MutableMapping.setdefault(self, key, default) | |
97 | ||
98 | def popitem(self): | |
99 | return collections.abc.MutableMapping.popitem(self) | |
100 | ||
101 | __all__.append('MessageHeaders') | |
102 | __all__.append('MessageHeadersIter') | |
103 | collections.abc.Iterable.register(MessageHeaders) | |
104 | collections.abc.Iterator.register(MessageHeadersIter) | |
105 | collections.abc.Mapping.register(MessageHeaders) | |
106 | collections.abc.MutableMapping.register(MessageHeaders) | |
107 | collections.abc.Container.register(MessageHeaders) | |
108 | collections.abc.Sized.register(MessageHeaders) |