From 00ea10750115f2d6887c2df1740974a8478edc8c Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Fri, 20 Nov 2020 17:37:44 -0600 Subject: [PATCH] New ini syntax for list after = --- examples/etc/pgwui.ini | 9 +++++++- examples/misc/development.ini | 6 ++++++ src/pgwui_server/pgwui_server.py | 23 ++++++++++++++++---- tests/test_pgwui_server.py | 37 ++++++++++++++++++++++++++------ 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/examples/etc/pgwui.ini b/examples/etc/pgwui.ini index 5bf231c..969a4dc 100644 --- a/examples/etc/pgwui.ini +++ b/examples/etc/pgwui.ini @@ -89,10 +89,16 @@ pgwui.dry_run = False # PGWUI components in use. The display value of the menu items can # be overridden using a "menu_label" setting for each component. # CAUTION: Do not uncomment the below, instead change the component's -# "menu_label" setting. +# "menu_label" setting. E.g.: #pgwui.pgwui_upload = # menu_label = upload -- Upload File Into Database +# The order of the menu items can be manually specified based +# PGWUI component name. Omitted components come last. +#pgwui.pgwui_menu = +# order = pgwui_upload +# pgwui_logout + # pgwui_upload # Take uploaded column headings literally? @@ -108,6 +114,7 @@ pgwui.pgwui_upload = literal_column_headings = off # menu_label = upload -- Upload File Into Database + # # Pyramid configuration # https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html diff --git a/examples/misc/development.ini b/examples/misc/development.ini index 645de43..0d069bd 100644 --- a/examples/misc/development.ini +++ b/examples/misc/development.ini @@ -93,6 +93,12 @@ pgwui.validate_hmac = False #pgwui.pgwui_upload = # menu_label = upload -- Upload File Into Database +# The order of the menu items can be manually specified based +# PGWUI component name. Omitted components come last. +#pgwui.pgwui_menu = +# order = pgwui_upload +# pgwui_logout + # pgwui_upload # Take uploaded column headings literally? diff --git a/src/pgwui_server/pgwui_server.py b/src/pgwui_server/pgwui_server.py index eb3b7c3..776e3fd 100644 --- a/src/pgwui_server/pgwui_server.py +++ b/src/pgwui_server/pgwui_server.py @@ -77,15 +77,30 @@ def dot_to_dict(settings, key, new_key): del settings[key] +def parse_multiline_assignments(lines, result): + '''Add the parse value to the result + ''' + for line in lines.splitlines(): + if '=' in line: + key, val = line.split('=', 1) + result.append((key.rstrip(), val.lstrip())) + else: + stripped = line.lstrip() + if stripped != '': + # Multiple values on different lines means a list + key, val = result[-1] + if not isinstance(val, list): + val = [val] + val.append(stripped) + result[-1] = (key, val) + + def parse_assignments(lines): '''Return a list of key/value tuples from the lines of a setting ''' result = [] if isinstance(lines, str): - for line in lines.splitlines(): - if '=' in line: - key, val = line.split('=', 1) - result.append((key.rstrip(), val.lstrip())) + parse_multiline_assignments(lines, result) else: for key, val in lines.items(): result.append((key, val)) diff --git a/tests/test_pgwui_server.py b/tests/test_pgwui_server.py index 2ba90a1..640202a 100644 --- a/tests/test_pgwui_server.py +++ b/tests/test_pgwui_server.py @@ -173,23 +173,46 @@ mock_dot_to_dict = testing.make_mock_fixture( pgwui_server, 'dot_to_dict') +# parse_multiline_assigments() + + +def test_parse_multiline_assignments_str(): + '''Appends key/value string tuples and when there's no "=", + and more than just whitespace, a list is the result + ''' + lines = ('key1 = value1\n' # whitespace around = is ignored + '\n' + 'second\n' + 'third\n' + 'key2=value2\n' # missing whitespace is fine + 'key3= value3=withequals\n' + ) + result = [] + pgwui_server.parse_multiline_assignments(lines, result) + assert result == [('key1', ['value1', 'second', 'third']), + ('key2', 'value2'), + ('key3', 'value3=withequals')] + + +mock_parse_multiline_assignments = testing.make_mock_fixture( + pgwui_server, 'parse_multiline_assignments') + + # parse_assignments() -def test_parse_assignments_str(): - '''Returns key/value string tuples and ignores lines without an "="''' +def test_parse_assignments_str(mock_parse_multiline_assignments): + '''Calls parse_multiline_assignments''' lines = ('key1 = value1\n' # whitespace around = is ignored '\n' 'ignored\n' 'key2=value2\n' # missing whitespace is fine 'key3= value3=withequals\n' ) - result = pgwui_server.parse_assignments(lines) - assert set(result) == set([('key1', 'value1'), - ('key2', 'value2'), - ('key3', 'value3=withequals')]) + pgwui_server.parse_assignments(lines) + mock_parse_multiline_assignments.assert_called_once() -def test_parse_assignments_dict(): +def test_parse_assignments_dict(mock_parse_multiline_assignments): '''Returns key value tuples. ''' lines = {'key1': 'value1', -- 2.34.1