diff options
-rwxr-xr-x | pgcli/main.py | 28 | ||||
-rw-r--r-- | tests/test_main.py | 15 | ||||
-rw-r--r-- | tests/utils.py | 8 |
3 files changed, 35 insertions, 16 deletions
diff --git a/pgcli/main.py b/pgcli/main.py index c90a6782..a4a0d3b9 100755 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -75,6 +75,13 @@ MetaQuery = namedtuple( ]) MetaQuery.__new__.__defaults__ = ('', False, 0, False, False, False, False) +OutputSettings = namedtuple( + 'OutputSettings', + 'table_format dcmlfmt floatfmt missingval expanded max_width' +) +OutputSettings.__new__.__defaults__ = ( + None, None, None, '<null>', False, None +) # no-op logging handler class NullHandler(logging.Handler): @@ -597,9 +604,15 @@ class PGCli(object): max_width = None expanded = self.pgspecial.expanded_output or self.expanded_output - formatted = format_output( - title, cur, headers, status, self.table_format, self.decimal_format, - self.float_format, self.null_string, expanded, max_width) + settings = OutputSettings( + table_format=self.table_format, + dcmlfmt=self.decimal_format, + floatfmt=self.float_format, + missingval=self.null_string, + expanded=expanded, + max_width=max_width + ) + formatted = format_output(title, cur, headers, status, settings) output.extend(formatted) total = time() - start @@ -810,9 +823,14 @@ def obfuscate_process_password(): setproctitle.setproctitle(process_title) -def format_output(title, cur, headers, status, table_format, dcmlfmt, floatfmt, - missingval='<null>', expanded=False, max_width=None): +def format_output(title, cur, headers, status, settings): output = [] + missingval = settings.missingval + table_format = settings.table_format + dcmlfmt = settings.dcmlfmt + floatfmt = settings.floatfmt + expanded = settings.expanded + max_width = settings.max_width if title: # Only print the title if it's not None. output.append(title) if cur: diff --git a/tests/test_main.py b/tests/test_main.py index e461fc0d..b705b730 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -8,7 +8,9 @@ try: except ImportError: setproctitle = None -from pgcli.main import obfuscate_process_password, format_output, PGCli +from pgcli.main import ( + obfuscate_process_password, format_output, PGCli, OutputSettings +) from utils import dbtest, run @@ -47,21 +49,20 @@ def test_obfuscate_process_password(): def test_format_output(): + settings = OutputSettings(table_format='psql', dcmlfmt='d', floatfmt='g') results = format_output('Title', [('abc', 'def')], ['head1', 'head2'], - 'test status', 'psql', dcmlfmt='d', floatfmt='g',) + 'test status', settings) expected = ['Title', '+---------+---------+\n| head1 | head2 |\n|---------+---------|\n| abc | def |\n+---------+---------+', 'test status'] assert results == expected def test_format_output_auto_expand(): + settings = OutputSettings(table_format='psql', dcmlfmt='d', floatfmt='g', max_width=100) table_results = format_output('Title', [('abc', 'def')], - ['head1', 'head2'], 'test status', 'psql', dcmlfmt='d', floatfmt='g', - max_width=100) + ['head1', 'head2'], 'test status', settings) table = ['Title', '+---------+---------+\n| head1 | head2 |\n|---------+---------|\n| abc | def |\n+---------+---------+', 'test status'] assert table_results == table - expanded_results = format_output('Title', [('abc', 'def')], - ['head1', 'head2'], 'test status', 'psql', dcmlfmt='d', floatfmt='g', - max_width=1) + ['head1', 'head2'], 'test status', settings._replace(max_width=1)) expanded = ['Title', u'-[ RECORD 0 ]-------------------------\nhead1 | abc\nhead2 | def\n', 'test status'] assert expanded_results == expanded diff --git a/tests/utils.py b/tests/utils.py index 2f17020b..3b73ce31 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,7 +1,7 @@ import pytest import psycopg2 import psycopg2.extras -from pgcli.main import format_output +from pgcli.main import format_output, OutputSettings from pgcli.pgexecute import register_json_typecasters # TODO: should this be somehow be divined from environment? @@ -64,10 +64,10 @@ def run(executor, sql, join=False, expanded=False, pgspecial=None, results = executor.run(sql, pgspecial, exception_formatter) formatted = [] - + settings = OutputSettings(table_format='psql', dcmlfmt='d', floatfmt='g', + expanded=expanded) for title, rows, headers, status, sql, success in results: - formatted.extend(format_output(title, rows, headers, status, 'psql', dcmlfmt='d', floatfmt='g', - expanded=expanded)) + formatted.extend(format_output(title, rows, headers, status, settings)) if join: formatted = '\n'.join(formatted) |