mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-17 01:32:37 +02:00
initial working yaml out
This commit is contained in:
63
jc/cli.py
63
jc/cli.py
@ -2,13 +2,13 @@
|
|||||||
JC cli module
|
JC cli module
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import io
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import textwrap
|
import textwrap
|
||||||
import signal
|
import signal
|
||||||
import shlex
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
import json
|
|
||||||
from .lib import (__version__, parser_info, all_parser_info, parsers,
|
from .lib import (__version__, parser_info, all_parser_info, parsers,
|
||||||
_get_parser, _parser_is_streaming)
|
_get_parser, _parser_is_streaming)
|
||||||
from . import utils
|
from . import utils
|
||||||
@ -27,7 +27,6 @@ try:
|
|||||||
except Exception:
|
except Exception:
|
||||||
PYGMENTS_INSTALLED = False
|
PYGMENTS_INSTALLED = False
|
||||||
|
|
||||||
|
|
||||||
JC_ERROR_EXIT = 100
|
JC_ERROR_EXIT = 100
|
||||||
|
|
||||||
|
|
||||||
@ -210,6 +209,7 @@ def helptext():
|
|||||||
-r raw JSON output
|
-r raw JSON output
|
||||||
-u unbuffer output
|
-u unbuffer output
|
||||||
-v version info
|
-v version info
|
||||||
|
-y YAML output
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
Standard Syntax:
|
Standard Syntax:
|
||||||
@ -263,11 +263,37 @@ def versiontext():
|
|||||||
return textwrap.dedent(versiontext_string)
|
return textwrap.dedent(versiontext_string)
|
||||||
|
|
||||||
|
|
||||||
|
def yaml_out(data, pretty=False, env_colors=None, mono=False, piped_out=False, ascii_only=False):
|
||||||
|
"""Return a YAML formatted string"""
|
||||||
|
# make ruamel.yaml import optional
|
||||||
|
try:
|
||||||
|
from ruamel.yaml import YAML
|
||||||
|
YAML_INSTALLED = True
|
||||||
|
except Exception:
|
||||||
|
YAML_INSTALLED = False
|
||||||
|
|
||||||
|
if YAML_INSTALLED:
|
||||||
|
y_string = io.BytesIO()
|
||||||
|
# monkey patch to disable plugins since we don't use them and in
|
||||||
|
# ruamel.yaml versions prior to 0.17.0 the use of __file__ in the
|
||||||
|
# plugin code is incompatible with the pyoxidizer packager
|
||||||
|
YAML.official_plug_ins = lambda a: []
|
||||||
|
yaml=YAML()
|
||||||
|
yaml.default_flow_style = False
|
||||||
|
yaml.dump(data, y_string)
|
||||||
|
return '---\n' + y_string.getvalue().decode('utf-8')[:-1]
|
||||||
|
|
||||||
|
utils.warning_message(['YAML Library not installed. Reverting to JSON output.'])
|
||||||
|
return json_out(data, pretty=pretty, env_colors=env_colors, mono=mono, piped_out=piped_out, ascii_only=ascii_only)
|
||||||
|
|
||||||
|
|
||||||
def json_out(data, pretty=False, env_colors=None, mono=False, piped_out=False, ascii_only=False):
|
def json_out(data, pretty=False, env_colors=None, mono=False, piped_out=False, ascii_only=False):
|
||||||
"""
|
"""
|
||||||
Return a JSON formatted string. String may include color codes or be
|
Return a JSON formatted string. String may include color codes or be
|
||||||
pretty printed.
|
pretty printed.
|
||||||
"""
|
"""
|
||||||
|
import json
|
||||||
|
|
||||||
separators = (',', ':')
|
separators = (',', ':')
|
||||||
indent = None
|
indent = None
|
||||||
|
|
||||||
@ -288,8 +314,17 @@ def json_out(data, pretty=False, env_colors=None, mono=False, piped_out=False, a
|
|||||||
|
|
||||||
|
|
||||||
def safe_print_json(list_or_dict, pretty=None, env_colors=None, mono=None,
|
def safe_print_json(list_or_dict, pretty=None, env_colors=None, mono=None,
|
||||||
piped_out=None, flush=None):
|
piped_out=None, flush=None, yaml=None):
|
||||||
"""Safely prints JSON output in both UTF-8 and ASCII systems"""
|
"""Safely prints JSON output in both UTF-8 and ASCII systems"""
|
||||||
|
if yaml:
|
||||||
|
print(yaml_out(list_or_dict,
|
||||||
|
pretty=pretty,
|
||||||
|
env_colors=env_colors,
|
||||||
|
mono=mono,
|
||||||
|
piped_out=piped_out),
|
||||||
|
flush=flush)
|
||||||
|
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
print(json_out(list_or_dict,
|
print(json_out(list_or_dict,
|
||||||
pretty=pretty,
|
pretty=pretty,
|
||||||
@ -435,6 +470,7 @@ def main():
|
|||||||
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
|
||||||
|
yaml_out = 'y' in options
|
||||||
|
|
||||||
if verbose_debug:
|
if verbose_debug:
|
||||||
tracebackplus.enable(context=11)
|
tracebackplus.enable(context=11)
|
||||||
@ -447,7 +483,8 @@ def main():
|
|||||||
pretty=pretty,
|
pretty=pretty,
|
||||||
env_colors=jc_colors,
|
env_colors=jc_colors,
|
||||||
mono=mono,
|
mono=mono,
|
||||||
piped_out=piped_output(force_color))
|
piped_out=piped_output(force_color),
|
||||||
|
yaml=yaml_out)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if help_me:
|
if help_me:
|
||||||
@ -539,7 +576,8 @@ def main():
|
|||||||
env_colors=jc_colors,
|
env_colors=jc_colors,
|
||||||
mono=mono,
|
mono=mono,
|
||||||
piped_out=piped_output(force_color),
|
piped_out=piped_output(force_color),
|
||||||
flush=unbuffer)
|
flush=unbuffer,
|
||||||
|
yaml=yaml_out)
|
||||||
|
|
||||||
sys.exit(combined_exit_code(magic_exit_code, 0))
|
sys.exit(combined_exit_code(magic_exit_code, 0))
|
||||||
|
|
||||||
@ -555,7 +593,8 @@ def main():
|
|||||||
env_colors=jc_colors,
|
env_colors=jc_colors,
|
||||||
mono=mono,
|
mono=mono,
|
||||||
piped_out=piped_output(force_color),
|
piped_out=piped_output(force_color),
|
||||||
flush=unbuffer)
|
flush=unbuffer,
|
||||||
|
yaml=yaml_out)
|
||||||
|
|
||||||
sys.exit(combined_exit_code(magic_exit_code, 0))
|
sys.exit(combined_exit_code(magic_exit_code, 0))
|
||||||
|
|
||||||
@ -570,13 +609,13 @@ def main():
|
|||||||
])
|
])
|
||||||
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
|
||||||
|
|
||||||
utils.error_message(['There was an issue generating the JSON output.',
|
# 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:
|
||||||
|
Reference in New Issue
Block a user