Skip to content

gh-133346: Make theming support in _colorize extensible #133347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Bring json.tool into the theming fold
  • Loading branch information
ambv committed May 5, 2025
commit 70ca1615327a7d0d74fd44465645a233269d3013
41 changes: 22 additions & 19 deletions Lib/json/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
import re
import sys
from _colorize import ANSIColors, can_colorize
from _colorize import get_theme, can_colorize


# The string we are colorizing is valid JSON,
Expand All @@ -17,27 +17,27 @@
_color_pattern = re.compile(r'''
(?P<key>"(\\.|[^"\\])*")(?=:) |
(?P<string>"(\\.|[^"\\])*") |
(?P<number>NaN|-?Infinity|[0-9\-+.Ee]+) |
(?P<boolean>true|false) |
(?P<null>null)
''', re.VERBOSE)


_colors = {
'key': ANSIColors.INTENSE_BLUE,
'string': ANSIColors.BOLD_GREEN,
'boolean': ANSIColors.BOLD_CYAN,
'null': ANSIColors.BOLD_CYAN,
_group_to_theme_color = {
"key": "definition",
"string": "string",
"number": "number",
"boolean": "keyword",
"null": "keyword",
}


def _replace_match_callback(match):
for key, color in _colors.items():
if m := match.group(key):
return f"{color}{m}{ANSIColors.RESET}"
return match.group()

def _colorize_json(json_str, theme):
def _replace_match_callback(match):
for group, color in _group_to_theme_color.items():
if m := match.group(group):
return f"{theme[color]}{m}{theme.reset}"
return match.group()

def _colorize_json(json_str):
return re.sub(_color_pattern, _replace_match_callback, json_str)


Expand Down Expand Up @@ -100,13 +100,16 @@ def main():
else:
outfile = open(options.outfile, 'w', encoding='utf-8')
with outfile:
for obj in objs:
if can_colorize(file=outfile):
if can_colorize(file=outfile):
t = get_theme(tty_file=outfile).syntax
for obj in objs:
json_str = json.dumps(obj, **dump_args)
outfile.write(_colorize_json(json_str))
else:
outfile.write(_colorize_json(json_str, t))
outfile.write('\n')
else:
for obj in objs:
json.dump(obj, outfile, **dump_args)
outfile.write('\n')
outfile.write('\n')
except ValueError as e:
raise SystemExit(e)

Expand Down
85 changes: 47 additions & 38 deletions Lib/test/test_json/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import subprocess

from test import support
from test.support import force_not_colorized, os_helper
from test.support import force_colorized, force_not_colorized, os_helper
from test.support.script_helper import assert_python_ok

from _colorize import get_theme


@support.requires_subprocess()
class TestMain(unittest.TestCase):
Expand Down Expand Up @@ -246,65 +248,72 @@ def test_broken_pipe_error(self):
proc.communicate(b'"{}"')
self.assertEqual(proc.returncode, errno.EPIPE)

@force_colorized
def test_colors(self):
infile = os_helper.TESTFN
self.addCleanup(os.remove, infile)

t = get_theme().syntax
ob = "{"
cb = "}"

cases = (
('{}', b'{}'),
('[]', b'[]'),
('null', b'\x1b[1;36mnull\x1b[0m'),
('true', b'\x1b[1;36mtrue\x1b[0m'),
('false', b'\x1b[1;36mfalse\x1b[0m'),
('NaN', b'NaN'),
('Infinity', b'Infinity'),
('-Infinity', b'-Infinity'),
('"foo"', b'\x1b[1;32m"foo"\x1b[0m'),
(r'" \"foo\" "', b'\x1b[1;32m" \\"foo\\" "\x1b[0m'),
('"α"', b'\x1b[1;32m"\\u03b1"\x1b[0m'),
('123', b'123'),
('-1.2345e+23', b'-1.2345e+23'),
('{}', '{}'),
('[]', '[]'),
('null', f'{t.keyword}null{t.reset}'),
('true', f'{t.keyword}true{t.reset}'),
('false', f'{t.keyword}false{t.reset}'),
('NaN', f'{t.number}NaN{t.reset}'),
('Infinity', f'{t.number}Infinity{t.reset}'),
('-Infinity', f'{t.number}-Infinity{t.reset}'),
('"foo"', f'{t.string}"foo"{t.reset}'),
(r'" \"foo\" "', f'{t.string}" \\"foo\\" "{t.reset}'),
('"α"', f'{t.string}"\\u03b1"{t.reset}'),
('123', f'{t.number}123{t.reset}'),
('-1.2345e+23', f'{t.number}-1.2345e+23{t.reset}'),
(r'{"\\": ""}',
b'''\
{
\x1b[94m"\\\\"\x1b[0m: \x1b[1;32m""\x1b[0m
}'''),
f'''\
{ob}
{t.definition}"\\\\"{t.reset}: {t.string}""{t.reset}
{cb}'''),
(r'{"\\\\": ""}',
b'''\
{
\x1b[94m"\\\\\\\\"\x1b[0m: \x1b[1;32m""\x1b[0m
}'''),
f'''\
{ob}
{t.definition}"\\\\\\\\"{t.reset}: {t.string}""{t.reset}
{cb}'''),
('''\
{
"foo": "bar",
"baz": 1234,
"qux": [true, false, null],
"xyz": [NaN, -Infinity, Infinity]
}''',
b'''\
{
\x1b[94m"foo"\x1b[0m: \x1b[1;32m"bar"\x1b[0m,
\x1b[94m"baz"\x1b[0m: 1234,
\x1b[94m"qux"\x1b[0m: [
\x1b[1;36mtrue\x1b[0m,
\x1b[1;36mfalse\x1b[0m,
\x1b[1;36mnull\x1b[0m
f'''\
{ob}
{t.definition}"foo"{t.reset}: {t.string}"bar"{t.reset},
{t.definition}"baz"{t.reset}: {t.number}1234{t.reset},
{t.definition}"qux"{t.reset}: [
{t.keyword}true{t.reset},
{t.keyword}false{t.reset},
{t.keyword}null{t.reset}
],
\x1b[94m"xyz"\x1b[0m: [
NaN,
-Infinity,
Infinity
{t.definition}"xyz"{t.reset}: [
{t.number}NaN{t.reset},
{t.number}-Infinity{t.reset},
{t.number}Infinity{t.reset}
]
}'''),
{cb}'''),
)

for input_, expected in cases:
with self.subTest(input=input_):
with open(infile, "w", encoding="utf-8") as fp:
fp.write(input_)
_, stdout, _ = assert_python_ok('-m', self.module, infile,
PYTHON_COLORS='1')
stdout = stdout.replace(b'\r\n', b'\n') # normalize line endings
_, stdout_b, _ = assert_python_ok(
'-m', self.module, infile, FORCE_COLOR='1', __isolated='1'
)
stdout = stdout_b.decode()
stdout = stdout.replace('\r\n', '\n') # normalize line endings
stdout = stdout.strip()
self.assertEqual(stdout, expected)

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy