def download_file(self, file_id, save_path, filename=None): """Download a file. Args: file_id: file id save_path: str to a directory filename: join filename to save_path if given, otherwise fetch file name from Google Drive Returns: if successful: str, download path else: None """ if filename is None: filename = self.get_id_name(file_id) if not filename: return self.create_local_folder(save_path) download_path = os.path.abspath(os.path.join(save_path, filename)) logging.info("GD DL: {} -> {}".format(file_id, download_path)) request = self.drive_service.files().get_media(fileId=file_id) with open(download_path, 'wb') as f: downloader = MediaIoBaseDownload( f, request, chunksize=self.DOWNLOAD_CHUNK_SIZE) pbar = progressbar.blockbar(desc="DL " + filename, bar_width=12) done = False while not done: try: status, done = downloader.next_chunk( num_retries=NUM_RETRIES) except HttpError as e: # "Request range not satisfiable" error # This is a bug in MediaIoBaseDownload. It happens when # trying to download a file with size 0 bytes. The error # is safe to ignore. if e.resp.status == 416: logging.warning(e) break raise e pbar.set_progress(status.progress() if status else 1) pbar.close() return download_path
def upload_file(self, file_path, folder_id='root', file_id=None, fields=None): """If file_id is specified, the file will be updated/patched.""" logging.info("GD UL: {}".format(file_path)) mime, encoding = mimetypes.guess_type(file_path) if mime is None: mime = 'application/octet-stream' body = { 'name': ft.real_case_filename(file_path), 'parents': [folder_id] } # Empty files can't be uploaded with chunks because they aren't resumable. resumable = ft.getsize(file_path) > 0 media_body = MediaFileUpload(file_path, mimetype=mime, chunksize=self.UPLOAD_CHUNK_SIZE, resumable=resumable) request = self._determine_update_or_insert(body, media_body=media_body, file_id=file_id, fields=fields) pbar = progressbar.blockbar(desc="UL " + body["name"], bar_width=12) response = None if resumable else request.execute( ) # Empty files are not chunked. while response is None: status, response = request.next_chunk(num_retries=5) pbar.set_progress(status.progress() if status else 1) pbar.close() return response
def test_block_multiline(): for i in progressbar.blockbar(range(20), desc="This blockbar spans multiple lines!\n\t"): fuzz()
def test_block_progress(): with progressbar.blockbar(desc="Progress test") as p: for step in range(500): p.set_progress(pow(step / 500, 3)) time.sleep(2 / 500)
def test_block_length(): for i in progressbar.blockbar(range(10), desc="A" * 999, max_width=100): fuzz()
def test_block_manual(): N = 20 with progressbar.blockbar(total=N, desc="Block manual test") as p: for i in range(N): fuzz() p.update()
def test_block_dummy(): with progressbar.blockbar(desc="Block dummy test", show_time=False, bar_width=8) as p: for i in range(20): fuzz() p.update()
def test_block_iterator(): iterable = [i for i in range(20)] for i in progressbar.blockbar(iterable, desc="Block iterable test"): fuzz()