1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-15 01:24:29 +02:00

initial working yaml out

This commit is contained in:
Kelly Brazil
2022-05-20 14:51:23 -07:00
parent 8a122cd9e1
commit 5b9fcd5852

View File

@ -2,13 +2,13 @@
JC cli module
"""
import io
import sys
import os
import textwrap
import signal
import shlex
import subprocess
import json
from .lib import (__version__, parser_info, all_parser_info, parsers,
_get_parser, _parser_is_streaming)
from . import utils
@ -27,7 +27,6 @@ try:
except Exception:
PYGMENTS_INSTALLED = False
JC_ERROR_EXIT = 100
@ -210,6 +209,7 @@ def helptext():
-r raw JSON output
-u unbuffer output
-v version info
-y YAML output
Examples:
Standard Syntax:
@ -263,11 +263,37 @@ def versiontext():
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):
"""
Return a JSON formatted string. String may include color codes or be
pretty printed.
"""
import json
separators = (',', ':')
indent = None
@ -288,23 +314,32 @@ 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,
piped_out=None, flush=None):
piped_out=None, flush=None, yaml=None):
"""Safely prints JSON output in both UTF-8 and ASCII systems"""
try:
print(json_out(list_or_dict,
if yaml:
print(yaml_out(list_or_dict,
pretty=pretty,
env_colors=env_colors,
mono=mono,
piped_out=piped_out),
flush=flush)
except UnicodeEncodeError:
print(json_out(list_or_dict,
pretty=pretty,
env_colors=env_colors,
mono=mono,
piped_out=piped_out,
ascii_only=True),
flush=flush)
else:
try:
print(json_out(list_or_dict,
pretty=pretty,
env_colors=env_colors,
mono=mono,
piped_out=piped_out),
flush=flush)
except UnicodeEncodeError:
print(json_out(list_or_dict,
pretty=pretty,
env_colors=env_colors,
mono=mono,
piped_out=piped_out,
ascii_only=True),
flush=flush)
def magic_parser(args):
@ -435,6 +470,7 @@ def main():
raw = 'r' in options
unbuffer = 'u' in options
version_info = 'v' in options
yaml_out = 'y' in options
if verbose_debug:
tracebackplus.enable(context=11)
@ -447,7 +483,8 @@ def main():
pretty=pretty,
env_colors=jc_colors,
mono=mono,
piped_out=piped_output(force_color))
piped_out=piped_output(force_color),
yaml=yaml_out)
sys.exit(0)
if help_me:
@ -539,7 +576,8 @@ def main():
env_colors=jc_colors,
mono=mono,
piped_out=piped_output(force_color),
flush=unbuffer)
flush=unbuffer,
yaml=yaml_out)
sys.exit(combined_exit_code(magic_exit_code, 0))
@ -555,7 +593,8 @@ def main():
env_colors=jc_colors,
mono=mono,
piped_out=piped_output(force_color),
flush=unbuffer)
flush=unbuffer,
yaml=yaml_out)
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))
except json.JSONDecodeError:
if debug:
raise
# except json.JSONDecodeError:
# if debug:
# raise
utils.error_message(['There was an issue generating the JSON output.',
'For details use the -d or -dd option.'])
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
# utils.error_message(['There was an issue generating the JSON output.',
# 'For details use the -d or -dd option.'])
# sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
except Exception:
if debug: