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:
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
Reference in New Issue
Block a user