mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-21 00:19:42 +02:00
make /proc slurpable with magic syntax
This commit is contained in:
47
jc/cli.py
47
jc/cli.py
@ -122,7 +122,7 @@ class JcCli():
|
|||||||
self.magic_options: List[str] = []
|
self.magic_options: List[str] = []
|
||||||
self.magic_run_command: Optional[List[str]] = None
|
self.magic_run_command: Optional[List[str]] = None
|
||||||
self.magic_run_command_str: str = ''
|
self.magic_run_command_str: str = ''
|
||||||
self.magic_stdout: Optional[str] = None
|
self.magic_stdout: Optional[Union[str, Iterable[str]]] = None
|
||||||
self.magic_stderr: Optional[str] = None
|
self.magic_stderr: Optional[str] = None
|
||||||
self.magic_returncode: int = 0
|
self.magic_returncode: int = 0
|
||||||
|
|
||||||
@ -194,7 +194,7 @@ class JcCli():
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def parser_shortname(parser_arg: str) -> str:
|
def parser_shortname(parser_arg: str) -> str:
|
||||||
"""Return short name of the parser with dashes and no -- prefix"""
|
"""Return short name of the parser with dashes and no -- prefix"""
|
||||||
return parser_arg[2:]
|
return parser_arg.lstrip('-')
|
||||||
|
|
||||||
def parsers_text(self) -> str:
|
def parsers_text(self) -> str:
|
||||||
"""Return the argument and description information from each parser"""
|
"""Return the argument and description information from each parser"""
|
||||||
@ -528,11 +528,32 @@ class JcCli():
|
|||||||
|
|
||||||
Supports running magic commands or opening /proc files to set the
|
Supports running magic commands or opening /proc files to set the
|
||||||
output to magic_stdout.
|
output to magic_stdout.
|
||||||
|
|
||||||
|
If multiple /proc files are detected, then a list of string output
|
||||||
|
is sento to magic_stdout. Since the proc parser is slurpable, it will
|
||||||
|
take the list of strings and parse the data into an array.
|
||||||
"""
|
"""
|
||||||
if self.magic_run_command_str.startswith('/proc'):
|
if self.magic_run_command_str.startswith('/proc'):
|
||||||
try:
|
try:
|
||||||
self.magic_found_parser = 'proc'
|
self.magic_found_parser = 'proc'
|
||||||
self.magic_stdout = self.open_text_file(self.magic_run_command_str)
|
|
||||||
|
# multiple proc files detected
|
||||||
|
if ' ' in self.magic_run_command_str:
|
||||||
|
self.slurp = True
|
||||||
|
multi_out: List[str] = []
|
||||||
|
proc_files = self.magic_run_command_str.split()
|
||||||
|
|
||||||
|
for file in proc_files:
|
||||||
|
# multi_out.append(self.open_text_file('/Users/kelly/temp/' + file))
|
||||||
|
multi_out.append(self.open_text_file(file))
|
||||||
|
|
||||||
|
self.magic_stdout = multi_out
|
||||||
|
|
||||||
|
# single proc file
|
||||||
|
else:
|
||||||
|
file = self.magic_run_command_str
|
||||||
|
# self.magic_stdout = self.open_text_file('/Users/kelly/temp/' + file)
|
||||||
|
self.magic_stdout = self.open_text_file(file)
|
||||||
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
if self.debug:
|
if self.debug:
|
||||||
@ -540,7 +561,7 @@ class JcCli():
|
|||||||
|
|
||||||
error_msg = os.strerror(e.errno)
|
error_msg = os.strerror(e.errno)
|
||||||
utils.error_message([
|
utils.error_message([
|
||||||
f'"{self.magic_run_command_str}" file could not be opened: {error_msg}.'
|
f'"{file}" file could not be opened: {error_msg}.'
|
||||||
])
|
])
|
||||||
self.exit_error()
|
self.exit_error()
|
||||||
|
|
||||||
@ -549,7 +570,7 @@ class JcCli():
|
|||||||
raise
|
raise
|
||||||
|
|
||||||
utils.error_message([
|
utils.error_message([
|
||||||
f'"{self.magic_run_command_str}" file could not be opened. For details use the -d or -dd option.'
|
f'"{file}" file could not be opened. For details use the -d or -dd option.'
|
||||||
])
|
])
|
||||||
self.exit_error()
|
self.exit_error()
|
||||||
|
|
||||||
@ -661,10 +682,20 @@ class JcCli():
|
|||||||
self.data_in = utils.line_slice(self.data_in, self.slice_start, self.slice_end)
|
self.data_in = utils.line_slice(self.data_in, self.slice_start, self.slice_end)
|
||||||
|
|
||||||
def create_slurp_output(self) -> None:
|
def create_slurp_output(self) -> None:
|
||||||
"""Slurp output into an array. Only works for single-line strings."""
|
"""
|
||||||
if self.parser_module and isinstance(self.data_in, str):
|
Slurp output into an array. Only works for single-line strings or
|
||||||
|
multiple files coming from the /proc magic syntax.
|
||||||
|
"""
|
||||||
|
if self.parser_module and isinstance(self.data_in, (str, Iterable)):
|
||||||
|
items = []
|
||||||
|
|
||||||
|
if isinstance(self.data_in, str):
|
||||||
|
items = self.data_in.splitlines()
|
||||||
|
elif isinstance(self.data_in, List):
|
||||||
|
items = self.data_in
|
||||||
|
|
||||||
self.data_out = []
|
self.data_out = []
|
||||||
for line in self.data_in.splitlines():
|
for line in items:
|
||||||
parsed_line = self.parser_module.parse(
|
parsed_line = self.parser_module.parse(
|
||||||
line,
|
line,
|
||||||
raw=self.raw,
|
raw=self.raw,
|
||||||
|
@ -120,12 +120,12 @@ from jc.exceptions import ParseError
|
|||||||
|
|
||||||
class info():
|
class info():
|
||||||
"""Provides parser metadata (version, author, etc.)"""
|
"""Provides parser metadata (version, author, etc.)"""
|
||||||
version = '1.2'
|
version = '1.3'
|
||||||
description = '`/proc/` file parser'
|
description = '`/proc/` file parser'
|
||||||
author = 'Kelly Brazil'
|
author = 'Kelly Brazil'
|
||||||
author_email = 'kellyjonbrazil@gmail.com'
|
author_email = 'kellyjonbrazil@gmail.com'
|
||||||
compatible = ['linux']
|
compatible = ['linux']
|
||||||
tags = ['file']
|
tags = ['file', 'slurpable']
|
||||||
|
|
||||||
|
|
||||||
__version__ = info.version
|
__version__ = info.version
|
||||||
|
Reference in New Issue
Block a user