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