1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-19 00:17:51 +02:00
This commit is contained in:
Kelly Brazil
2021-09-24 08:43:09 -07:00
parent 7a4ebcd1ec
commit 22e151b01c
2 changed files with 55 additions and 59 deletions

View File

@ -13,7 +13,7 @@ import shlex
import subprocess import subprocess
import json import json
import jc import jc
import jc.appdirs as appdirs from jc import appdirs
import jc.utils import jc.utils
import jc.tracebackplus import jc.tracebackplus
from jc.exceptions import LibraryNotInstalled, ParseError from jc.exceptions import LibraryNotInstalled, ParseError
@ -26,9 +26,9 @@ try:
from pygments.token import (Name, Number, String, Keyword) from pygments.token import (Name, Number, String, Keyword)
from pygments.lexers import JsonLexer from pygments.lexers import JsonLexer
from pygments.formatters import Terminal256Formatter from pygments.formatters import Terminal256Formatter
pygments_installed = True PYGMENTS_INSTALLED = True
except Exception: except Exception:
pygments_installed = False PYGMENTS_INSTALLED = False
class info(): class info():
@ -144,7 +144,7 @@ if os.path.isdir(local_parsers_dir):
# We only support 2.3.0+, pygments changed color names in 2.4.0. # We only support 2.3.0+, pygments changed color names in 2.4.0.
# startswith is sufficient and avoids potential exceptions from split and int. # startswith is sufficient and avoids potential exceptions from split and int.
if pygments_installed: if PYGMENTS_INSTALLED:
if pygments.__version__.startswith('2.3.'): if pygments.__version__.startswith('2.3.'):
PYGMENT_COLOR = { PYGMENT_COLOR = {
'black': '#ansiblack', 'black': '#ansiblack',
@ -233,7 +233,7 @@ def set_env_colors(env_colors=None):
def piped_output(): def piped_output():
"""Return False if stdout is a TTY. True if output is being piped to another program""" """Return False if stdout is a TTY. True if output is being piped to another program"""
return False if sys.stdout.isatty() else True return not sys.stdout.isatty()
def ctrlc(signum, frame): def ctrlc(signum, frame):
@ -241,9 +241,9 @@ def ctrlc(signum, frame):
sys.exit(JC_ERROR_EXIT) sys.exit(JC_ERROR_EXIT)
def parser_shortname(parser_argument): def parser_shortname(parser_arg):
"""Return short name of the parser with dashes and no -- prefix""" """Return short name of the parser with dashes and no -- prefix"""
return parser_argument[2:] return parser_arg[2:]
def parser_argument(parser): def parser_argument(parser):
@ -401,8 +401,7 @@ def json_out(data, pretty=False, env_colors=None, mono=False, piped_out=False):
return str(highlight(json.dumps(data, indent=indent, separators=separators, ensure_ascii=False), return str(highlight(json.dumps(data, indent=indent, separators=separators, ensure_ascii=False),
JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1]) JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
else: return json.dumps(data, indent=indent, separators=separators, ensure_ascii=False)
return json.dumps(data, indent=indent, separators=separators, ensure_ascii=False)
def magic_parser(args): def magic_parser(args):
@ -429,7 +428,7 @@ def magic_parser(args):
return False, None, None, [] return False, None, None, []
# option found - populate option list # option found - populate option list
elif arg.startswith('-'): if arg.startswith('-'):
options.extend(args_given.pop(0)[1:]) options.extend(args_given.pop(0)[1:])
# command found if iterator didn't already stop - stop iterating # command found if iterator didn't already stop - stop iterating
@ -460,7 +459,7 @@ def magic_parser(args):
found_parser = magic_dict.get(two_word_command, magic_dict.get(one_word_command)) found_parser = magic_dict.get(two_word_command, magic_dict.get(one_word_command))
return ( return (
True if found_parser else False, # was a suitable parser found? bool(found_parser), # was a suitable parser found?
args_given, # run_command args_given, # run_command
found_parser, # the parser selected found_parser, # the parser selected
options # jc options to preserve options # jc options to preserve
@ -485,8 +484,7 @@ def run_user_command(command):
def combined_exit_code(program_exit=0, jc_exit=0): def combined_exit_code(program_exit=0, jc_exit=0):
exit_code = program_exit + jc_exit exit_code = program_exit + jc_exit
if exit_code > 255: exit_code = min(exit_code, 255)
exit_code = 255
return exit_code return exit_code
@ -523,12 +521,12 @@ def main():
about = 'a' in options about = 'a' in options
debug = 'd' in options debug = 'd' in options
verbose_debug = True if options.count('d') > 1 else False verbose_debug = options.count('d') > 1
mono = 'm' in options mono = 'm' in options
help_me = 'h' in options help_me = 'h' in options
pretty = 'p' in options pretty = 'p' in options
quiet = 'q' in options quiet = 'q' in options
ignore_exceptions = True if options.count('q') > 1 else False ignore_exceptions = options.count('q') > 1
raw = 'r' in options raw = 'r' in options
unbuffer = 'u' in options unbuffer = 'u' in options
version_info = 'v' in options version_info = 'v' in options
@ -536,7 +534,7 @@ def main():
if verbose_debug: if verbose_debug:
jc.tracebackplus.enable(context=11) jc.tracebackplus.enable(context=11)
if not pygments_installed: if not PYGMENTS_INSTALLED:
mono = True mono = True
if about: if about:
@ -568,23 +566,23 @@ def main():
except FileNotFoundError: except FileNotFoundError:
if debug: if debug:
raise raise
else:
jc.utils.error_message([f'"{run_command_str}" command could not be found. For details use the -d or -dd option.']) jc.utils.error_message([f'"{run_command_str}" command could not be found. For details use the -d or -dd option.'])
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT)) sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
except OSError: except OSError:
if debug: if debug:
raise raise
else:
jc.utils.error_message([f'"{run_command_str}" command could not be run due to too many open files. For details use the -d or -dd option.']) jc.utils.error_message([f'"{run_command_str}" command could not be run due to too many open files. For details use the -d or -dd option.'])
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT)) sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
except Exception: except Exception:
if debug: if debug:
raise raise
else:
jc.utils.error_message([f'"{run_command_str}" command could not be run. For details use the -d or -dd option.']) jc.utils.error_message([f'"{run_command_str}" command could not be run. For details use the -d or -dd option.'])
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT)) sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
elif run_command is not None: elif run_command is not None:
jc.utils.error_message([f'"{run_command_str}" cannot be used with Magic syntax. Use "jc -h" for help.']) jc.utils.error_message([f'"{run_command_str}" cannot be used with Magic syntax. Use "jc -h" for help.'])
@ -651,34 +649,34 @@ def main():
except (ParseError, LibraryNotInstalled) as e: except (ParseError, LibraryNotInstalled) as e:
if debug: if debug:
raise raise
else:
jc.utils.error_message([f'Parser issue with {parser_name}:', jc.utils.error_message([f'Parser issue with {parser_name}:',
f'{e.__class__.__name__}: {e}', f'{e.__class__.__name__}: {e}',
'For details use the -d or -dd option. Use "jc -h" for help.']) 'For details use the -d or -dd option. Use "jc -h" for help.'])
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT)) sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
except json.JSONDecodeError: except json.JSONDecodeError:
if debug: if debug:
raise raise
else:
jc.utils.error_message(['There was an issue generating the JSON output.', jc.utils.error_message(['There was an issue generating the JSON output.',
'For details use the -d or -dd option.']) 'For details use the -d or -dd option.'])
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT)) sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
except Exception: except Exception:
if debug: if debug:
raise raise
else:
streaming_msg = ''
if getattr(parser.info, 'streaming', None):
streaming_msg = 'Use the -qq option to ignore streaming parser errors.'
jc.utils.error_message([ streaming_msg = ''
f'{parser_name} parser could not parse the input data. Did you use the correct parser?', if getattr(parser.info, 'streaming', None):
f'{streaming_msg}', streaming_msg = 'Use the -qq option to ignore streaming parser errors.'
'For details use the -d or -dd option. Use "jc -h" for help.'
]) jc.utils.error_message([
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT)) f'{parser_name} parser could not parse the input data. Did you use the correct parser?',
f'{streaming_msg}',
'For details use the -d or -dd option. Use "jc -h" for help.'
])
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -116,7 +116,7 @@ def has_data(data):
Boolean True if input string (data) contains non-whitespace characters, otherwise False Boolean True if input string (data) contains non-whitespace characters, otherwise False
""" """
return True if data and not data.isspace() else False return bool(data and not data.isspace())
def convert_to_int(value): def convert_to_int(value):
@ -204,7 +204,7 @@ def convert_to_bool(value):
pass pass
if value: if value:
return True if value.lower() in truthy else False return value.lower() in truthy
return False return False
@ -224,15 +224,15 @@ def stream_error(e, ignore_exceptions, line):
if not ignore_exceptions: if not ignore_exceptions:
e.args = (str(e) + '... Use the ignore_exceptions option (-qq) to ignore streaming parser errors.',) e.args = (str(e) + '... Use the ignore_exceptions option (-qq) to ignore streaming parser errors.',)
raise e raise e
else:
return { return {
'_jc_meta': '_jc_meta':
{ {
'success': False, 'success': False,
'error': f'{e.__class__.__name__}: {e}', 'error': f'{e.__class__.__name__}: {e}',
'line': line.strip() 'line': line.strip()
} }
} }
class timestamp: class timestamp:
@ -307,10 +307,8 @@ class timestamp:
if 'UTC' in data: if 'UTC' in data:
utc_tz = True utc_tz = True
if 'UTC+' in data or 'UTC-' in data: if 'UTC+' in data or 'UTC-' in data:
if 'UTC+0000' in data or 'UTC-0000' in data: utc_tz = bool('UTC+0000' in data or 'UTC-0000' in data)
utc_tz = True
else:
utc_tz = False
elif '+0000' in data or '-0000' in data: elif '+0000' in data or '-0000' in data:
utc_tz = True utc_tz = True