mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-13 01:20:24 +02:00
for for UnicodeEncodeError exception when printing JSON
This commit is contained in:
@ -1,7 +1,8 @@
|
|||||||
jc changelog
|
jc changelog
|
||||||
|
|
||||||
20220425 v1.18.8
|
20220426 v1.18.8
|
||||||
- Fix quotation marks around asciified output with UnicodeEncodeError
|
- Fix UnicodeEncodeError on some systems where LANG=C is set and unicode
|
||||||
|
characters are in the output
|
||||||
|
|
||||||
20220425 v1.18.7
|
20220425 v1.18.7
|
||||||
- Add git log command parser
|
- Add git log command parser
|
||||||
@ -9,8 +10,6 @@ jc changelog
|
|||||||
- Add update-alternatives --get-selections parser
|
- Add update-alternatives --get-selections parser
|
||||||
- Fix key/value and ini parsers to allow duplicate keys
|
- Fix key/value and ini parsers to allow duplicate keys
|
||||||
- Fix yaml file parser for files including timestamp objects
|
- Fix yaml file parser for files including timestamp objects
|
||||||
- Fix UnicodeEncodeError on some systems where LANG=C is set and unicode
|
|
||||||
characters are in the output
|
|
||||||
- Update xrandr parser: add a 'rotation' field
|
- Update xrandr parser: add a 'rotation' field
|
||||||
- Fix failing tests by moving template files
|
- Fix failing tests by moving template files
|
||||||
- Add python interpreter version and path to -v and -a output
|
- Add python interpreter version and path to -v and -a output
|
||||||
|
90
jc/cli.py
90
jc/cli.py
@ -274,7 +274,7 @@ def versiontext():
|
|||||||
return textwrap.dedent(versiontext_string)
|
return textwrap.dedent(versiontext_string)
|
||||||
|
|
||||||
|
|
||||||
def json_out(data, pretty=False, env_colors=None, mono=False, piped_out=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.
|
||||||
@ -291,23 +291,13 @@ def json_out(data, pretty=False, env_colors=None, mono=False, piped_out=False):
|
|||||||
class JcStyle(Style):
|
class JcStyle(Style):
|
||||||
styles = set_env_colors(env_colors)
|
styles = set_env_colors(env_colors)
|
||||||
|
|
||||||
try:
|
return str(highlight(json.dumps(data,
|
||||||
return str(highlight(json.dumps(data,
|
indent=indent,
|
||||||
indent=indent,
|
separators=separators,
|
||||||
separators=separators,
|
ensure_ascii=ascii_only),
|
||||||
ensure_ascii=False),
|
JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
|
||||||
JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
|
|
||||||
except UnicodeEncodeError:
|
|
||||||
return str(highlight(json.dumps(data,
|
|
||||||
indent=indent,
|
|
||||||
separators=separators,
|
|
||||||
ensure_ascii=True),
|
|
||||||
JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
|
|
||||||
|
|
||||||
try:
|
return json.dumps(data, indent=indent, separators=separators, ensure_ascii=ascii_only)
|
||||||
return json.dumps(data, indent=indent, separators=separators, ensure_ascii=False)
|
|
||||||
except UnicodeEncodeError:
|
|
||||||
return json.dumps(data, indent=indent, separators=separators, ensure_ascii=True)
|
|
||||||
|
|
||||||
|
|
||||||
def magic_parser(args):
|
def magic_parser(args):
|
||||||
@ -445,11 +435,19 @@ def main():
|
|||||||
mono = True
|
mono = True
|
||||||
|
|
||||||
if about:
|
if about:
|
||||||
print(json_out(about_jc(),
|
try:
|
||||||
pretty=pretty,
|
print(json_out(about_jc(),
|
||||||
env_colors=jc_colors,
|
pretty=pretty,
|
||||||
mono=mono,
|
env_colors=jc_colors,
|
||||||
piped_out=piped_output(force_color)))
|
mono=mono,
|
||||||
|
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:
|
||||||
@ -478,7 +476,10 @@ def main():
|
|||||||
try:
|
try:
|
||||||
magic_stdout, magic_stderr, magic_exit_code = run_user_command(run_command)
|
magic_stdout, magic_stderr, magic_exit_code = run_user_command(run_command)
|
||||||
if magic_stderr:
|
if magic_stderr:
|
||||||
print(magic_stderr[:-1], file=sys.stderr)
|
try:
|
||||||
|
print(magic_stderr[:-1], file=sys.stderr)
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
print(asciify(magic_stderr[:-1], file=sys.stderr))
|
||||||
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
if debug:
|
if debug:
|
||||||
@ -542,12 +543,21 @@ def main():
|
|||||||
quiet=quiet,
|
quiet=quiet,
|
||||||
ignore_exceptions=ignore_exceptions)
|
ignore_exceptions=ignore_exceptions)
|
||||||
for line in result:
|
for line in result:
|
||||||
print(json_out(line,
|
try:
|
||||||
pretty=pretty,
|
print(json_out(line,
|
||||||
env_colors=jc_colors,
|
pretty=pretty,
|
||||||
mono=mono,
|
env_colors=jc_colors,
|
||||||
piped_out=piped_output(force_color)),
|
mono=mono,
|
||||||
flush=unbuffer)
|
piped_out=piped_output(force_color)),
|
||||||
|
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))
|
||||||
|
|
||||||
@ -557,12 +567,22 @@ def main():
|
|||||||
result = parser.parse(data,
|
result = parser.parse(data,
|
||||||
raw=raw,
|
raw=raw,
|
||||||
quiet=quiet)
|
quiet=quiet)
|
||||||
print(json_out(result,
|
|
||||||
pretty=pretty,
|
try:
|
||||||
env_colors=jc_colors,
|
print(json_out(result,
|
||||||
mono=mono,
|
pretty=pretty,
|
||||||
piped_out=piped_output(force_color)),
|
env_colors=jc_colors,
|
||||||
flush=unbuffer)
|
mono=mono,
|
||||||
|
piped_out=piped_output(force_color)),
|
||||||
|
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