From 7f8299a9a17686456ae066ee46f91f64793e5866 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Mon, 5 Sep 2016 23:57:41 +0300 Subject: [PATCH] Join demo scripts; add argparse parameters parsing Join group-{file,sql}.py into demo-group.py parse-{file,sql}.py into demo-parse.py. --- demo/{group-sql.py => demo-group.py} | 46 ++++++++++++++++------ demo/demo-parse.py | 59 ++++++++++++++++++++++++++++ demo/group-file.py | 33 ---------------- demo/parse-file.py | 25 ------------ demo/parse-sql.py | 39 ------------------ 5 files changed, 92 insertions(+), 110 deletions(-) rename demo/{group-sql.py => demo-group.py} (51%) create mode 100755 demo/demo-parse.py delete mode 100755 demo/group-file.py delete mode 100755 demo/parse-file.py delete mode 100755 demo/parse-sql.py diff --git a/demo/group-sql.py b/demo/demo-group.py similarity index 51% rename from demo/group-sql.py rename to demo/demo-group.py index 11f1f8c..267c2ce 100755 --- a/demo/group-sql.py +++ b/demo/demo-group.py @@ -1,15 +1,16 @@ #! /usr/bin/env python from __future__ import print_function +import argparse import sys from sqlconvert.print_tokens import print_tokens from sqlconvert.process_tokens import find_error, StatementGrouper -def main(*queries): +def group_lines(*lines): grouper = StatementGrouper(encoding='utf-8') - for query in queries: - grouper.process_line(query) + for line in lines: + grouper.process_line(line) if grouper.statements: for statement in grouper.get_statements(): print("----------") @@ -26,8 +27,8 @@ def main(*queries): print(repr(token)) -def test(): - main( +def group_test(_args): + group_lines( "SELECT * FROM `mytable`; -- line-comment", "INSERT into /* inline comment */ mytable VALUES (1, 'one');", "/*! directive*/ INSERT INTO `MyTable` (`Id`, `Name`) " @@ -35,12 +36,31 @@ def test(): ) +def group_sql(args): + group_lines(*args.lines) + + +def group_file(args): + infile = open(args.filename, 'rt') + lines = infile.readlines() + infile.close() + group_lines(*lines) + + if __name__ == '__main__': - if len(sys.argv) <= 1: - sys.exit("Usage: %s [-t | sql_query_string [; sql_query_string ...]]" % - sys.argv[0]) - if sys.argv[1] == '-t': - test() - else: - queries = sys.argv[1:] - main(*queries) + main_parser = argparse.ArgumentParser(description='Group') + subparsers = main_parser.add_subparsers(help='Commands') + + parser = subparsers.add_parser('sql', help='SQL from command line') + parser.add_argument('lines', nargs='+', help='SQL lines') + parser.set_defaults(func=group_sql) + + parser = subparsers.add_parser('test', help='SQL from test data') + parser.set_defaults(func=group_test) + + parser = subparsers.add_parser('file', help='SQL from a file') + parser.add_argument('filename', help='SQL file') + parser.set_defaults(func=group_file) + + args = main_parser.parse_args() + args.func(args) diff --git a/demo/demo-parse.py b/demo/demo-parse.py new file mode 100755 index 0000000..7307934 --- /dev/null +++ b/demo/demo-parse.py @@ -0,0 +1,59 @@ +#! /usr/bin/env python +from __future__ import print_function + +import argparse +import sys +from sqlparse import parse +from sqlconvert.print_tokens import print_tokens +from sqlconvert.process_tokens import find_error + + +def parse_queries(*queries): + for query in queries: + for parsed in parse(query, encoding='utf-8'): + print("----------") + if find_error(parsed): + print("ERRORS IN QUERY") + print_tokens(parsed, encoding='utf-8') + print() + parsed._pprint_tree() + print("----------") + + +def parse_test(_args): + parse_queries( + "SELECT * FROM `mytable`; -- line-comment", + "INSERT into /* inline comment */ mytable VALUES (1, 'one')", + "/*! directive*/ INSERT INTO `MyTable` (`Id`, `Name`) " + "VALUES (1, 'one')" + ) + + +def parse_sql(args): + parse_queries(*args.lines) + + +def parse_file(args): + infile = open(args.filename, 'rt') + lines = infile.readlines() + infile.close() + parse_queries(*lines) + + +if __name__ == '__main__': + main_parser = argparse.ArgumentParser(description='Parse') + subparsers = main_parser.add_subparsers(help='Commands') + + parser = subparsers.add_parser('sql', help='SQL from command line') + parser.add_argument('lines', nargs='+', help='SQL lines') + parser.set_defaults(func=parse_sql) + + parser = subparsers.add_parser('test', help='SQL from test data') + parser.set_defaults(func=parse_test) + + parser = subparsers.add_parser('file', help='SQL from a file') + parser.add_argument('filename', help='SQL file') + parser.set_defaults(func=parse_file) + + args = main_parser.parse_args() + args.func(args) diff --git a/demo/group-file.py b/demo/group-file.py deleted file mode 100755 index 87df331..0000000 --- a/demo/group-file.py +++ /dev/null @@ -1,33 +0,0 @@ -#! /usr/bin/env python -from __future__ import print_function - -import sys -from sqlconvert.print_tokens import print_tokens -from sqlconvert.process_tokens import find_error, StatementGrouper - - -def main(filename): - grouper = StatementGrouper(encoding='utf-8') - with open(filename) as infile: - for line in infile: - grouper.process_line(line) - if grouper.statements: - for statement in grouper.get_statements(): - print("----------") - if find_error(statement): - print("ERRORS IN QUERY") - print_tokens(statement, encoding='utf-8') - print() - statement._pprint_tree() - print("----------") - tokens = grouper.close() - if tokens: - for token in tokens: - print_tokens(token, encoding='utf-8') - print(repr(token)) - - -if __name__ == '__main__': - if len(sys.argv) <= 1: - sys.exit("Usage: %s file" % sys.argv[0]) - main(sys.argv[1]) diff --git a/demo/parse-file.py b/demo/parse-file.py deleted file mode 100755 index 55a7829..0000000 --- a/demo/parse-file.py +++ /dev/null @@ -1,25 +0,0 @@ -#! /usr/bin/env python -from __future__ import print_function - -import sys -from sqlparse import parse -from sqlconvert.print_tokens import print_tokens -from sqlconvert.process_tokens import find_error - - -def main(filename): - with open(filename) as infile: - for query in infile: - for parsed in parse(query, encoding='utf-8'): - print("----------") - if find_error(parsed): - print("ERRORS IN QUERY") - print_tokens(parsed, encoding='utf-8') - print() - parsed._pprint_tree() - print("----------") - -if __name__ == '__main__': - if len(sys.argv) <= 1: - sys.exit("Usage: %s file" % sys.argv[0]) - main(sys.argv[1]) diff --git a/demo/parse-sql.py b/demo/parse-sql.py deleted file mode 100755 index abf38ba..0000000 --- a/demo/parse-sql.py +++ /dev/null @@ -1,39 +0,0 @@ -#! /usr/bin/env python -from __future__ import print_function - -import sys -from sqlparse import parse -from sqlconvert.print_tokens import print_tokens -from sqlconvert.process_tokens import find_error - - -def main(*queries): - for query in queries: - for parsed in parse(query, encoding='utf-8'): - print("----------") - if find_error(parsed): - print("ERRORS IN QUERY") - print_tokens(parsed, encoding='utf-8') - print() - parsed._pprint_tree() - print("----------") - - -def test(): - main( - "SELECT * FROM `mytable`; -- line-comment", - "INSERT into /* inline comment */ mytable VALUES (1, 'one')", - "/*! directive*/ INSERT INTO `MyTable` (`Id`, `Name`) " - "VALUES (1, 'one')" - ) - - -if __name__ == '__main__': - if len(sys.argv) <= 1: - sys.exit("Usage: %s [-t | sql_query_string [; sql_query_string ...]]" % - sys.argv[0]) - if sys.argv[1] == '-t': - test() - else: - queries = sys.argv[1:] - main(*queries) -- 2.39.5