mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-17 01:32:37 +02:00
separate kv and ini parsers. add fake top-level section in ini files that are missing it.
This commit is contained in:
@ -1242,4 +1242,4 @@ cat istio.yaml | jc -p --yaml
|
|||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
© 2019-2022 Kelly Brazil
|
© 2019-2023 Kelly Brazil
|
@ -92,4 +92,4 @@ Returns:
|
|||||||
### Parser Information
|
### Parser Information
|
||||||
Compatibility: linux, darwin, cygwin, win32, aix, freebsd
|
Compatibility: linux, darwin, cygwin, win32, aix, freebsd
|
||||||
|
|
||||||
Version 1.8 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
Version 2.0 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||||
|
@ -65,8 +65,6 @@ def parse(data, raw=False, quiet=False)
|
|||||||
|
|
||||||
Main text parsing function
|
Main text parsing function
|
||||||
|
|
||||||
Note: this is just a wrapper for jc.parsers.ini
|
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
|
|
||||||
data: (string) text data to parse
|
data: (string) text data to parse
|
||||||
@ -75,9 +73,9 @@ Parameters:
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
|
|
||||||
Dictionary representing the key/value file
|
Dictionary representing the ini file
|
||||||
|
|
||||||
### Parser Information
|
### Parser Information
|
||||||
Compatibility: linux, darwin, cygwin, win32, aix, freebsd
|
Compatibility: linux, darwin, cygwin, win32, aix, freebsd
|
||||||
|
|
||||||
Version 1.2 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
Version 2.0 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||||
|
@ -70,11 +70,11 @@ import configparser
|
|||||||
|
|
||||||
class info():
|
class info():
|
||||||
"""Provides parser metadata (version, author, etc.)"""
|
"""Provides parser metadata (version, author, etc.)"""
|
||||||
version = '1.8'
|
version = '2.0'
|
||||||
description = 'INI file parser'
|
description = 'INI file parser'
|
||||||
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 python standard library'
|
||||||
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
|
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
|
||||||
tags = ['standard', 'file', 'string']
|
tags = ['standard', 'file', 'string']
|
||||||
|
|
||||||
@ -155,11 +155,9 @@ def parse(data, raw=False, quiet=False):
|
|||||||
raw_output = {s: dict(ini.items(s)) for s in ini.sections()}
|
raw_output = {s: dict(ini.items(s)) for s in ini.sections()}
|
||||||
|
|
||||||
except configparser.MissingSectionHeaderError:
|
except configparser.MissingSectionHeaderError:
|
||||||
data = '[data]\n' + data
|
data = '[_top_level_section_]\n' + data
|
||||||
ini.read_string(data)
|
ini.read_string(data)
|
||||||
output_dict = {s: dict(ini.items(s)) for s in ini.sections()}
|
raw_output = {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
|
||||||
|
@ -50,15 +50,17 @@ Examples:
|
|||||||
"occupation": "Engineer"
|
"occupation": "Engineer"
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
import jc.utils
|
||||||
|
import configparser
|
||||||
|
|
||||||
|
|
||||||
class info():
|
class info():
|
||||||
"""Provides parser metadata (version, author, etc.)"""
|
"""Provides parser metadata (version, author, etc.)"""
|
||||||
version = '1.2'
|
version = '2.0'
|
||||||
description = 'Key/Value file and string parser'
|
description = 'Key/Value file and string parser'
|
||||||
author = 'Kelly Brazil'
|
author = 'Kelly Brazil'
|
||||||
author_email = 'kellyjonbrazil@gmail.com'
|
author_email = 'kellyjonbrazil@gmail.com'
|
||||||
details = 'This is a wrapper for the INI parser'
|
details = 'Using configparser from the python standard library'
|
||||||
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
|
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
|
||||||
tags = ['generic', 'file', 'string']
|
tags = ['generic', 'file', 'string']
|
||||||
|
|
||||||
@ -66,12 +68,50 @@ class info():
|
|||||||
__version__ = info.version
|
__version__ = info.version
|
||||||
|
|
||||||
|
|
||||||
|
def _process(proc_data):
|
||||||
|
"""
|
||||||
|
Final processing to conform to the schema.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
proc_data: (Dictionary) raw structured data to process
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
Dictionary representing an ini or simple key/value pair document.
|
||||||
|
"""
|
||||||
|
# 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 is not None and value.startswith('"') and value.endswith('"'):
|
||||||
|
proc_data[heading][key] = value.lstrip('"').rstrip('"')
|
||||||
|
|
||||||
|
elif value is not None and value.startswith("'") and value.endswith("'"):
|
||||||
|
proc_data[heading][key] = value.lstrip("'").rstrip("'")
|
||||||
|
|
||||||
|
elif value is None:
|
||||||
|
proc_data[heading][key] = ''
|
||||||
|
|
||||||
|
# simple key/value files with no headers
|
||||||
|
else:
|
||||||
|
if proc_data[heading] is not None and proc_data[heading].startswith('"') and proc_data[heading].endswith('"'):
|
||||||
|
proc_data[heading] = proc_data[heading].lstrip('"').rstrip('"')
|
||||||
|
|
||||||
|
elif proc_data[heading] is not None and proc_data[heading].startswith("'") and proc_data[heading].endswith("'"):
|
||||||
|
proc_data[heading] = proc_data[heading].lstrip("'").rstrip("'")
|
||||||
|
|
||||||
|
elif proc_data[heading] is None:
|
||||||
|
proc_data[heading] = ''
|
||||||
|
|
||||||
|
return proc_data
|
||||||
|
|
||||||
|
|
||||||
def parse(data, raw=False, quiet=False):
|
def parse(data, raw=False, quiet=False):
|
||||||
"""
|
"""
|
||||||
Main text parsing function
|
Main text parsing function
|
||||||
|
|
||||||
Note: this is just a wrapper for jc.parsers.ini
|
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
|
|
||||||
data: (string) text data to parse
|
data: (string) text data to parse
|
||||||
@ -80,7 +120,35 @@ def parse(data, raw=False, quiet=False):
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
|
|
||||||
Dictionary representing the key/value file
|
Dictionary representing the ini file
|
||||||
"""
|
"""
|
||||||
import jc.parsers.ini
|
jc.utils.compatibility(__name__, info.compatible, quiet)
|
||||||
return jc.parsers.ini.parse(data, raw=raw, quiet=quiet)
|
jc.utils.input_type_check(data)
|
||||||
|
|
||||||
|
raw_output = {}
|
||||||
|
|
||||||
|
if jc.utils.has_data(data):
|
||||||
|
|
||||||
|
ini = configparser.ConfigParser(allow_no_value=True,
|
||||||
|
interpolation=None,
|
||||||
|
strict=False)
|
||||||
|
|
||||||
|
# don't convert keys to lower-case:
|
||||||
|
ini.optionxform = lambda option: option
|
||||||
|
|
||||||
|
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:
|
||||||
|
return _process(raw_output)
|
||||||
|
|
||||||
|
4
man/jc.1
4
man/jc.1
@ -1,4 +1,4 @@
|
|||||||
.TH jc 1 2022-12-30 1.22.4 "JSON Convert"
|
.TH jc 1 2023-01-03 1.22.5 "JSON Convert"
|
||||||
.SH NAME
|
.SH NAME
|
||||||
\fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, and strings
|
\fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, and strings
|
||||||
.SH SYNOPSIS
|
.SH SYNOPSIS
|
||||||
@ -1232,6 +1232,6 @@ Kelly Brazil (kellyjonbrazil@gmail.com)
|
|||||||
https://github.com/kellyjonbrazil/jc
|
https://github.com/kellyjonbrazil/jc
|
||||||
|
|
||||||
.SH COPYRIGHT
|
.SH COPYRIGHT
|
||||||
Copyright (c) 2019-2022 Kelly Brazil
|
Copyright (c) 2019-2023 Kelly Brazil
|
||||||
|
|
||||||
License: MIT License
|
License: MIT License
|
@ -58,11 +58,12 @@ class MyTests(unittest.TestCase):
|
|||||||
Test input that contains duplicate keys. Only the last value should be used.
|
Test input that contains duplicate keys. Only the last value should be used.
|
||||||
"""
|
"""
|
||||||
data = '''
|
data = '''
|
||||||
|
[section]
|
||||||
duplicate_key: value1
|
duplicate_key: value1
|
||||||
another_key = foo
|
another_key = foo
|
||||||
duplicate_key = value2
|
duplicate_key = value2
|
||||||
'''
|
'''
|
||||||
expected = {'duplicate_key': 'value2', 'another_key': 'foo'}
|
expected = {'section': {'duplicate_key': 'value2', 'another_key': 'foo'}}
|
||||||
self.assertEqual(jc.parsers.ini.parse(data, quiet=True), expected)
|
self.assertEqual(jc.parsers.ini.parse(data, quiet=True), expected)
|
||||||
|
|
||||||
def test_ini_doublequote(self):
|
def test_ini_doublequote(self):
|
||||||
|
Reference in New Issue
Block a user