Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/urllib3/_collections.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
1 from __future__ import absolute_import | |
2 | |
3 try: | |
4 from collections.abc import Mapping, MutableMapping | |
5 except ImportError: | |
6 from collections import Mapping, MutableMapping | |
7 try: | |
8 from threading import RLock | |
9 except ImportError: # Platform-specific: No threads available | |
10 | |
11 class RLock: | |
12 def __enter__(self): | |
13 pass | |
14 | |
15 def __exit__(self, exc_type, exc_value, traceback): | |
16 pass | |
17 | |
18 | |
19 from collections import OrderedDict | |
20 from .exceptions import InvalidHeader | |
21 from .packages.six import iterkeys, itervalues, PY3 | |
22 | |
23 | |
24 __all__ = ["RecentlyUsedContainer", "HTTPHeaderDict"] | |
25 | |
26 | |
27 _Null = object() | |
28 | |
29 | |
30 class RecentlyUsedContainer(MutableMapping): | |
31 """ | |
32 Provides a thread-safe dict-like container which maintains up to | |
33 ``maxsize`` keys while throwing away the least-recently-used keys beyond | |
34 ``maxsize``. | |
35 | |
36 :param maxsize: | |
37 Maximum number of recent elements to retain. | |
38 | |
39 :param dispose_func: | |
40 Every time an item is evicted from the container, | |
41 ``dispose_func(value)`` is called. Callback which will get called | |
42 """ | |
43 | |
44 ContainerCls = OrderedDict | |
45 | |
46 def __init__(self, maxsize=10, dispose_func=None): | |
47 self._maxsize = maxsize | |
48 self.dispose_func = dispose_func | |
49 | |
50 self._container = self.ContainerCls() | |
51 self.lock = RLock() | |
52 | |
53 def __getitem__(self, key): | |
54 # Re-insert the item, moving it to the end of the eviction line. | |
55 with self.lock: | |
56 item = self._container.pop(key) | |
57 self._container[key] = item | |
58 return item | |
59 | |
60 def __setitem__(self, key, value): | |
61 evicted_value = _Null | |
62 with self.lock: | |
63 # Possibly evict the existing value of 'key' | |
64 evicted_value = self._container.get(key, _Null) | |
65 self._container[key] = value | |
66 | |
67 # If we didn't evict an existing value, we might have to evict the | |
68 # least recently used item from the beginning of the container. | |
69 if len(self._container) > self._maxsize: | |
70 _key, evicted_value = self._container.popitem(last=False) | |
71 | |
72 if self.dispose_func and evicted_value is not _Null: | |
73 self.dispose_func(evicted_value) | |
74 | |
75 def __delitem__(self, key): | |
76 with self.lock: | |
77 value = self._container.pop(key) | |
78 | |
79 if self.dispose_func: | |
80 self.dispose_func(value) | |
81 | |
82 def __len__(self): | |
83 with self.lock: | |
84 return len(self._container) | |
85 | |
86 def __iter__(self): | |
87 raise NotImplementedError( | |
88 "Iteration over this class is unlikely to be threadsafe." | |
89 ) | |
90 | |
91 def clear(self): | |
92 with self.lock: | |
93 # Copy pointers to all values, then wipe the mapping | |
94 values = list(itervalues(self._container)) | |
95 self._container.clear() | |
96 | |
97 if self.dispose_func: | |
98 for value in values: | |
99 self.dispose_func(value) | |
100 | |
101 def keys(self): | |
102 with self.lock: | |
103 return list(iterkeys(self._container)) | |
104 | |
105 | |
106 class HTTPHeaderDict(MutableMapping): | |
107 """ | |
108 :param headers: | |
109 An iterable of field-value pairs. Must not contain multiple field names | |
110 when compared case-insensitively. | |
111 | |
112 :param kwargs: | |
113 Additional field-value pairs to pass in to ``dict.update``. | |
114 | |
115 A ``dict`` like container for storing HTTP Headers. | |
116 | |
117 Field names are stored and compared case-insensitively in compliance with | |
118 RFC 7230. Iteration provides the first case-sensitive key seen for each | |
119 case-insensitive pair. | |
120 | |
121 Using ``__setitem__`` syntax overwrites fields that compare equal | |
122 case-insensitively in order to maintain ``dict``'s api. For fields that | |
123 compare equal, instead create a new ``HTTPHeaderDict`` and use ``.add`` | |
124 in a loop. | |
125 | |
126 If multiple fields that are equal case-insensitively are passed to the | |
127 constructor or ``.update``, the behavior is undefined and some will be | |
128 lost. | |
129 | |
130 >>> headers = HTTPHeaderDict() | |
131 >>> headers.add('Set-Cookie', 'foo=bar') | |
132 >>> headers.add('set-cookie', 'baz=quxx') | |
133 >>> headers['content-length'] = '7' | |
134 >>> headers['SET-cookie'] | |
135 'foo=bar, baz=quxx' | |
136 >>> headers['Content-Length'] | |
137 '7' | |
138 """ | |
139 | |
140 def __init__(self, headers=None, **kwargs): | |
141 super(HTTPHeaderDict, self).__init__() | |
142 self._container = OrderedDict() | |
143 if headers is not None: | |
144 if isinstance(headers, HTTPHeaderDict): | |
145 self._copy_from(headers) | |
146 else: | |
147 self.extend(headers) | |
148 if kwargs: | |
149 self.extend(kwargs) | |
150 | |
151 def __setitem__(self, key, val): | |
152 self._container[key.lower()] = [key, val] | |
153 return self._container[key.lower()] | |
154 | |
155 def __getitem__(self, key): | |
156 val = self._container[key.lower()] | |
157 return ", ".join(val[1:]) | |
158 | |
159 def __delitem__(self, key): | |
160 del self._container[key.lower()] | |
161 | |
162 def __contains__(self, key): | |
163 return key.lower() in self._container | |
164 | |
165 def __eq__(self, other): | |
166 if not isinstance(other, Mapping) and not hasattr(other, "keys"): | |
167 return False | |
168 if not isinstance(other, type(self)): | |
169 other = type(self)(other) | |
170 return dict((k.lower(), v) for k, v in self.itermerged()) == dict( | |
171 (k.lower(), v) for k, v in other.itermerged() | |
172 ) | |
173 | |
174 def __ne__(self, other): | |
175 return not self.__eq__(other) | |
176 | |
177 if not PY3: # Python 2 | |
178 iterkeys = MutableMapping.iterkeys | |
179 itervalues = MutableMapping.itervalues | |
180 | |
181 __marker = object() | |
182 | |
183 def __len__(self): | |
184 return len(self._container) | |
185 | |
186 def __iter__(self): | |
187 # Only provide the originally cased names | |
188 for vals in self._container.values(): | |
189 yield vals[0] | |
190 | |
191 def pop(self, key, default=__marker): | |
192 """D.pop(k[,d]) -> v, remove specified key and return the corresponding value. | |
193 If key is not found, d is returned if given, otherwise KeyError is raised. | |
194 """ | |
195 # Using the MutableMapping function directly fails due to the private marker. | |
196 # Using ordinary dict.pop would expose the internal structures. | |
197 # So let's reinvent the wheel. | |
198 try: | |
199 value = self[key] | |
200 except KeyError: | |
201 if default is self.__marker: | |
202 raise | |
203 return default | |
204 else: | |
205 del self[key] | |
206 return value | |
207 | |
208 def discard(self, key): | |
209 try: | |
210 del self[key] | |
211 except KeyError: | |
212 pass | |
213 | |
214 def add(self, key, val): | |
215 """Adds a (name, value) pair, doesn't overwrite the value if it already | |
216 exists. | |
217 | |
218 >>> headers = HTTPHeaderDict(foo='bar') | |
219 >>> headers.add('Foo', 'baz') | |
220 >>> headers['foo'] | |
221 'bar, baz' | |
222 """ | |
223 key_lower = key.lower() | |
224 new_vals = [key, val] | |
225 # Keep the common case aka no item present as fast as possible | |
226 vals = self._container.setdefault(key_lower, new_vals) | |
227 if new_vals is not vals: | |
228 vals.append(val) | |
229 | |
230 def extend(self, *args, **kwargs): | |
231 """Generic import function for any type of header-like object. | |
232 Adapted version of MutableMapping.update in order to insert items | |
233 with self.add instead of self.__setitem__ | |
234 """ | |
235 if len(args) > 1: | |
236 raise TypeError( | |
237 "extend() takes at most 1 positional " | |
238 "arguments ({0} given)".format(len(args)) | |
239 ) | |
240 other = args[0] if len(args) >= 1 else () | |
241 | |
242 if isinstance(other, HTTPHeaderDict): | |
243 for key, val in other.iteritems(): | |
244 self.add(key, val) | |
245 elif isinstance(other, Mapping): | |
246 for key in other: | |
247 self.add(key, other[key]) | |
248 elif hasattr(other, "keys"): | |
249 for key in other.keys(): | |
250 self.add(key, other[key]) | |
251 else: | |
252 for key, value in other: | |
253 self.add(key, value) | |
254 | |
255 for key, value in kwargs.items(): | |
256 self.add(key, value) | |
257 | |
258 def getlist(self, key, default=__marker): | |
259 """Returns a list of all the values for the named field. Returns an | |
260 empty list if the key doesn't exist.""" | |
261 try: | |
262 vals = self._container[key.lower()] | |
263 except KeyError: | |
264 if default is self.__marker: | |
265 return [] | |
266 return default | |
267 else: | |
268 return vals[1:] | |
269 | |
270 # Backwards compatibility for httplib | |
271 getheaders = getlist | |
272 getallmatchingheaders = getlist | |
273 iget = getlist | |
274 | |
275 # Backwards compatibility for http.cookiejar | |
276 get_all = getlist | |
277 | |
278 def __repr__(self): | |
279 return "%s(%s)" % (type(self).__name__, dict(self.itermerged())) | |
280 | |
281 def _copy_from(self, other): | |
282 for key in other: | |
283 val = other.getlist(key) | |
284 if isinstance(val, list): | |
285 # Don't need to convert tuples | |
286 val = list(val) | |
287 self._container[key.lower()] = [key] + val | |
288 | |
289 def copy(self): | |
290 clone = type(self)() | |
291 clone._copy_from(self) | |
292 return clone | |
293 | |
294 def iteritems(self): | |
295 """Iterate over all header lines, including duplicate ones.""" | |
296 for key in self: | |
297 vals = self._container[key.lower()] | |
298 for val in vals[1:]: | |
299 yield vals[0], val | |
300 | |
301 def itermerged(self): | |
302 """Iterate over all headers, merging duplicate ones together.""" | |
303 for key in self: | |
304 val = self._container[key.lower()] | |
305 yield val[0], ", ".join(val[1:]) | |
306 | |
307 def items(self): | |
308 return list(self.iteritems()) | |
309 | |
310 @classmethod | |
311 def from_httplib(cls, message): # Python 2 | |
312 """Read headers from a Python 2 httplib message object.""" | |
313 # python2.7 does not expose a proper API for exporting multiheaders | |
314 # efficiently. This function re-reads raw lines from the message | |
315 # object and extracts the multiheaders properly. | |
316 obs_fold_continued_leaders = (" ", "\t") | |
317 headers = [] | |
318 | |
319 for line in message.headers: | |
320 if line.startswith(obs_fold_continued_leaders): | |
321 if not headers: | |
322 # We received a header line that starts with OWS as described | |
323 # in RFC-7230 S3.2.4. This indicates a multiline header, but | |
324 # there exists no previous header to which we can attach it. | |
325 raise InvalidHeader( | |
326 "Header continuation with no previous header: %s" % line | |
327 ) | |
328 else: | |
329 key, value = headers[-1] | |
330 headers[-1] = (key, value + " " + line.strip()) | |
331 continue | |
332 | |
333 key, value = line.split(":", 1) | |
334 headers.append((key, value.strip())) | |
335 | |
336 return cls(headers) |