3 # Copyright (C) 2021 Igalia S.L.
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.
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.
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
20 import collections.abc
22 from ..module import get_introspection_module
23 from ..overrides import override
25 Soup = get_introspection_module('Soup')
29 if Soup._version == '3.0':
31 class MessageHeadersIter(Soup.MessageHeadersIter):
34 ret, key, value = self.next()
41 class MessageHeaders(Soup.MessageHeaders):
43 def __getitem__(self, key):
44 if not isinstance(key, str):
47 value = self.get_one(key)
53 def __delitem__(self, key):
54 if not isinstance(key, str):
59 def __setitem__(self, key, value):
60 if not isinstance(key, str) or not isinstance(value, str):
63 self.replace(key, value)
65 def __contains__(self, item):
66 if not isinstance(item, str):
69 return self.get_one(item) is not None
72 return MessageHeadersIter.init(self)
75 return len(self.keys())
78 return [k for k, _ in self]
81 return [v for _, v in self]
84 return {k: v for k, v in self}
86 def get(self, default=None):
87 return collections.abc.Mapping.get(self, default)
90 return collections.abc.MutableMapping.pop(self, key)
92 def update(self, key, value):
93 return collections.abc.MutableMapping.update(self, key, value)
95 def setdefault(self, key, default=None):
96 return collections.abc.MutableMapping.setdefault(self, key, default)
99 return collections.abc.MutableMapping.popitem(self)
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)