From: Oleg Broytman Date: Sat, 3 Sep 2016 20:40:29 +0000 (+0300) Subject: Display progress bar X-Git-Tag: 0.0.2~1 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=447c2ab10de0e28b7d6ecc95c3c2aaf0efddebd3;p=sqlconvert.git Display progress bar --- diff --git a/TODO b/TODO index a1c0d9f..e719c69 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,3 @@ -Progress bar. - - Remove /*! directives */ diff --git a/docs/index.rst b/docs/index.rst index 8b73c1a..242918a 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -29,15 +29,19 @@ Usage:: Options:: - -e ENCODING, --encoding ENCODING + -e ENCODING, --encoding ENCODING input/output encoding, default is utf-8 - -E OUTPUT_ENCODING, --output-encoding OUTPUT_ENCODING + -E OUTPUT_ENCODING, --output-encoding OUTPUT_ENCODING separate output encoding, default is the same as `-e` except for console; for console output charset from the current locale is used + -P, --no-pbar Inhibit progress bar infile Input file, stdin if absent or '-' -o, --outfile outfile Output file, stdout if absent or '-' +If stderr is connected to the console the program displays a text mode progress +bar. Option `-P/--no-pbar` inhibits it. + Option `-o` is useful when infile is absent (input is redirected), for example:: diff --git a/scripts/mysql2sql b/scripts/mysql2sql index 23197ff..3809c1b 100755 --- a/scripts/mysql2sql +++ b/scripts/mysql2sql @@ -3,17 +3,48 @@ from __future__ import print_function import argparse from io import open +import os import sys +from sqlparse.compat import text_type from mysql2sql.print_tokens import print_tokens from mysql2sql.process_tokens import requote_names, StatementGrouper from m_lib.defenc import default_encoding +from m_lib.pbar.tty_pbar import ttyProgressBar -def main(infile, encoding, outfile, output_encoding): +def get_fsize(fp): + try: + fp.seek(0, os.SEEK_END) + except IOError: + return None # File size is unknown + size = fp.tell() + fp.seek(0, os.SEEK_SET) + return size + + +def main(infile, encoding, outfile, output_encoding, use_pbar): + if use_pbar: + size = get_fsize(infile) + if size is None: + use_pbar = False + + print("Converting: ", end='', file=sys.stderr) + sys.stderr.flush() + + if use_pbar: + pbar = ttyProgressBar(0, size-1) + cur_pos = 0 + grouper = StatementGrouper(encoding=encoding) for line in infile: + if use_pbar: + if isinstance(line, text_type): + cur_pos += len(line.encode(encoding)) + else: + cur_pos += len(line) + pbar.display(cur_pos) grouper.process_line(line) if grouper.statements: for statement in grouper.get_statements(): @@ -25,6 +56,9 @@ def main(infile, encoding, outfile, output_encoding): for token in tokens: print_tokens(token, outfile=outfile, encoding=output_encoding) + if use_pbar: + pbar.erase() + print("done.") if __name__ == '__main__': parser = argparse.ArgumentParser(description='Convert MySQL to SQL') @@ -35,6 +69,8 @@ if __name__ == '__main__': 'as -e except for console; for console output ' 'charset from the current locale is used') parser.add_argument('-o', '--outfile', help='output file name') + parser.add_argument('-P', '--no-pbar', action='store_true', + help='inhibit progress bar') parser.add_argument('infile', help='input file name') parser.add_argument('output_file', nargs='?', help='output file name') args = parser.parse_args() @@ -83,4 +119,4 @@ if __name__ == '__main__': infile.close() raise - main(infile, args.encoding, outfile, output_encoding) + main(infile, args.encoding, outfile, output_encoding, not args.no_pbar)