From a1c3e0150642e33325f18a9220cd6aa8574a080c Mon Sep 17 00:00:00 2001 From: Alexey Vasilyev Date: Sun, 18 Aug 2019 11:09:56 +0200 Subject: [PATCH 1/5] optparse TYPE_CHECKER signature is 'def check_mytype(option, opt, value)' --- rethinkdb/utils_common.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rethinkdb/utils_common.py b/rethinkdb/utils_common.py index 823e2fa9..f53db25e 100644 --- a/rethinkdb/utils_common.py +++ b/rethinkdb/utils_common.py @@ -153,7 +153,7 @@ def format_epilog(self, formatter): def __init__(self, *args, **kwargs): # -- Type Checkers - def check_tls_option(opt_str, value): + def check_tls_option(_, opt_str, value): value = str(value) if os.path.isfile(value): @@ -161,7 +161,7 @@ def check_tls_option(opt_str, value): else: raise optparse.OptionValueError('Option %s value is not a file: %r' % (opt_str, value)) - def check_db_table_option(value): + def check_db_table_option(_, opt_str, value): res = _tableNameRegex.match(value) if not res: @@ -171,7 +171,7 @@ def check_db_table_option(value): return DbTable(res.group('db'), res.group('table')) - def check_positive_int(opt_str, value): + def check_positive_int(_, opt_str, value): if not isinstance(value, int) or value < 1: raise optparse.OptionValueError('%s value must be an integer greater that 1: %s' % (opt_str, value)) @@ -183,7 +183,7 @@ def check_existing_file(_, opt_str, value): return os.path.realpath(value) - def check_new_file_location(opt_str, value): + def check_new_file_location(_, opt_str, value): try: real_value = os.path.realpath(value) except Exception: @@ -194,7 +194,7 @@ def check_new_file_location(opt_str, value): return real_value - def file_contents(opt_str, value): + def file_contents(_, opt_str, value): if not os.path.isfile(value): raise optparse.OptionValueError('%s value is not an existing file: %r' % (opt_str, value)) From ddae915ecf0e601c02f82c56ac6f6dccc0d0a43c Mon Sep 17 00:00:00 2001 From: Alexey Vasilyev Date: Sun, 18 Aug 2019 15:36:02 +0200 Subject: [PATCH 2/5] fix parsing --clients=N option --- rethinkdb/utils_common.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/rethinkdb/utils_common.py b/rethinkdb/utils_common.py index f53db25e..e755084a 100644 --- a/rethinkdb/utils_common.py +++ b/rethinkdb/utils_common.py @@ -172,10 +172,14 @@ def check_db_table_option(_, opt_str, value): return DbTable(res.group('db'), res.group('table')) def check_positive_int(_, opt_str, value): - if not isinstance(value, int) or value < 1: - raise optparse.OptionValueError('%s value must be an integer greater that 1: %s' % (opt_str, value)) + try: + value = int(value) + if value < 1: + raise ValueError + except ValueError: + raise optparse.OptionValueError('%s value must be an integer greater than 1: %s' % (opt_str, value)) - return int(value) + return value def check_existing_file(_, opt_str, value): if not os.path.isfile(value): From 326476e0fbfcf45d3a3c941bb9cd5414437dfc91 Mon Sep 17 00:00:00 2001 From: Alexey Vasilyev Date: Sun, 18 Aug 2019 15:57:29 +0200 Subject: [PATCH 3/5] fix -e option in dump command --- rethinkdb/_dump.py | 1 + 1 file changed, 1 insertion(+) diff --git a/rethinkdb/_dump.py b/rethinkdb/_dump.py index a90b2c08..eba85ea9 100755 --- a/rethinkdb/_dump.py +++ b/rethinkdb/_dump.py @@ -66,6 +66,7 @@ def parse_options(argv, prog=None): dest="db_tables", metavar="DB|DB.TABLE", default=[], + type='db_table', help='limit dump to the given database or table (may be specified multiple times)', action="append") From d514c0b0315bd2d68b268e4de8a470f251e0aa30 Mon Sep 17 00:00:00 2001 From: Alexey Vasilyev Date: Sun, 18 Aug 2019 23:06:10 +0200 Subject: [PATCH 4/5] unit-tests for option parser --- tests/test_utils_common.py | 73 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 tests/test_utils_common.py diff --git a/tests/test_utils_common.py b/tests/test_utils_common.py new file mode 100644 index 00000000..f7fc3839 --- /dev/null +++ b/tests/test_utils_common.py @@ -0,0 +1,73 @@ +import pytest +from rethinkdb import utils_common + + +@pytest.fixture +def parser(): + opt_parser = utils_common.CommonOptionsParser() + opt_parser.add_option( + "-e", + "--export", + dest="db_tables", + metavar="DB|DB.TABLE", + default=[], + type='db_table', + action="append") + opt_parser.add_option( + "--clients", + dest="clients", + metavar="NUM", + default=3, + type="pos_int") + return opt_parser + + +def test_option_parser_int_pos(parser): + options, args = parser.parse_args(['--clients', '4'], connect=False) + + assert options.clients == 4 + + +def test_option_parser_int_pos_equals(parser): + options, args = parser.parse_args(['--clients=4'], connect=False) + + assert options.clients == 4 + + +def test_option_parser_int_pos_default(parser): + options, args = parser.parse_args([], connect=False) + + assert options.clients == 3 + + +def test_option_parser_int_pos_fail(parser): + with pytest.raises(SystemExit): + parser.parse_args(['--clients=asdf'], connect=False) + + +def test_option_parser_int_pos_zero(parser): + with pytest.raises(SystemExit): + parser.parse_args(['--clients=0'], connect=False) + + +def test_option_parser_db_table(parser): + options, args = parser.parse_args(['--export=example.table'], connect=False) + + assert options.db_tables == [('example', 'table')] + + +def test_option_parser_db_table_append(parser): + options, args = parser.parse_args(['--export=example.table', '--export=example.another'], connect=False) + + assert options.db_tables == [('example', 'table'), ('example', 'another')] + + +def test_option_parser_db_table_only_db(parser): + options, args = parser.parse_args(['--export=example'], connect=False) + + assert options.db_tables == [('example', None)] + + +def test_option_parser_db_table_fail(parser): + with pytest.raises(SystemExit): + parser.parse_args(['--export='], connect=False) From b15ca65def68a59e6d185371edbaa6f2ab8dac88 Mon Sep 17 00:00:00 2001 From: Alexey Vasilyev Date: Sun, 18 Aug 2019 23:37:16 +0200 Subject: [PATCH 5/5] fix unused arg for lint --- rethinkdb/utils_common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rethinkdb/utils_common.py b/rethinkdb/utils_common.py index e755084a..82274c20 100644 --- a/rethinkdb/utils_common.py +++ b/rethinkdb/utils_common.py @@ -161,7 +161,7 @@ def check_tls_option(_, opt_str, value): else: raise optparse.OptionValueError('Option %s value is not a file: %r' % (opt_str, value)) - def check_db_table_option(_, opt_str, value): + def check_db_table_option(_, _opt_str, value): res = _tableNameRegex.match(value) if not res: