mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-11 01:10:37 +02:00
add update-alternatives --get-selections parser
This commit is contained in:
@ -94,6 +94,7 @@ parsers = [
|
||||
'ufw',
|
||||
'ufw-appinfo',
|
||||
'uname',
|
||||
'update-alt-gs',
|
||||
'update-alt-q',
|
||||
'upower',
|
||||
'uptime',
|
||||
|
111
jc/parsers/update_alt_gs.py
Normal file
111
jc/parsers/update_alt_gs.py
Normal file
@ -0,0 +1,111 @@
|
||||
"""jc - JSON Convert `update-alternatives --get-selections` command output parser
|
||||
|
||||
Usage (cli):
|
||||
|
||||
$ update-alternatives --get-selections | jc --update-alt-gs
|
||||
|
||||
or
|
||||
|
||||
$ jc update-alternatives --get-selections
|
||||
|
||||
Usage (module):
|
||||
|
||||
import jc
|
||||
result = jc.parse('update-alt-gs',
|
||||
update_alternatives_get_selections_command_output)
|
||||
|
||||
Schema:
|
||||
|
||||
[
|
||||
{
|
||||
"update-alternatives --get-selection": string,
|
||||
"bar": boolean,
|
||||
"baz": integer
|
||||
}
|
||||
]
|
||||
|
||||
Examples:
|
||||
|
||||
$ update-alternatives --get-selections | jc --update-alt-gs -p
|
||||
[
|
||||
{
|
||||
"name": "arptables",
|
||||
"status": "auto",
|
||||
"current": "/usr/sbin/arptables-nft"
|
||||
},
|
||||
{
|
||||
"name": "awk",
|
||||
"status": "auto",
|
||||
"current": "/usr/bin/gawk"
|
||||
}
|
||||
]
|
||||
"""
|
||||
from typing import List, Dict
|
||||
import jc.utils
|
||||
|
||||
|
||||
class info():
|
||||
"""Provides parser metadata (version, author, etc.)"""
|
||||
version = '1.0'
|
||||
description = '`update-alternatives --get-selections` command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
compatible = ['linux']
|
||||
magic_commands = ['update-alternatives --get-selections']
|
||||
|
||||
|
||||
__version__ = info.version
|
||||
|
||||
|
||||
def _process(proc_data: List[Dict]) -> List[Dict]:
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
# nothing to process
|
||||
return proc_data
|
||||
|
||||
|
||||
def parse(
|
||||
data: str,
|
||||
raw: bool = False,
|
||||
quiet: bool = False
|
||||
) -> List[Dict]:
|
||||
"""
|
||||
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 = []
|
||||
output_line = {}
|
||||
|
||||
if jc.utils.has_data(data):
|
||||
|
||||
for line in filter(None, data.splitlines()):
|
||||
line_list = line.split(maxsplit=2)
|
||||
output_line = {
|
||||
"name": line_list[0],
|
||||
"status": line_list[1],
|
||||
"current": line_list[2]
|
||||
}
|
||||
raw_output.append(output_line)
|
||||
|
||||
return raw_output if raw else _process(raw_output)
|
41
tests/fixtures/generic/update-alternatives-get-selection.out
vendored
Normal file
41
tests/fixtures/generic/update-alternatives-get-selection.out
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
arptables auto /usr/sbin/arptables-nft
|
||||
awk auto /usr/bin/gawk
|
||||
builtins.7.gz auto /usr/share/man/man7/bash-builtins.7.gz
|
||||
c++ auto /usr/bin/g++
|
||||
c89 auto /usr/bin/c89-gcc
|
||||
c99 auto /usr/bin/c99-gcc
|
||||
cc auto /usr/bin/gcc
|
||||
cpp auto /usr/bin/cpp
|
||||
ebtables auto /usr/sbin/ebtables-nft
|
||||
editor auto /bin/nano
|
||||
ex auto /usr/bin/vim.basic
|
||||
fakeroot auto /usr/bin/fakeroot-sysv
|
||||
ftp auto /usr/bin/netkit-ftp
|
||||
infobrowser auto /usr/bin/info
|
||||
ip6tables auto /usr/sbin/ip6tables-nft
|
||||
iptables auto /usr/sbin/iptables-nft
|
||||
jsondiff auto /usr/bin/json-patch-jsondiff
|
||||
lzma auto /usr/bin/xz
|
||||
mt auto /bin/mt-gnu
|
||||
nc auto /bin/nc.openbsd
|
||||
newt-palette auto /etc/newt/palette.ubuntu
|
||||
pager auto /usr/bin/less
|
||||
pico auto /bin/nano
|
||||
pinentry auto /usr/bin/pinentry-curses
|
||||
rcp auto /usr/bin/scp
|
||||
rlogin auto /usr/bin/slogin
|
||||
rmt auto /usr/sbin/rmt-tar
|
||||
rsh auto /usr/bin/ssh
|
||||
rview auto /usr/bin/vim.basic
|
||||
rvim auto /usr/bin/vim.basic
|
||||
sar auto /usr/bin/sar.sysstat
|
||||
telnet auto /usr/bin/telnet.netkit
|
||||
text.plymouth auto /usr/share/plymouth/themes/ubuntu-text/ubuntu-text.plymouth
|
||||
traceroute6 auto /usr/bin/traceroute6.iputils
|
||||
vi auto /usr/bin/vim.basic
|
||||
view auto /usr/bin/vim.basic
|
||||
vim auto /usr/bin/vim.basic
|
||||
vimdiff auto /usr/bin/vim.basic
|
||||
vtrgb auto /etc/console-setup/vtrgb
|
||||
w auto /usr/bin/w.procps
|
||||
write auto /usr/bin/write.ul
|
Reference in New Issue
Block a user