X-Git-Url: https://git.phdru.name/?p=sqlconvert.git;a=blobdiff_plain;f=scripts%2Fmysql-to-sql.py;h=23197ffce6b2cbe7b312d7ab7b0f60d02ba7a7c2;hp=add0a8cad3e8a0a1d7e63568ee16a01d8cfce1ea;hb=4c93c3d89685aba33fc45082022373eb93b6583e;hpb=979d3300ae140d6a07c662c899b00a5de84f8b38 diff --git a/scripts/mysql-to-sql.py b/scripts/mysql-to-sql.py index add0a8c..23197ff 100755 --- a/scripts/mysql-to-sql.py +++ b/scripts/mysql-to-sql.py @@ -1,48 +1,86 @@ #! /usr/bin/env python +from __future__ import print_function import argparse +from io import open import sys from mysql2sql.print_tokens import print_tokens from mysql2sql.process_tokens import requote_names, StatementGrouper +from m_lib.defenc import default_encoding -def main(infile, outfile): - grouper = StatementGrouper() + +def main(infile, encoding, outfile, output_encoding): + grouper = StatementGrouper(encoding=encoding) for line in infile: grouper.process_line(line) if grouper.statements: for statement in grouper.get_statements(): requote_names(statement) - print_tokens(statement, outfile=outfile) + print_tokens(statement, outfile=outfile, + encoding=output_encoding) tokens = grouper.close() if tokens: for token in tokens: - print_tokens(token, outfile=outfile) + print_tokens(token, outfile=outfile, encoding=output_encoding) if __name__ == '__main__': parser = argparse.ArgumentParser(description='Convert MySQL to SQL') - parser.add_argument('-i', '--infile', help='input file name') + parser.add_argument('-e', '--encoding', default='utf-8', + help='input/output encoding, default is utf-8') + parser.add_argument('-E', '--output-encoding', + help='separate output encoding, default is the same ' + '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('infile', help='input file name') + parser.add_argument('output_file', nargs='?', help='output file name') args = parser.parse_args() if args.infile: - infile = open(args.infile, 'rt') + if args.infile == '-': + infile = sys.stdin + else: + infile = open(args.infile, 'rt', encoding=args.encoding) else: infile = sys.stdin - if infile.isatty(): - parser.print_help() - sys.exit() + + if infile.isatty(): + print("Error: cannot read from console", file=sys.stderr) + parser.print_help() + sys.exit(1) if args.outfile: + if args.output_file: + print("Error: too many output files", file=sys.stderr) + parser.print_help() + sys.exit(1) + + outfile = args.outfile + + elif args.output_file: + outfile = args.output_file + + else: + outfile = '-' + + if args.output_encoding: + output_encoding = args.output_encoding + elif outfile == '-': + output_encoding = default_encoding + else: + output_encoding = args.encoding + + if outfile == '-': + outfile = sys.stdout + else: try: - outfile = open(args.outfile, 'wt') + outfile = open(outfile, 'wt', encoding=output_encoding) except: if infile is not sys.stdin: infile.close() raise - else: - outfile = sys.stdout - main(infile, outfile) + main(infile, args.encoding, outfile, output_encoding)