def get(self, filename: Union[str, None]) -> Dict[str, Union[str, bytes, None]]: """Implementation of the core downloading function. Each call to this method operates on a single file, downloading it to the designated location. Arguments: filename {str} -- repository path from which the file should be downloaded Keyword Arguments: to {str} -- local download path. If not specified, the destination path used to instatiate the Ftp object is used. If it is also None, the download will fail. (default: {None}) Returns: tuple -- (filepath, filename, download_status) """ status = "error" try: result = {"status": status, "filename": filename, "content": b""} logger.info(f"Starting download from {self.url}...") byteio = io.BytesIO() ts = timer() self.retrbinary("RETR " + filename, byteio.write) # type: ignore te = timer() exc_time = round(te - ts, 0) content = byteio.getvalue() size = len(content) logger.info( "Download successful (download size: %s, download_time: %ss)", hf_size(size or 0), exc_time, extra={ "download_bytes": size, "download_seconds": exc_time }, ) result["status"] = "success" result["content"] = content except error_perm as e: logger.warning(f"{e} -- {filename}") except TypeError as te: logger.error(f"Failed downloading file -- {te}") return result
def get(self) -> requests.Response: logger.info(f"Starting download from {self.url}...") ts = timer() r = requests.get(self.url) te = timer() exc_time = round(te - ts, 0) size = self.get_file_size(r) logger.info( "Download successful (download size: %s, download_time: %ss)", hf_size(size or 0), exc_time, extra={ "download_bytes": size, "download_seconds": exc_time }, ) return r
def test_hf_size_string_arg(self): assert hf_size("123") == "123.0 B"
def test_hf_size_zero_bytes(self): assert hf_size(0) == "0B"
def test_hf_format_gb(self): assert hf_size(1200000000) == "1.12 GB"
def test_hf_format_mb(self): assert hf_size(1200000) == "1.14 MB"
def test_hf_format_kb(self): assert hf_size(123456) == "120.56 KB"