retrieve(url) returns (filename, headers) for a local object or (tempfilename, headers) for a remote object.
(self, url, filename=None, reporthook=None, data=None)
| 1804 | |
| 1805 | # External interface |
| 1806 | def retrieve(self, url, filename=None, reporthook=None, data=None): |
| 1807 | """retrieve(url) returns (filename, headers) for a local object |
| 1808 | or (tempfilename, headers) for a remote object.""" |
| 1809 | url = unwrap(_to_bytes(url)) |
| 1810 | if self.tempcache and url in self.tempcache: |
| 1811 | return self.tempcache[url] |
| 1812 | type, url1 = _splittype(url) |
| 1813 | if filename is None and (not type or type == 'file'): |
| 1814 | try: |
| 1815 | fp = self.open_local_file(url1) |
| 1816 | hdrs = fp.info() |
| 1817 | fp.close() |
| 1818 | return url2pathname(_splithost(url1)[1]), hdrs |
| 1819 | except OSError: |
| 1820 | pass |
| 1821 | fp = self.open(url, data) |
| 1822 | try: |
| 1823 | headers = fp.info() |
| 1824 | if filename: |
| 1825 | tfp = open(filename, 'wb') |
| 1826 | else: |
| 1827 | garbage, path = _splittype(url) |
| 1828 | garbage, path = _splithost(path or "") |
| 1829 | path, garbage = _splitquery(path or "") |
| 1830 | path, garbage = _splitattr(path or "") |
| 1831 | suffix = os.path.splitext(path)[1] |
| 1832 | (fd, filename) = tempfile.mkstemp(suffix) |
| 1833 | self.__tempfiles.append(filename) |
| 1834 | tfp = os.fdopen(fd, 'wb') |
| 1835 | try: |
| 1836 | result = filename, headers |
| 1837 | if self.tempcache is not None: |
| 1838 | self.tempcache[url] = result |
| 1839 | bs = 1024*8 |
| 1840 | size = -1 |
| 1841 | read = 0 |
| 1842 | blocknum = 0 |
| 1843 | if "content-length" in headers: |
| 1844 | size = int(headers["Content-Length"]) |
| 1845 | if reporthook: |
| 1846 | reporthook(blocknum, bs, size) |
| 1847 | while 1: |
| 1848 | block = fp.read(bs) |
| 1849 | if not block: |
| 1850 | break |
| 1851 | read += len(block) |
| 1852 | tfp.write(block) |
| 1853 | blocknum += 1 |
| 1854 | if reporthook: |
| 1855 | reporthook(blocknum, bs, size) |
| 1856 | finally: |
| 1857 | tfp.close() |
| 1858 | finally: |
| 1859 | fp.close() |
| 1860 | |
| 1861 | # raise exception if actual size does not match content-length header |
| 1862 | if size >= 0 and read < size: |
| 1863 | raise ContentTooShortError( |
nothing calls this directly
no test coverage detected