mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-15 01:24:29 +02:00
create safe_print_json function
This commit is contained in:
80
jc/cli.py
80
jc/cli.py
@ -84,6 +84,26 @@ if PYGMENTS_INSTALLED:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def safe_print_json(string, pretty=None, env_colors=None, mono=None,
|
||||||
|
piped_out=None, flush=None):
|
||||||
|
"""Safely prints JSON output in both UTF-8 and ASCII systems"""
|
||||||
|
try:
|
||||||
|
print(json_out(string,
|
||||||
|
pretty=pretty,
|
||||||
|
env_colors=env_colors,
|
||||||
|
mono=mono,
|
||||||
|
piped_out=piped_out),
|
||||||
|
flush=flush)
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
print(json_out(string,
|
||||||
|
pretty=pretty,
|
||||||
|
env_colors=env_colors,
|
||||||
|
mono=mono,
|
||||||
|
piped_out=piped_out,
|
||||||
|
ascii_only=True),
|
||||||
|
flush=flush)
|
||||||
|
|
||||||
|
|
||||||
def set_env_colors(env_colors=None):
|
def set_env_colors(env_colors=None):
|
||||||
"""
|
"""
|
||||||
Return a dictionary to be used in Pygments custom style class.
|
Return a dictionary to be used in Pygments custom style class.
|
||||||
@ -423,19 +443,11 @@ def main():
|
|||||||
mono = True
|
mono = True
|
||||||
|
|
||||||
if about:
|
if about:
|
||||||
try:
|
safe_print_json(about_jc(),
|
||||||
print(json_out(about_jc(),
|
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)))
|
|
||||||
except UnicodeEncodeError:
|
|
||||||
print(json_out(about_jc(),
|
|
||||||
pretty=pretty,
|
|
||||||
env_colors=jc_colors,
|
|
||||||
mono=mono,
|
|
||||||
piped_out=piped_output(force_color),
|
|
||||||
ascii_only=True))
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if help_me:
|
if help_me:
|
||||||
@ -522,21 +534,12 @@ def main():
|
|||||||
quiet=quiet,
|
quiet=quiet,
|
||||||
ignore_exceptions=ignore_exceptions)
|
ignore_exceptions=ignore_exceptions)
|
||||||
for line in result:
|
for line in result:
|
||||||
try:
|
safe_print_json(line,
|
||||||
print(json_out(line,
|
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)),
|
flush=unbuffer)
|
||||||
flush=unbuffer)
|
|
||||||
except UnicodeEncodeError:
|
|
||||||
print(json_out(line,
|
|
||||||
pretty=pretty,
|
|
||||||
env_colors=jc_colors,
|
|
||||||
mono=mono,
|
|
||||||
piped_out=piped_output(force_color),
|
|
||||||
ascii_only=True),
|
|
||||||
flush=unbuffer)
|
|
||||||
|
|
||||||
sys.exit(combined_exit_code(magic_exit_code, 0))
|
sys.exit(combined_exit_code(magic_exit_code, 0))
|
||||||
|
|
||||||
@ -547,21 +550,12 @@ def main():
|
|||||||
raw=raw,
|
raw=raw,
|
||||||
quiet=quiet)
|
quiet=quiet)
|
||||||
|
|
||||||
try:
|
safe_print_json(result,
|
||||||
print(json_out(result,
|
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)),
|
flush=unbuffer)
|
||||||
flush=unbuffer)
|
|
||||||
except UnicodeEncodeError:
|
|
||||||
print(json_out(result,
|
|
||||||
pretty=pretty,
|
|
||||||
env_colors=jc_colors,
|
|
||||||
mono=mono,
|
|
||||||
piped_out=piped_output(force_color),
|
|
||||||
ascii_only=True),
|
|
||||||
flush=unbuffer)
|
|
||||||
|
|
||||||
sys.exit(combined_exit_code(magic_exit_code, 0))
|
sys.exit(combined_exit_code(magic_exit_code, 0))
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user