diff --git a/jc/parsers/ini.py b/jc/parsers/ini.py index b476937e..77827fdb 100644 --- a/jc/parsers/ini.py +++ b/jc/parsers/ini.py @@ -1,8 +1,10 @@ """jc - JSON Convert `INI` file parser 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 removed. If you would like to keep the quotation marks, use the `-r` @@ -67,7 +69,7 @@ import configparser class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.5' + version = '1.6' description = 'INI file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -135,7 +137,9 @@ def parse(data, raw=False, quiet=False): 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: ini.read_string(data) raw_output = {s: dict(ini.items(s)) for s in ini.sections()} diff --git a/jc/parsers/kv.py b/jc/parsers/kv.py index 2d5feee5..a893687a 100644 --- a/jc/parsers/kv.py +++ b/jc/parsers/kv.py @@ -1,8 +1,10 @@ """jc - JSON Convert `Key/Value` file parser -Supports 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. +Supports 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. +- If duplicate keys are found, only the last value will be used. 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` @@ -52,7 +54,7 @@ Examples: class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.1' + version = '1.2' description = 'Key/Value file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' diff --git a/tests/test_ini.py b/tests/test_ini.py index 977886e1..f4ac1123 100644 --- a/tests/test_ini.py +++ b/tests/test_ini.py @@ -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) + 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__': unittest.main() diff --git a/tests/test_kv.py b/tests/test_kv.py index 301aa60c..6815cca8 100644 --- a/tests/test_kv.py +++ b/tests/test_kv.py @@ -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) + 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__': unittest.main()