diff --git a/CHANGELOG b/CHANGELOG index 2ab31bff..3cbe6dce 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,8 @@ jc changelog 20231221 v1.24.1 - Enhance `proc-net-tcp` parser to add opposite endian support for architectures like the s390x +- Add source link to parser documentation +- Refactor parser aliases for `kv`, `pkg_index_deb`, `lsb_release`, and `os-release` 20231216 v1.24.0 - Add `debconf-show` command parser diff --git a/jc/parsers/kv.py b/jc/parsers/kv.py index ab54e62e..de3e3440 100644 --- a/jc/parsers/kv.py +++ b/jc/parsers/kv.py @@ -51,17 +51,16 @@ Examples: "occupation": "Engineer" } """ -import jc.utils -import configparser +import jc.parsers.ini as ini class info(): """Provides parser metadata (version, author, etc.)""" - version = '2.0' + version = '2.1' description = 'Key/Value file and string parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' - details = 'Using configparser from the python standard library' + details = 'Using the ini parser' compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] tags = ['generic', 'file', 'string'] @@ -69,32 +68,6 @@ class info(): __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 a Key/Value pair document. - """ - # remove quotation marks from beginning and end of values - for key in proc_data: - if proc_data[key] is None: - proc_data[key] = '' - - elif proc_data[key].startswith('"') and proc_data[key].endswith('"'): - proc_data[key] = proc_data[key][1:-1] - - elif proc_data[key].startswith("'") and proc_data[key].endswith("'"): - proc_data[key] = proc_data[key][1:-1] - - return proc_data - - def parse(data, raw=False, quiet=False): """ Main text parsing function @@ -109,28 +82,6 @@ def parse(data, raw=False, quiet=False): Dictionary representing a Key/Value pair document. """ - jc.utils.compatibility(__name__, info.compatible, quiet) - jc.utils.input_type_check(data) - - raw_output = {} - - if jc.utils.has_data(data): - - kv_parser = configparser.ConfigParser( - allow_no_value=True, - interpolation=None, - default_section=None, - strict=False - ) - - # don't convert keys to lower-case: - kv_parser.optionxform = lambda option: option - - data = '[data]\n' + data - kv_parser.read_string(data) - output_dict = {s: dict(kv_parser.items(s)) for s in kv_parser.sections()} - for key, value in output_dict['data'].items(): - raw_output[key] = value - - return raw_output if raw else _process(raw_output) - + # This parser is an alias of ini.py + ini.info = info # type: ignore + return ini.parse(data, raw, quiet) diff --git a/jc/parsers/lsb_release.py b/jc/parsers/lsb_release.py index aa3c8dd1..7d8198d9 100644 --- a/jc/parsers/lsb_release.py +++ b/jc/parsers/lsb_release.py @@ -31,17 +31,16 @@ Examples: } """ from jc.jc_types import JSONDictType -import jc.parsers.kv -import jc.utils +import jc.parsers.ini as ini class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.0' + version = '1.1' description = '`lsb_release` command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' - details = 'Using the Key/Value parser' + details = 'Using the ini parser' compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] magic_commands = ['lsb_release'] tags = ['command'] @@ -50,21 +49,6 @@ class info(): __version__ = info.version -def _process(proc_data: JSONDictType) -> JSONDictType: - """ - Final processing to conform to the schema. - - Parameters: - - proc_data: (Dictionary) raw structured data to process - - Returns: - - Dictionary. Structured to conform to the schema. - """ - return jc.parsers.kv._process(proc_data) - - def parse( data: str, raw: bool = False, @@ -83,7 +67,6 @@ def parse( Dictionary. Raw or processed structured data. """ - jc.utils.compatibility(__name__, info.compatible, quiet) - raw_output = jc.parsers.kv.parse(data, raw, quiet) - - return raw_output if raw else _process(raw_output) + # This parser is an alias of ini.py + ini.info = info # type: ignore + return ini.parse(data, raw, quiet) diff --git a/jc/parsers/os_release.py b/jc/parsers/os_release.py index be11de59..d3797b37 100644 --- a/jc/parsers/os_release.py +++ b/jc/parsers/os_release.py @@ -56,17 +56,16 @@ Examples: } """ from jc.jc_types import JSONDictType -import jc.parsers.kv -import jc.utils +import jc.parsers.ini as ini class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.0' + version = '1.1' description = '`/etc/os-release` file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' - details = 'Using the Key/Value parser' + details = 'Using the ini parser' compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] tags = ['file', 'standard', 'string'] @@ -74,21 +73,6 @@ class info(): __version__ = info.version -def _process(proc_data: JSONDictType) -> JSONDictType: - """ - Final processing to conform to the schema. - - Parameters: - - proc_data: (Dictionary) raw structured data to process - - Returns: - - Dictionary. Structured to conform to the schema. - """ - return jc.parsers.kv._process(proc_data) - - def parse( data: str, raw: bool = False, @@ -107,7 +91,6 @@ def parse( Dictionary. Raw or processed structured data. """ - jc.utils.compatibility(__name__, info.compatible, quiet) - raw_output = jc.parsers.kv.parse(data, raw, quiet) - - return raw_output if raw else _process(raw_output) + # This parser is an alias of ini.py + ini.info = info # type: ignore + return ini.parse(data, raw, quiet) diff --git a/jc/parsers/pkg_index_deb.py b/jc/parsers/pkg_index_deb.py index 2c759bd2..03854400 100644 --- a/jc/parsers/pkg_index_deb.py +++ b/jc/parsers/pkg_index_deb.py @@ -112,7 +112,7 @@ import jc.parsers.rpm_qi as rpm_qi class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.0' + version = '1.1' description = 'Debian Package Index file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -143,6 +143,5 @@ def parse( List of Dictionaries. Raw or processed structured data. """ # This parser is an alias of rpm_qi.py - rpm_qi.info.compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] - rpm_qi.info.tags = ['file'] + rpm_qi.info = info # type: ignore return rpm_qi.parse(data, raw, quiet)