1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

remove duplicate code. add tests

This commit is contained in:
Kelly Brazil
2023-01-03 12:44:12 -08:00
parent b8e2678c01
commit cee9f8bf32
4 changed files with 58 additions and 41 deletions

View File

@ -105,28 +105,15 @@ def _process(proc_data):
""" """
# remove quotation marks from beginning and end of values # remove quotation marks from beginning and end of values
for heading in proc_data: for heading in proc_data:
# standard ini files with headers for key, value in proc_data[heading].items():
if isinstance(proc_data[heading], dict): if value is not None and value.startswith('"') and value.endswith('"'):
for key, value in proc_data[heading].items(): proc_data[heading][key] = value.lstrip('"').rstrip('"')
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("'"): elif value is not None and value.startswith("'") and value.endswith("'"):
proc_data[heading][key] = value.lstrip("'").rstrip("'") proc_data[heading][key] = value.lstrip("'").rstrip("'")
elif value is None: elif value is None:
proc_data[heading][key] = '' 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] = ''
return proc_data return proc_data

View File

@ -81,29 +81,15 @@ def _process(proc_data):
Dictionary representing an ini or simple key/value pair document. Dictionary representing an ini or simple key/value pair document.
""" """
# remove quotation marks from beginning and end of values # remove quotation marks from beginning and end of values
for heading in proc_data: for key in proc_data:
# standard ini files with headers if proc_data[key] is not None and proc_data[key].startswith('"') and proc_data[key].endswith('"'):
if isinstance(proc_data[heading], dict): proc_data[key] = proc_data[key].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("'"): elif proc_data[key] is not None and proc_data[key].startswith("'") and proc_data[key].endswith("'"):
proc_data[heading][key] = value.lstrip("'").rstrip("'") proc_data[key] = proc_data[key].lstrip("'").rstrip("'")
elif value is None: elif proc_data[key] is None:
proc_data[heading][key] = '' proc_data[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] = ''
return proc_data return proc_data

View File

@ -66,6 +66,28 @@ duplicate_key = value2
expected = {'section': {'duplicate_key': 'value2', 'another_key': 'foo'}} expected = {'section': {'duplicate_key': 'value2', 'another_key': 'foo'}}
self.assertEqual(jc.parsers.ini.parse(data, quiet=True), expected) 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): def test_ini_doublequote(self):
""" """
Test ini file with double quotes around a value Test ini file with double quotes around a value

View File

@ -53,6 +53,28 @@ duplicate_key = value2
expected = {'duplicate_key': 'value2', 'another_key': 'foo'} expected = {'duplicate_key': 'value2', 'another_key': 'foo'}
self.assertEqual(jc.parsers.kv.parse(data, quiet=True), expected) 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__': if __name__ == '__main__':
unittest.main() unittest.main()