From cee9f8bf3232a7f8a81df67216e13145dd3a7c0b Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 3 Jan 2023 12:44:12 -0800 Subject: [PATCH] remove duplicate code. add tests --- jc/parsers/ini.py | 27 +++++++-------------------- jc/parsers/kv.py | 28 +++++++--------------------- tests/test_ini.py | 22 ++++++++++++++++++++++ tests/test_kv.py | 22 ++++++++++++++++++++++ 4 files changed, 58 insertions(+), 41 deletions(-) diff --git a/jc/parsers/ini.py b/jc/parsers/ini.py index 34a89205..22b51496 100644 --- a/jc/parsers/ini.py +++ b/jc/parsers/ini.py @@ -105,28 +105,15 @@ def _process(proc_data): """ # remove quotation marks from beginning and end of values for heading in proc_data: - # standard ini files with headers - if isinstance(proc_data[heading], dict): - for key, value in proc_data[heading].items(): - if value is not None and value.startswith('"') and value.endswith('"'): - proc_data[heading][key] = value.lstrip('"').rstrip('"') + for key, value in proc_data[heading].items(): + if value is not None and value.startswith('"') and value.endswith('"'): + proc_data[heading][key] = value.lstrip('"').rstrip('"') - elif value is not None and value.startswith("'") and value.endswith("'"): - proc_data[heading][key] = value.lstrip("'").rstrip("'") + elif value is not None and value.startswith("'") and value.endswith("'"): + proc_data[heading][key] = value.lstrip("'").rstrip("'") - elif value is None: - proc_data[heading][key] = '' - - # simple key/value files with no headers - else: - if proc_data[heading] is not None and proc_data[heading].startswith('"') and proc_data[heading].endswith('"'): - proc_data[heading] = proc_data[heading].lstrip('"').rstrip('"') - - elif proc_data[heading] is not None and proc_data[heading].startswith("'") and proc_data[heading].endswith("'"): - proc_data[heading] = proc_data[heading].lstrip("'").rstrip("'") - - elif proc_data[heading] is None: - proc_data[heading] = '' + elif value is None: + proc_data[heading][key] = '' return proc_data diff --git a/jc/parsers/kv.py b/jc/parsers/kv.py index 3f55152e..58263299 100644 --- a/jc/parsers/kv.py +++ b/jc/parsers/kv.py @@ -81,29 +81,15 @@ def _process(proc_data): Dictionary representing an ini or simple key/value pair document. """ # remove quotation marks from beginning and end of values - for heading in proc_data: - # standard ini files with headers - if isinstance(proc_data[heading], dict): - for key, value in proc_data[heading].items(): - if value is not None and value.startswith('"') and value.endswith('"'): - proc_data[heading][key] = value.lstrip('"').rstrip('"') + for key in proc_data: + if proc_data[key] is not None and proc_data[key].startswith('"') and proc_data[key].endswith('"'): + proc_data[key] = proc_data[key].lstrip('"').rstrip('"') - elif value is not None and value.startswith("'") and value.endswith("'"): - proc_data[heading][key] = value.lstrip("'").rstrip("'") + elif proc_data[key] is not None and proc_data[key].startswith("'") and proc_data[key].endswith("'"): + proc_data[key] = proc_data[key].lstrip("'").rstrip("'") - elif value is None: - proc_data[heading][key] = '' - - # simple key/value files with no headers - else: - if proc_data[heading] is not None and proc_data[heading].startswith('"') and proc_data[heading].endswith('"'): - proc_data[heading] = proc_data[heading].lstrip('"').rstrip('"') - - elif proc_data[heading] is not None and proc_data[heading].startswith("'") and proc_data[heading].endswith("'"): - proc_data[heading] = proc_data[heading].lstrip("'").rstrip("'") - - elif proc_data[heading] is None: - proc_data[heading] = '' + elif proc_data[key] is None: + proc_data[key] = '' return proc_data diff --git a/tests/test_ini.py b/tests/test_ini.py index 9ccb0c80..45a28be5 100644 --- a/tests/test_ini.py +++ b/tests/test_ini.py @@ -66,6 +66,28 @@ duplicate_key = value2 expected = {'section': {'duplicate_key': 'value2', 'another_key': 'foo'}} self.assertEqual(jc.parsers.ini.parse(data, quiet=True), expected) + def test_ini_missing_top_section(self): + """ + Test INI file missing top-level section header. Should output a fake + section header called `_top_level_section_` + """ + data = ''' +key: value1 +another_key = foo +[section2] +key3: bar +''' + expected = { + '_top_level_section_': { + 'key': 'value1', + 'another_key': 'foo' + }, + 'section2': { + 'key3': 'bar' + } + } + self.assertEqual(jc.parsers.ini.parse(data, quiet=True), expected) + def test_ini_doublequote(self): """ Test ini file with double quotes around a value diff --git a/tests/test_kv.py b/tests/test_kv.py index ebf3a4d6..1ee613b7 100644 --- a/tests/test_kv.py +++ b/tests/test_kv.py @@ -53,6 +53,28 @@ duplicate_key = value2 expected = {'duplicate_key': 'value2', 'another_key': 'foo'} self.assertEqual(jc.parsers.kv.parse(data, quiet=True), expected) + def test_kv_doublequote(self): + """ + Test kv string with double quotes around a value + """ + data = ''' +key1: "value1" +key2: value2 + ''' + expected = {'key1': 'value1', 'key2': 'value2'} + self.assertEqual(jc.parsers.kv.parse(data, quiet=True), expected) + + def test_kv_singlequote(self): + """ + Test kv string with double quotes around a value + """ + data = ''' +key1: 'value1' +key2: value2 + ''' + expected = {'key1': 'value1', 'key2': 'value2'} + self.assertEqual(jc.parsers.kv.parse(data, quiet=True), expected) + if __name__ == '__main__': unittest.main()