mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-11 01:10:37 +02:00
add lsb_release parser
This commit is contained in:
@ -80,6 +80,7 @@ parsers: List[str] = [
|
|||||||
'ls',
|
'ls',
|
||||||
'ls-s',
|
'ls-s',
|
||||||
'lsattr',
|
'lsattr',
|
||||||
|
'lsb-release',
|
||||||
'lsblk',
|
'lsblk',
|
||||||
'lsmod',
|
'lsmod',
|
||||||
'lsof',
|
'lsof',
|
||||||
|
89
jc/parsers/lsb_release.py
Normal file
89
jc/parsers/lsb_release.py
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
"""jc - JSON Convert `lsb_release` command parser
|
||||||
|
|
||||||
|
This parser is an alias to the Key/Value parser (`--kv`).
|
||||||
|
|
||||||
|
Usage (cli):
|
||||||
|
|
||||||
|
$ lsb_release -a | jc --lsb-release
|
||||||
|
|
||||||
|
or
|
||||||
|
$ jc lsb_release -a
|
||||||
|
|
||||||
|
Usage (module):
|
||||||
|
|
||||||
|
import jc
|
||||||
|
result = jc.parse('lsb_release', lsb_release_command_output)
|
||||||
|
|
||||||
|
Schema:
|
||||||
|
|
||||||
|
{
|
||||||
|
"<key>": string
|
||||||
|
}
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
$ lsb_release -a | jc --lsb-release -p
|
||||||
|
{
|
||||||
|
"Distributor ID": "Ubuntu",
|
||||||
|
"Description": "Ubuntu 16.04.6 LTS",
|
||||||
|
"Release": "16.04",
|
||||||
|
"Codename": "xenial"
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
from jc.jc_types import JSONDictType
|
||||||
|
import jc.parsers.kv
|
||||||
|
import jc.utils
|
||||||
|
|
||||||
|
|
||||||
|
class info():
|
||||||
|
"""Provides parser metadata (version, author, etc.)"""
|
||||||
|
version = '1.0'
|
||||||
|
description = '`lsb_release` command parser'
|
||||||
|
author = 'Kelly Brazil'
|
||||||
|
author_email = 'kellyjonbrazil@gmail.com'
|
||||||
|
details = 'Using the Key/Value parser'
|
||||||
|
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
|
||||||
|
magic_commands = ['lsb_release']
|
||||||
|
tags = ['command']
|
||||||
|
|
||||||
|
|
||||||
|
__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,
|
||||||
|
quiet: bool = False
|
||||||
|
) -> 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:
|
||||||
|
|
||||||
|
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)
|
@ -9,7 +9,7 @@ Usage (cli):
|
|||||||
Usage (module):
|
Usage (module):
|
||||||
|
|
||||||
import jc
|
import jc
|
||||||
result = jc.parse('os_release', os_release_command_output)
|
result = jc.parse('os_release', os_release_output)
|
||||||
|
|
||||||
Schema:
|
Schema:
|
||||||
|
|
||||||
|
1
tests/fixtures/generic/lsb_release-a.json
vendored
Normal file
1
tests/fixtures/generic/lsb_release-a.json
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"Distributor ID":"Ubuntu","Description":"Ubuntu 16.04.6 LTS","Release":"16.04","Codename":"xenial"}
|
4
tests/fixtures/generic/lsb_release-a.out
vendored
Normal file
4
tests/fixtures/generic/lsb_release-a.out
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
Distributor ID: Ubuntu
|
||||||
|
Description: Ubuntu 16.04.6 LTS
|
||||||
|
Release: 16.04
|
||||||
|
Codename: xenial
|
47
tests/test_lsb_release.py
Normal file
47
tests/test_lsb_release.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
import json
|
||||||
|
from typing import Dict
|
||||||
|
from jc.parsers.lsb_release import parse
|
||||||
|
|
||||||
|
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
|
||||||
|
class MyTests(unittest.TestCase):
|
||||||
|
f_in: Dict = {}
|
||||||
|
f_json: Dict = {}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
fixtures = {
|
||||||
|
'lsb_release_a': (
|
||||||
|
'fixtures/generic/lsb_release-a.out',
|
||||||
|
'fixtures/generic/lsb_release-a.json')
|
||||||
|
}
|
||||||
|
|
||||||
|
for file, filepaths in fixtures.items():
|
||||||
|
with open(os.path.join(THIS_DIR, filepaths[0]), 'r', encoding='utf-8') as a, \
|
||||||
|
open(os.path.join(THIS_DIR, filepaths[1]), 'r', encoding='utf-8') as b:
|
||||||
|
cls.f_in[file] = a.read()
|
||||||
|
cls.f_json[file] = json.loads(b.read())
|
||||||
|
|
||||||
|
|
||||||
|
def test_lsb_release_nodata(self):
|
||||||
|
"""
|
||||||
|
Test 'lsb_release' with no data
|
||||||
|
"""
|
||||||
|
self.assertEqual(parse('', quiet=True), {})
|
||||||
|
|
||||||
|
|
||||||
|
def test_lsb_release_a(self):
|
||||||
|
"""
|
||||||
|
Test 'lsb_release -a'
|
||||||
|
"""
|
||||||
|
self.assertEqual(
|
||||||
|
parse(self.f_in['lsb_release_a'], quiet=True),
|
||||||
|
self.f_json['lsb_release_a']
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
Reference in New Issue
Block a user