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

allow duplicate keys

This commit is contained in:
Kelly Brazil
2022-04-04 11:38:52 -07:00
parent d5e9074f1c
commit a2ef9c429e
4 changed files with 38 additions and 8 deletions

View File

@ -1,8 +1,10 @@
"""jc - JSON Convert `INI` file parser """jc - JSON Convert `INI` file parser
Parses standard `INI` files and files containing simple key/value pairs. Parses standard `INI` files and files containing simple key/value pairs.
Delimiter can be `=` or `:`. Missing values are supported. Comment prefix
can be `#` or `;`. Comments must be on their own line. - Delimiter can be `=` or `:`. Missing values are supported.
- Comment prefix can be `#` or `;`. Comments must be on their own line.
- If duplicate keys are found, only the last value will be used.
Note: Values starting and ending with quotation marks will have the marks Note: Values starting and ending with quotation marks will have the marks
removed. If you would like to keep the quotation marks, use the `-r` removed. If you would like to keep the quotation marks, use the `-r`
@ -67,7 +69,7 @@ import configparser
class info(): class info():
"""Provides parser metadata (version, author, etc.)""" """Provides parser metadata (version, author, etc.)"""
version = '1.5' version = '1.6'
description = 'INI file parser' description = 'INI file parser'
author = 'Kelly Brazil' author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com' author_email = 'kellyjonbrazil@gmail.com'
@ -135,7 +137,9 @@ def parse(data, raw=False, quiet=False):
if jc.utils.has_data(data): if jc.utils.has_data(data):
ini = configparser.ConfigParser(allow_no_value=True, interpolation=None) ini = configparser.ConfigParser(allow_no_value=True,
interpolation=None,
strict=False)
try: try:
ini.read_string(data) ini.read_string(data)
raw_output = {s: dict(ini.items(s)) for s in ini.sections()} raw_output = {s: dict(ini.items(s)) for s in ini.sections()}

View File

@ -1,8 +1,10 @@
"""jc - JSON Convert `Key/Value` file parser """jc - JSON Convert `Key/Value` file parser
Supports files containing simple key/value pairs. Delimiter can be `=` or Supports files containing simple key/value pairs.
`:`. Missing values are supported. Comment prefix can be `#` or `;`.
Comments must be on their own line. - Delimiter can be `=` or `:`. Missing values are supported.
- Comment prefix can be `#` or `;`. Comments must be on their own line.
- If duplicate keys are found, only the last value will be used.
Note: Values starting and ending with quotation marks will have the marks Note: Values starting and ending with quotation marks will have the marks
removed. If you would like to keep the quotation marks, use the `-r` removed. If you would like to keep the quotation marks, use the `-r`
@ -52,7 +54,7 @@ Examples:
class info(): class info():
"""Provides parser metadata (version, author, etc.)""" """Provides parser metadata (version, author, etc.)"""
version = '1.1' version = '1.2'
description = 'Key/Value file parser' description = 'Key/Value file parser'
author = 'Kelly Brazil' author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com' author_email = 'kellyjonbrazil@gmail.com'

View File

@ -41,6 +41,18 @@ class MyTests(unittest.TestCase):
""" """
self.assertEqual(jc.parsers.ini.parse(self.generic_ini_iptelserver, quiet=True), self.generic_ini_iptelserver_json) self.assertEqual(jc.parsers.ini.parse(self.generic_ini_iptelserver, quiet=True), self.generic_ini_iptelserver_json)
def test_ini_duplicate_keys(self):
"""
Test input that contains duplicate keys. Only the last value should be used.
"""
data = '''
duplicate_key: value1
another_key = foo
duplicate_key = value2
'''
expected = {'duplicate_key': 'value2', 'another_key': 'foo'}
self.assertEqual(jc.parsers.ini.parse(data, quiet=True), expected)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -41,6 +41,18 @@ class MyTests(unittest.TestCase):
""" """
self.assertEqual(jc.parsers.kv.parse(self.generic_ini_keyvalue_ifcfg, quiet=True), self.generic_ini_keyvalue_ifcfg_json) self.assertEqual(jc.parsers.kv.parse(self.generic_ini_keyvalue_ifcfg, quiet=True), self.generic_ini_keyvalue_ifcfg_json)
def test_kv_duplicate_keys(self):
"""
Test input that contains duplicate keys. Only the last value should be used.
"""
data = '''
duplicate_key: value1
another_key = foo
duplicate_key = value2
'''
expected = {'duplicate_key': 'value2', 'another_key': 'foo'}
self.assertEqual(jc.parsers.kv.parse(data, quiet=True), expected)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()