mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-19 00:17:51 +02:00
initial findmnt parser
This commit is contained in:
@ -38,6 +38,7 @@ parsers: List[str] = [
|
|||||||
'email-address',
|
'email-address',
|
||||||
'env',
|
'env',
|
||||||
'file',
|
'file',
|
||||||
|
'findmnt',
|
||||||
'finger',
|
'finger',
|
||||||
'free',
|
'free',
|
||||||
'fstab',
|
'fstab',
|
||||||
|
135
jc/parsers/findmnt.py
Normal file
135
jc/parsers/findmnt.py
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
"""jc - JSON Convert `findmnt` command output parser
|
||||||
|
|
||||||
|
<<Short findmnt description and caveats>>
|
||||||
|
|
||||||
|
Usage (cli):
|
||||||
|
|
||||||
|
$ findmnt | jc --findmnt
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
$ jc findmnt
|
||||||
|
|
||||||
|
Usage (module):
|
||||||
|
|
||||||
|
import jc
|
||||||
|
result = jc.parse('findmnt', findmnt_command_output)
|
||||||
|
|
||||||
|
Schema:
|
||||||
|
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"findmnt": string,
|
||||||
|
"bar": boolean,
|
||||||
|
"baz": integer
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
$ findmnt | jc --findmnt -p
|
||||||
|
[]
|
||||||
|
|
||||||
|
$ findmnt | jc --findmnt -p -r
|
||||||
|
[]
|
||||||
|
"""
|
||||||
|
import re
|
||||||
|
from typing import List, Dict
|
||||||
|
from jc.jc_types import JSONDictType
|
||||||
|
from jc.parsers.universal import simple_table_parse
|
||||||
|
import jc.utils
|
||||||
|
|
||||||
|
|
||||||
|
class info():
|
||||||
|
"""Provides parser metadata (version, author, etc.)"""
|
||||||
|
version = '1.0'
|
||||||
|
description = '`findmnt` command parser'
|
||||||
|
author = 'Kelly Brazil'
|
||||||
|
author_email = 'kellyjonbrazil@gmail.com'
|
||||||
|
compatible = ['linux']
|
||||||
|
magic_commands = ['findmnt']
|
||||||
|
|
||||||
|
|
||||||
|
__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.
|
||||||
|
"""
|
||||||
|
# split normal options and k/v options
|
||||||
|
for item in proc_data:
|
||||||
|
reg_options = []
|
||||||
|
kv_options = {}
|
||||||
|
|
||||||
|
if 'options' in item:
|
||||||
|
opt_list = item['options'].split(',') # type: ignore
|
||||||
|
|
||||||
|
for option in opt_list:
|
||||||
|
if '=' in option:
|
||||||
|
k, v = option.split('=', maxsplit=1)
|
||||||
|
kv_options[k] = v
|
||||||
|
|
||||||
|
else:
|
||||||
|
reg_options.append(option)
|
||||||
|
|
||||||
|
if reg_options:
|
||||||
|
item['options'] = reg_options
|
||||||
|
if kv_options:
|
||||||
|
item['kv_options'] = kv_options
|
||||||
|
|
||||||
|
return proc_data
|
||||||
|
|
||||||
|
|
||||||
|
def _replace(matchobj):
|
||||||
|
if matchobj:
|
||||||
|
matchlen = len(matchobj.group(1))
|
||||||
|
return ' ' * matchlen + '/'
|
||||||
|
|
||||||
|
|
||||||
|
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[Dict] = []
|
||||||
|
table: List[str] = []
|
||||||
|
|
||||||
|
if jc.utils.has_data(data):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for line in filter(None, data.splitlines()):
|
||||||
|
|
||||||
|
# remove initial drawing characters
|
||||||
|
line = re.sub(r'^([│ ├─└─|`-]+)/', _replace, line, count=1)
|
||||||
|
table.append(line)
|
||||||
|
|
||||||
|
table[0] = table[0].lower()
|
||||||
|
raw_output = simple_table_parse(table)
|
||||||
|
|
||||||
|
return raw_output if raw else _process(raw_output)
|
33
tests/fixtures/centos-7.7/findmnt.out
vendored
Normal file
33
tests/fixtures/centos-7.7/findmnt.out
vendored
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
TARGET SOURCE FSTYPE OPTIONS
|
||||||
|
/ /dev/mapper/centos-root xfs rw,relatime,seclabel,attr2,inode64,noquota
|
||||||
|
├─/sys sysfs sysfs rw,nosuid,nodev,noexec,relatime,seclabel
|
||||||
|
│ ├─/sys/kernel/security securityfs securityfs rw,nosuid,nodev,noexec,relatime
|
||||||
|
│ ├─/sys/fs/cgroup tmpfs tmpfs ro,nosuid,nodev,noexec,seclabel,mode=755
|
||||||
|
│ │ ├─/sys/fs/cgroup/systemd cgroup cgroup rw,nosuid,nodev,noexec,relatime,seclabel,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd
|
||||||
|
│ │ ├─/sys/fs/cgroup/net_cls,net_prio cgroup cgroup rw,nosuid,nodev,noexec,relatime,seclabel,net_prio,net_cls
|
||||||
|
│ │ ├─/sys/fs/cgroup/blkio cgroup cgroup rw,nosuid,nodev,noexec,relatime,seclabel,blkio
|
||||||
|
│ │ ├─/sys/fs/cgroup/devices cgroup cgroup rw,nosuid,nodev,noexec,relatime,seclabel,devices
|
||||||
|
│ │ ├─/sys/fs/cgroup/hugetlb cgroup cgroup rw,nosuid,nodev,noexec,relatime,seclabel,hugetlb
|
||||||
|
│ │ ├─/sys/fs/cgroup/cpuset cgroup cgroup rw,nosuid,nodev,noexec,relatime,seclabel,cpuset
|
||||||
|
│ │ ├─/sys/fs/cgroup/cpu,cpuacct cgroup cgroup rw,nosuid,nodev,noexec,relatime,seclabel,cpuacct,cpu
|
||||||
|
│ │ ├─/sys/fs/cgroup/memory cgroup cgroup rw,nosuid,nodev,noexec,relatime,seclabel,memory
|
||||||
|
│ │ ├─/sys/fs/cgroup/perf_event cgroup cgroup rw,nosuid,nodev,noexec,relatime,seclabel,perf_event
|
||||||
|
│ │ ├─/sys/fs/cgroup/freezer cgroup cgroup rw,nosuid,nodev,noexec,relatime,seclabel,freezer
|
||||||
|
│ │ └─/sys/fs/cgroup/pids cgroup cgroup rw,nosuid,nodev,noexec,relatime,seclabel,pids
|
||||||
|
│ ├─/sys/fs/pstore pstore pstore rw,nosuid,nodev,noexec,relatime
|
||||||
|
│ ├─/sys/kernel/config configfs configfs rw,relatime
|
||||||
|
│ ├─/sys/fs/selinux selinuxfs selinuxfs rw,relatime
|
||||||
|
│ └─/sys/kernel/debug debugfs debugfs rw,relatime
|
||||||
|
├─/proc proc proc rw,nosuid,nodev,noexec,relatime
|
||||||
|
│ └─/proc/sys/fs/binfmt_misc systemd-1 autofs rw,relatime,fd=36,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=13995
|
||||||
|
├─/dev devtmpfs devtmpfs rw,nosuid,seclabel,size=1918816k,nr_inodes=479704,mode=755
|
||||||
|
│ ├─/dev/shm tmpfs tmpfs rw,nosuid,nodev,seclabel
|
||||||
|
│ ├─/dev/pts devpts devpts rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000
|
||||||
|
│ ├─/dev/mqueue mqueue mqueue rw,relatime,seclabel
|
||||||
|
│ └─/dev/hugepages hugetlbfs hugetlbfs rw,relatime,seclabel
|
||||||
|
├─/run tmpfs tmpfs rw,nosuid,nodev,seclabel,mode=755
|
||||||
|
│ ├─/run/user/0 tmpfs tmpfs rw,nosuid,nodev,relatime,seclabel,size=386136k,mode=700
|
||||||
|
│ └─/run/user/1000 tmpfs tmpfs rw,nosuid,nodev,relatime,seclabel,size=386136k,mode=700,uid=1000,gid=1000
|
||||||
|
├─/boot /dev/sda1 xfs rw,relatime,seclabel,attr2,inode64,noquota
|
||||||
|
├─/var/lib/docker/containers /dev/mapper/centos-root[/var/lib/docker/containers] xfs rw,relatime,seclabel,attr2,inode64,noquota
|
||||||
|
└─/var/lib/docker/overlay2 /dev/mapper/centos-root[/var/lib/docker/overlay2] xfs rw,relatime,seclabel,attr2,inode64,noquota
|
Reference in New Issue
Block a user