mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-23 00:29:59 +02:00
112 lines
2.2 KiB
Python
112 lines
2.2 KiB
Python
"""jc - JSON Convert `foo` command output parser
|
|
|
|
<<Short foo description and caveats>>
|
|
|
|
Usage (cli):
|
|
|
|
$ foo | jc --foo
|
|
|
|
or
|
|
|
|
$ jc foo
|
|
|
|
Usage (module):
|
|
|
|
import jc
|
|
result = jc.parse('foo', foo_command_output)
|
|
|
|
Schema:
|
|
|
|
[
|
|
{
|
|
"foo": string,
|
|
"bar": boolean,
|
|
"baz": integer
|
|
}
|
|
]
|
|
|
|
Examples:
|
|
|
|
$ foo | jc --foo -p
|
|
[]
|
|
|
|
$ foo | jc --foo -p -r
|
|
[]
|
|
"""
|
|
from typing import List
|
|
from jc.jc_types import JSONDictType
|
|
import jc.utils
|
|
|
|
|
|
class info():
|
|
"""Provides parser metadata (version, author, etc.)"""
|
|
version = '1.0'
|
|
description = '`foo` command parser'
|
|
author = 'John Doe'
|
|
author_email = 'johndoe@gmail.com'
|
|
# details = 'enter any other details here'
|
|
|
|
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
|
|
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
|
|
magic_commands = ['foo']
|
|
|
|
|
|
__version__ = info.version
|
|
|
|
|
|
def _process(proc_data: List[JSONDictType]) -> List[JSONDictType]:
|
|
"""
|
|
Final processing to conform to the schema.
|
|
|
|
Parameters:
|
|
|
|
proc_data: (List of Dictionaries) raw structured data to process
|
|
|
|
Returns:
|
|
|
|
List of Dictionaries. Structured to conform to the schema.
|
|
"""
|
|
|
|
# process the data here
|
|
# rebuild output for added semantic information
|
|
# use helper functions in jc.utils for int, float, bool
|
|
# conversions and timestamps
|
|
|
|
return proc_data
|
|
|
|
|
|
def parse(
|
|
data: str,
|
|
raw: bool = False,
|
|
quiet: bool = False
|
|
) -> List[JSONDictType]:
|
|
"""
|
|
Main text parsing function
|
|
|
|
Parameters:
|
|
|
|
data: (string) text data to parse
|
|
raw: (boolean) unprocessed output if True
|
|
quiet: (boolean) suppress warning messages if True
|
|
|
|
Returns:
|
|
|
|
List of Dictionaries. Raw or processed structured data.
|
|
"""
|
|
jc.utils.compatibility(__name__, info.compatible, quiet)
|
|
jc.utils.input_type_check(data)
|
|
|
|
raw_output: List[JSONDictType] = []
|
|
|
|
if jc.utils.has_data(data):
|
|
|
|
for line in filter(None, data.splitlines()):
|
|
|
|
# parse the content here
|
|
# check out helper functions in jc.utils
|
|
# and jc.parsers.universal
|
|
|
|
pass
|
|
|
|
return raw_output if raw else _process(raw_output)
|