From 44ce3a594ccbdd94a2f71181eb15462862a2de57 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Fri, 16 Nov 2018 23:27:54 -0600 Subject: [PATCH] Parse settings which are dicts --- src/pgwui_server/__init__.py | 12 ++++++++---- tests/test___init__.py | 16 ++++++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/pgwui_server/__init__.py b/src/pgwui_server/__init__.py index 52c03a3..121e6f0 100644 --- a/src/pgwui_server/__init__.py +++ b/src/pgwui_server/__init__.py @@ -29,10 +29,14 @@ def parse_assignments(lines): '''Return a list of key/value tuples from the lines of a setting ''' result = [] - for line in lines.split('\n'): - if '=' in line: - key, val = line.split('=', 1) - result.append((key.rstrip(), val.lstrip())) + if isinstance(lines, str): + for line in lines.splitlines(): + if '=' in line: + key, val = line.split('=', 1) + result.append((key.rstrip(), val.lstrip())) + else: + for key, val in lines.items(): + result.append((key, val)) return result diff --git a/tests/test___init__.py b/tests/test___init__.py index d405761..2cbfbc2 100644 --- a/tests/test___init__.py +++ b/tests/test___init__.py @@ -52,8 +52,8 @@ class MockConfig(): # parse_assignments() -def test_parse_assignments(): - '''Returns key/value tuples and ignores lines without an "="''' +def test_parse_assignments_str(): + '''Returns key/value string tuples and ignores lines without an "="''' lines = ('key1 = value1\n' # whitespace around = is ignored '\n' 'ignored\n' @@ -66,6 +66,18 @@ def test_parse_assignments(): ('key3', 'value3=withequals')] +def test_parse_assignments_dict(): + '''Returns key value tuples. + ''' + lines = {'key1': 'value1', + 'key2': 'value2', + } + result = pgwui_server_init.parse_assignments(lines) + assert result == [('key1', 'value1'), + ('key2', 'value2'), + ] + + # add_routes() def test_add_routes_empty(): -- 2.34.1