mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-17 01:32:37 +02:00
add support for simple key/value pairs
This commit is contained in:
@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
Usage:
|
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:
|
Compatibility:
|
||||||
|
|
||||||
@ -47,8 +49,8 @@ import configparser
|
|||||||
|
|
||||||
|
|
||||||
class info():
|
class info():
|
||||||
version = '1.1'
|
version = '1.2'
|
||||||
description = 'INI file parser'
|
description = 'INI file parser. Also parses files/output containing simple key/value pairs'
|
||||||
author = 'Kelly Brazil'
|
author = 'Kelly Brazil'
|
||||||
author_email = 'kellyjonbrazil@gmail.com'
|
author_email = 'kellyjonbrazil@gmail.com'
|
||||||
details = 'Using configparser from the standard library'
|
details = 'Using configparser from the standard library'
|
||||||
@ -73,12 +75,26 @@ def process(proc_data):
|
|||||||
Dictionary representing an ini document:
|
Dictionary representing an ini document:
|
||||||
|
|
||||||
{
|
{
|
||||||
ini document converted to a dictionary
|
ini or key/value document converted to a dictionary - see configparser standard
|
||||||
see configparser standard library documentation for more details
|
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
|
return proc_data
|
||||||
|
|
||||||
|
|
||||||
@ -103,9 +119,17 @@ def parse(data, raw=False, quiet=False):
|
|||||||
|
|
||||||
if jc.utils.has_data(data):
|
if jc.utils.has_data(data):
|
||||||
|
|
||||||
ini = configparser.ConfigParser()
|
ini = configparser.ConfigParser(allow_no_value=True)
|
||||||
ini.read_string(data)
|
try:
|
||||||
raw_output = {s: dict(ini.items(s)) for s in ini.sections()}
|
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:
|
if raw:
|
||||||
return raw_output
|
return raw_output
|
||||||
|
Reference in New Issue
Block a user