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

add support for simple key/value pairs

This commit is contained in:
Kelly Brazil
2020-07-24 16:16:24 -07:00
parent 928e39cd10
commit f5e546c6fa

View File

@ -2,7 +2,9 @@
Usage:
specify --ini as the first argument if the piped input is coming from an INI file
Specify --ini as the first argument if the piped input is coming from an INI file or any
simple key/value pair file. Delimiter can be '=' or ':'. Missing values are supported.
Comment prefix can be '#' or ';'. Comments must be on their own line.
Compatibility:
@ -47,8 +49,8 @@ import configparser
class info():
version = '1.1'
description = 'INI file parser'
version = '1.2'
description = 'INI file parser. Also parses files/output containing simple key/value pairs'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
details = 'Using configparser from the standard library'
@ -73,12 +75,26 @@ def process(proc_data):
Dictionary representing an ini document:
{
ini document converted to a dictionary
see configparser standard library documentation for more details
ini or key/value document converted to a dictionary - see configparser standard
library documentation for more details.
Note: Values starting and ending with quotation marks will have the marks removed.
If you would like to keep the quotation markes, use the -r or raw=True argument.
}
"""
# 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.startswith('"') and value.endswith('"'):
proc_data[heading][key] = value.lstrip('"').rstrip('"')
# simple key/value files with no headers
else:
if proc_data[heading].startswith('"') and proc_data[heading].endswith('"'):
proc_data[heading] = proc_data[heading].lstrip('"').rstrip('"')
# No further processing
return proc_data
@ -103,10 +119,18 @@ def parse(data, raw=False, quiet=False):
if jc.utils.has_data(data):
ini = configparser.ConfigParser()
ini = configparser.ConfigParser(allow_no_value=True)
try:
ini.read_string(data)
raw_output = {s: dict(ini.items(s)) for s in ini.sections()}
except configparser.MissingSectionHeaderError:
data = '[data]\n' + data
ini.read_string(data)
output_dict = {s: dict(ini.items(s)) for s in ini.sections()}
for key, value in output_dict['data'].items():
raw_output[key] = value
if raw:
return raw_output
else: