1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-13 01:20:24 +02:00

move unsectioned k/v's to the top level

This commit is contained in:
Kelly Brazil
2023-01-05 15:44:21 -08:00
parent 362977e598
commit 7a93a61f54
2 changed files with 47 additions and 21 deletions

View File

@ -75,6 +75,7 @@ Examples:
""" """
import jc.utils import jc.utils
import configparser import configparser
import uuid
class info(): class info():
@ -91,6 +92,19 @@ class info():
__version__ = info.version __version__ = info.version
def _remove_quotes(value):
if value is not None and value.startswith('"') and value.endswith('"'):
value = value[1:-1]
elif value is not None and value.startswith("'") and value.endswith("'"):
value = value[1:-1]
elif value is None:
value = ''
return value
def _process(proc_data): def _process(proc_data):
""" """
Final processing to conform to the schema. Final processing to conform to the schema.
@ -104,16 +118,13 @@ def _process(proc_data):
Dictionary representing the INI file. Dictionary representing the INI file.
""" """
# remove quotation marks from beginning and end of values # remove quotation marks from beginning and end of values
for heading in proc_data: for k, v in proc_data.items():
for key, value in proc_data[heading].items(): if isinstance(v, dict):
if value is not None and value.startswith('"') and value.endswith('"'): for key, value in v.items():
proc_data[heading][key] = value[1:-1] v[key] = _remove_quotes(value)
continue
elif value is not None and value.startswith("'") and value.endswith("'"): proc_data[k] = _remove_quotes(v)
proc_data[heading][key] = value[1:-1]
elif value is None:
proc_data[heading][key] = ''
return proc_data return proc_data
@ -153,11 +164,21 @@ def parse(data, raw=False, quiet=False):
raw_output = {s: dict(ini_parser.items(s)) for s in ini_parser.sections()} raw_output = {s: dict(ini_parser.items(s)) for s in ini_parser.sections()}
except configparser.MissingSectionHeaderError: except configparser.MissingSectionHeaderError:
data = '[_top_level_section_]\n' + data # find a top-level section name that will not collide with any existing ones
ini_parser.read_string(data) while True:
raw_output = {s: dict(ini_parser.items(s)) for s in ini_parser.sections()} my_uuid = str(uuid.uuid4())
if my_uuid not in data:
break
data = f'[{my_uuid}]\n' + data
ini_parser.read_string(data)
temp_dict = {s: dict(ini_parser.items(s)) for s in ini_parser.sections()}
# move items under fake top-level sections to the root
raw_output = temp_dict.pop(my_uuid)
# get the rest of the sections
raw_output.update(temp_dict)
return raw_output if raw else _process(raw_output)
if raw:
return raw_output
else:
return _process(raw_output)

View File

@ -76,14 +76,19 @@ key: value1
another_key = foo another_key = foo
[section2] [section2]
key3: bar key3: bar
key4 =
[section 3]
key5 = "quoted"
''' '''
expected = { expected = {
'_top_level_section_': { 'key': 'value1',
'key': 'value1', 'another_key': 'foo',
'another_key': 'foo'
},
'section2': { 'section2': {
'key3': 'bar' 'key3': 'bar',
'key4': ''
},
'section 3': {
'key5': 'quoted'
} }
} }
self.assertEqual(jc.parsers.ini.parse(data, quiet=True), expected) self.assertEqual(jc.parsers.ini.parse(data, quiet=True), expected)