Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/future/backports/urllib/response.py @ 0:d30785e31577 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
| author | guerler |
|---|---|
| date | Fri, 31 Jul 2020 00:18:57 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:d30785e31577 |
|---|---|
| 1 """Response classes used by urllib. | |
| 2 | |
| 3 The base class, addbase, defines a minimal file-like interface, | |
| 4 including read() and readline(). The typical response object is an | |
| 5 addinfourl instance, which defines an info() method that returns | |
| 6 headers and a geturl() method that returns the url. | |
| 7 """ | |
| 8 from __future__ import absolute_import, division, unicode_literals | |
| 9 from future.builtins import object | |
| 10 | |
| 11 class addbase(object): | |
| 12 """Base class for addinfo and addclosehook.""" | |
| 13 | |
| 14 # XXX Add a method to expose the timeout on the underlying socket? | |
| 15 | |
| 16 def __init__(self, fp): | |
| 17 # TODO(jhylton): Is there a better way to delegate using io? | |
| 18 self.fp = fp | |
| 19 self.read = self.fp.read | |
| 20 self.readline = self.fp.readline | |
| 21 # TODO(jhylton): Make sure an object with readlines() is also iterable | |
| 22 if hasattr(self.fp, "readlines"): | |
| 23 self.readlines = self.fp.readlines | |
| 24 if hasattr(self.fp, "fileno"): | |
| 25 self.fileno = self.fp.fileno | |
| 26 else: | |
| 27 self.fileno = lambda: None | |
| 28 | |
| 29 def __iter__(self): | |
| 30 # Assigning `__iter__` to the instance doesn't work as intended | |
| 31 # because the iter builtin does something like `cls.__iter__(obj)` | |
| 32 # and thus fails to find the _bound_ method `obj.__iter__`. | |
| 33 # Returning just `self.fp` works for built-in file objects but | |
| 34 # might not work for general file-like objects. | |
| 35 return iter(self.fp) | |
| 36 | |
| 37 def __repr__(self): | |
| 38 return '<%s at %r whose fp = %r>' % (self.__class__.__name__, | |
| 39 id(self), self.fp) | |
| 40 | |
| 41 def close(self): | |
| 42 if self.fp: | |
| 43 self.fp.close() | |
| 44 self.fp = None | |
| 45 self.read = None | |
| 46 self.readline = None | |
| 47 self.readlines = None | |
| 48 self.fileno = None | |
| 49 self.__iter__ = None | |
| 50 self.__next__ = None | |
| 51 | |
| 52 def __enter__(self): | |
| 53 if self.fp is None: | |
| 54 raise ValueError("I/O operation on closed file") | |
| 55 return self | |
| 56 | |
| 57 def __exit__(self, type, value, traceback): | |
| 58 self.close() | |
| 59 | |
| 60 class addclosehook(addbase): | |
| 61 """Class to add a close hook to an open file.""" | |
| 62 | |
| 63 def __init__(self, fp, closehook, *hookargs): | |
| 64 addbase.__init__(self, fp) | |
| 65 self.closehook = closehook | |
| 66 self.hookargs = hookargs | |
| 67 | |
| 68 def close(self): | |
| 69 if self.closehook: | |
| 70 self.closehook(*self.hookargs) | |
| 71 self.closehook = None | |
| 72 self.hookargs = None | |
| 73 addbase.close(self) | |
| 74 | |
| 75 class addinfo(addbase): | |
| 76 """class to add an info() method to an open file.""" | |
| 77 | |
| 78 def __init__(self, fp, headers): | |
| 79 addbase.__init__(self, fp) | |
| 80 self.headers = headers | |
| 81 | |
| 82 def info(self): | |
| 83 return self.headers | |
| 84 | |
| 85 class addinfourl(addbase): | |
| 86 """class to add info() and geturl() methods to an open file.""" | |
| 87 | |
| 88 def __init__(self, fp, headers, url, code=None): | |
| 89 addbase.__init__(self, fp) | |
| 90 self.headers = headers | |
| 91 self.url = url | |
| 92 self.code = code | |
| 93 | |
| 94 def info(self): | |
| 95 return self.headers | |
| 96 | |
| 97 def getcode(self): | |
| 98 return self.code | |
| 99 | |
| 100 def geturl(self): | |
| 101 return self.url | |
| 102 | |
| 103 del absolute_import, division, unicode_literals, object |
