def test_display_without_start(ansi_io): bar = ProgressBar(ansi_io, 50, 0) bar.display() expected = " 0/50 [>---------------------------] 0%" assert expected == ansi_io.fetch_error()
def test_overwrite_with_section_output(ansi_io): bar = ProgressBar(ansi_io.section(), 50, 0) bar.start() bar.display() bar.advance() bar.advance() output = [ " 0/50 [>---------------------------] 0%", " 1/50 [>---------------------------] 2%", " 2/50 [=>--------------------------] 4%", ] expected = "\n\x1b[1A\x1b[0J".join(output) + "\n" assert expected == ansi_io.fetch_output()
def test_percent_not_hundred_before_complete(ansi_io): bar = ProgressBar(ansi_io, 200, 0) bar.start() bar.display() bar.advance(199) bar.advance() output = [ " 0/200 [>---------------------------] 0%", " 199/200 [===========================>] 99%", " 200/200 [============================] 100%", ] expected = generate_output(output) assert expected == ansi_io.fetch_error()
def test_percent(ansi_io): bar = ProgressBar(ansi_io, 50, 0) bar.start() bar.display() bar.advance() bar.advance() output = [ " 0/50 [>---------------------------] 0%", " 1/50 [>---------------------------] 2%", " 2/50 [=>--------------------------] 4%", ] expected = generate_output(output) assert expected == ansi_io.fetch_error()
def test_set_current_progress(ansi_io): bar = ProgressBar(ansi_io, 50, 0) bar.start() bar.display() bar.advance() bar.set_progress(15) bar.set_progress(25) output = [ " 0/50 [>---------------------------] 0%", " 1/50 [>---------------------------] 2%", " 15/50 [========>-------------------] 30%", " 25/50 [==============>-------------] 50%", ] expected = generate_output(output) assert expected == ansi_io.fetch_error()
def test_overwrite_with_shorter_line(ansi_io): bar = ProgressBar(ansi_io, 50, 0) bar.set_format(" %current%/%max% [%bar%] %percent:3s%%") bar.start() bar.display() bar.advance() # Set shorter format bar.set_format(" %current%/%max% [%bar%]") bar.advance() output = [ " 0/50 [>---------------------------] 0%", " 1/50 [>---------------------------] 2%", " 2/50 [=>--------------------------]", ] expected = generate_output(output) assert expected == ansi_io.fetch_error()
def test_display_with_quiet_verbosity(ansi_io): ansi_io.set_verbosity(Verbosity.QUIET) bar = ProgressBar(ansi_io, 50, 0) bar.display() assert "" == ansi_io.fetch_error()
def _upload_file( self, session: requests.Session, url: str, file: Path, dry_run: bool = False, skip_existing: bool = False, ) -> None: from cleo.ui.progress_bar import ProgressBar data = self.post_data(file) data.update({ # action ":action": "file_upload", "protocol_version": "1", }) data_to_send: list[tuple[str, Any]] = self._prepare_data(data) with file.open("rb") as fp: data_to_send.append( ("content", (file.name, fp, "application/octet-stream"))) encoder = MultipartEncoder(data_to_send) bar = ProgressBar(self._io, max=encoder.len) bar.set_format( f" - Uploading <c1>{file.name}</c1> <b>%percent%%</b>") monitor = MultipartEncoderMonitor( encoder, lambda monitor: bar.set_progress(monitor.bytes_read)) bar.start() resp = None try: if not dry_run: resp = session.post( url, data=monitor, allow_redirects=False, headers={"Content-Type": monitor.content_type}, ) if resp is None or 200 <= resp.status_code < 300: bar.set_format( f" - Uploading <c1>{file.name}</c1> <fg=green>%percent%%</>" ) bar.finish() elif resp.status_code == 301: if self._io.output.is_decorated(): self._io.overwrite( f" - Uploading <c1>{file.name}</c1> <error>FAILED</>" ) raise UploadError("Redirects are not supported. " "Is the URL missing a trailing slash?") elif resp.status_code == 400 and "was ever registered" in resp.text: self._register(session, url) resp.raise_for_status() elif skip_existing and self._is_file_exists_error(resp): bar.set_format( f" - Uploading <c1>{file.name}</c1> <warning>File exists." " Skipping</>") bar.display() else: resp.raise_for_status() except (requests.ConnectionError, requests.HTTPError) as e: if self._io.output.is_decorated(): self._io.overwrite( f" - Uploading <c1>{file.name}</c1> <error>FAILED</>") raise UploadError(e) finally: self._io.write_line("")