1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

refactor alias parsers

This commit is contained in:
Kelly Brazil
2023-12-22 09:43:21 -08:00
parent 45a6cfc9cf
commit fc75ce90d1
5 changed files with 22 additions and 104 deletions

View File

@ -3,6 +3,8 @@ jc changelog
20231221 v1.24.1 20231221 v1.24.1
- Enhance `proc-net-tcp` parser to add opposite endian support for architectures - Enhance `proc-net-tcp` parser to add opposite endian support for architectures
like the s390x 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 20231216 v1.24.0
- Add `debconf-show` command parser - Add `debconf-show` command parser

View File

@ -51,17 +51,16 @@ Examples:
"occupation": "Engineer" "occupation": "Engineer"
} }
""" """
import jc.utils import jc.parsers.ini as ini
import configparser
class info(): class info():
"""Provides parser metadata (version, author, etc.)""" """Provides parser metadata (version, author, etc.)"""
version = '2.0' version = '2.1'
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 = 'Using configparser from the python standard library' details = 'Using the ini parser'
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
tags = ['generic', 'file', 'string'] tags = ['generic', 'file', 'string']
@ -69,32 +68,6 @@ 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 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): def parse(data, raw=False, quiet=False):
""" """
Main text parsing function Main text parsing function
@ -109,28 +82,6 @@ def parse(data, raw=False, quiet=False):
Dictionary representing a Key/Value pair document. Dictionary representing a Key/Value pair document.
""" """
jc.utils.compatibility(__name__, info.compatible, quiet) # This parser is an alias of ini.py
jc.utils.input_type_check(data) ini.info = info # type: ignore
return ini.parse(data, raw, quiet)
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)

View File

@ -31,17 +31,16 @@ Examples:
} }
""" """
from jc.jc_types import JSONDictType from jc.jc_types import JSONDictType
import jc.parsers.kv import jc.parsers.ini as ini
import jc.utils
class info(): class info():
"""Provides parser metadata (version, author, etc.)""" """Provides parser metadata (version, author, etc.)"""
version = '1.0' version = '1.1'
description = '`lsb_release` command parser' description = '`lsb_release` command parser'
author = 'Kelly Brazil' author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com' author_email = 'kellyjonbrazil@gmail.com'
details = 'Using the Key/Value parser' details = 'Using the ini parser'
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
magic_commands = ['lsb_release'] magic_commands = ['lsb_release']
tags = ['command'] tags = ['command']
@ -50,21 +49,6 @@ class info():
__version__ = info.version __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( def parse(
data: str, data: str,
raw: bool = False, raw: bool = False,
@ -83,7 +67,6 @@ def parse(
Dictionary. Raw or processed structured data. Dictionary. Raw or processed structured data.
""" """
jc.utils.compatibility(__name__, info.compatible, quiet) # This parser is an alias of ini.py
raw_output = jc.parsers.kv.parse(data, raw, quiet) ini.info = info # type: ignore
return ini.parse(data, raw, quiet)
return raw_output if raw else _process(raw_output)

View File

@ -56,17 +56,16 @@ Examples:
} }
""" """
from jc.jc_types import JSONDictType from jc.jc_types import JSONDictType
import jc.parsers.kv import jc.parsers.ini as ini
import jc.utils
class info(): class info():
"""Provides parser metadata (version, author, etc.)""" """Provides parser metadata (version, author, etc.)"""
version = '1.0' version = '1.1'
description = '`/etc/os-release` file parser' description = '`/etc/os-release` file parser'
author = 'Kelly Brazil' author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com' author_email = 'kellyjonbrazil@gmail.com'
details = 'Using the Key/Value parser' details = 'Using the ini parser'
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
tags = ['file', 'standard', 'string'] tags = ['file', 'standard', 'string']
@ -74,21 +73,6 @@ class info():
__version__ = info.version __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( def parse(
data: str, data: str,
raw: bool = False, raw: bool = False,
@ -107,7 +91,6 @@ def parse(
Dictionary. Raw or processed structured data. Dictionary. Raw or processed structured data.
""" """
jc.utils.compatibility(__name__, info.compatible, quiet) # This parser is an alias of ini.py
raw_output = jc.parsers.kv.parse(data, raw, quiet) ini.info = info # type: ignore
return ini.parse(data, raw, quiet)
return raw_output if raw else _process(raw_output)

View File

@ -112,7 +112,7 @@ import jc.parsers.rpm_qi as rpm_qi
class info(): class info():
"""Provides parser metadata (version, author, etc.)""" """Provides parser metadata (version, author, etc.)"""
version = '1.0' version = '1.1'
description = 'Debian Package Index file parser' description = 'Debian Package Index file parser'
author = 'Kelly Brazil' author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com' author_email = 'kellyjonbrazil@gmail.com'
@ -143,6 +143,5 @@ def parse(
List of Dictionaries. Raw or processed structured data. List of Dictionaries. Raw or processed structured data.
""" """
# This parser is an alias of rpm_qi.py # This parser is an alias of rpm_qi.py
rpm_qi.info.compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] rpm_qi.info = info # type: ignore
rpm_qi.info.tags = ['file']
return rpm_qi.parse(data, raw, quiet) return rpm_qi.parse(data, raw, quiet)