Skip to content

Commit 4036022

Browse files
authored
Merge pull request #1858 from tthijm/feat/bowtie-info-pretty
feat: improve bowtie info pretty output
2 parents 8ff9dd3 + d21f183 commit 4036022

File tree

4 files changed

+143
-58
lines changed

4 files changed

+143
-58
lines changed

bowtie/_cli.py

+59-5
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,62 @@ async def write(response: Awaitable[Response]):
16601660
asyncio.run(write(dialect.latest_report()))
16611661

16621662

1663+
def _info_links_table_for(metadata: dict[str, Any]):
1664+
table = Table(
1665+
Column(style="spring_green4"),
1666+
box=None,
1667+
padding=(0, 1, 0, 0),
1668+
show_header=False,
1669+
)
1670+
1671+
table.add_row("homepage", metadata["homepage"])
1672+
table.add_row("source", metadata["source"])
1673+
table.add_row("issues", metadata["issues"])
1674+
1675+
if "documentation" in metadata:
1676+
table.add_row("documentation", metadata["documentation"])
1677+
1678+
for link in metadata.get("links", []):
1679+
table.add_row(link["description"], link["url"])
1680+
1681+
return table
1682+
1683+
1684+
def _info_table_for(metadata: dict[str, Any]):
1685+
table = Table(
1686+
Column(style="cyan bold"),
1687+
box=box.ROUNDED,
1688+
show_header=False,
1689+
border_style="bright_black",
1690+
)
1691+
1692+
table.add_row(
1693+
"implementation",
1694+
f"{metadata["name"]} [grey58]{metadata.get("version", "")}[/grey58]",
1695+
)
1696+
table.add_row(
1697+
"language",
1698+
f"{metadata["language"]} [grey58]{metadata.get("language_version", "")}[/grey58]", # noqa: E501
1699+
)
1700+
table.add_row(
1701+
"dialects",
1702+
"\n".join(
1703+
Dialect.from_str(dialect).pretty_name
1704+
for dialect in cast(list[str], metadata.get("dialects"))
1705+
),
1706+
end_section=True,
1707+
)
1708+
table.add_row("links", _info_links_table_for(metadata))
1709+
1710+
if "os" in metadata:
1711+
table.caption = Text(
1712+
f"Ran on {metadata["os"]} {metadata.get("os_version", "")}",
1713+
style="bright_black",
1714+
)
1715+
1716+
return table
1717+
1718+
16631719
@implementation_subcommand() # type: ignore[reportArgumentType]
16641720
@format_option()
16651721
@click.option(
@@ -1676,6 +1732,7 @@ async def info(
16761732
Show information about a supported implementation.
16771733
"""
16781734
serializable: dict[ConnectableId, dict[str, Any]] = {}
1735+
out = console.Console()
16791736

16801737
async for _, each in start():
16811738
metadata = [(k, v) for k, v in each.info.serializable().items() if v]
@@ -1698,11 +1755,8 @@ async def info(
16981755
case "json":
16991756
serializable[each.id] = dict(metadata)
17001757
case "pretty":
1701-
click.echo(
1702-
"\n".join(
1703-
f"{k}: {json.dumps(v, indent=2)}" for k, v in metadata
1704-
),
1705-
)
1758+
table = _info_table_for(dict(metadata))
1759+
out.print(table, "\n")
17061760
case "markdown":
17071761
click.echo(
17081762
"\n".join(

bowtie/_direct_connectable.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,15 @@ def __init__(
117117
name = kwargs.pop("name", compiler_for.__name__)
118118
if "version" not in kwargs:
119119
kwargs["version"] = metadata.version(name)
120-
info = ImplementationInfo(
121-
name=name,
122-
os=platform.system(),
123-
os_version=platform.release(),
124-
language_version=platform.python_version(),
125-
**kwargs,
126-
)
120+
121+
for key, default in [
122+
("language_version", platform.python_version()),
123+
("os", platform.system()),
124+
("os_version", platform.release()),
125+
]:
126+
kwargs.setdefault(key, default)
127+
128+
info = ImplementationInfo(name, **kwargs)
127129
object.__setattr__(self, "_info", info)
128130

129131
def __call__(self):

bowtie/tests/miniatures.py

+40
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,46 @@ def fake_javascript(dialect: Dialect):
8888
return lambda schema, registry: lambda instance: None
8989

9090

91+
@fake(
92+
language="python",
93+
language_version="1.2.3",
94+
os="Linux",
95+
os_version="4.5.6",
96+
)
97+
def fake_language_and_os(dialect: Dialect):
98+
"""
99+
An implementation with a static language and os.
100+
101+
The validity result of instances should not be relied on.
102+
"""
103+
return lambda schema, registry: lambda instance: None
104+
105+
106+
@fake(
107+
language="python",
108+
language_version="1.2.3",
109+
os="Linux",
110+
os_version="4.5.6",
111+
dialects=frozenset(
112+
[
113+
Dialect.by_short_name()["draft2020-12"],
114+
Dialect.by_short_name()["draft7"],
115+
],
116+
),
117+
links=[
118+
Link(description="foo", url=URL.parse("urn:example:bar")),
119+
Link(description="hello", url=URL.parse("urn:example:world")),
120+
],
121+
)
122+
def fake_language_and_os_with_links(dialect: Dialect):
123+
"""
124+
An implementation with a static language and os.
125+
126+
The validity result of instances should not be relied on.
127+
"""
128+
return lambda schema, registry: lambda instance: None
129+
130+
91131
@fake()
92132
def passes_smoke(dialect: Dialect):
93133
"""

bowtie/tests/test_integration.py

+35-46
Original file line numberDiff line numberDiff line change
@@ -2003,28 +2003,26 @@ async def test_info_pretty():
20032003
"--format",
20042004
"pretty",
20052005
"-i",
2006-
miniatures.always_invalid,
2006+
miniatures.fake_language_and_os,
20072007
)
20082008
assert stdout == dedent(
2009-
f"""\
2010-
name: "always_invalid"
2011-
language: "python"
2012-
version: "v1.0.0"
2013-
homepage: "https://bowtie.report/"
2014-
issues: "https://github.com/bowtie-json-schema/bowtie/issues"
2015-
language_version: "{platform.python_version()}"
2016-
os: "{platform.system()}"
2017-
os_version: "{platform.release()}"
2018-
source: "https://github.com/bowtie-json-schema/bowtie"
2019-
dialects: [
2020-
"https://json-schema.org/draft/2020-12/schema",
2021-
"https://json-schema.org/draft/2019-09/schema",
2022-
"http://json-schema.org/draft-07/schema#",
2023-
"http://json-schema.org/draft-06/schema#",
2024-
"http://json-schema.org/draft-04/schema#",
2025-
"http://json-schema.org/draft-03/schema#"
2026-
]
2027-
""",
2009+
"""\
2010+
╭────────────────┬─────────────────────────────────────────────────────────────╮
2011+
│ implementation │ fake_language_and_os v1.0.0 │
2012+
│ language │ python 1.2.3 │
2013+
│ dialects │ Draft 2020-12 │
2014+
│ │ Draft 2019-09 │
2015+
│ │ Draft 7 │
2016+
│ │ Draft 6 │
2017+
│ │ Draft 4 │
2018+
│ │ Draft 3 │
2019+
├────────────────┼─────────────────────────────────────────────────────────────┤
2020+
│ links │ homepage https://bowtie.report/ │
2021+
│ │ source https://github.com/bowtie-json-schema/bowtie │
2022+
│ │ issues https://github.com/bowtie-json-schema/bowtie/iss… │
2023+
╰────────────────┴─────────────────────────────────────────────────────────────╯
2024+
Ran on Linux 4.5.6 \n\n
2025+
""", # noqa: E501
20282026
)
20292027
assert stderr == ""
20302028

@@ -2238,39 +2236,30 @@ async def test_info_json_multiple_implementations():
22382236

22392237

22402238
@pytest.mark.asyncio
2241-
async def test_info_links():
2239+
async def test_info_pretty_links():
22422240
stdout, stderr = await bowtie(
22432241
"info",
22442242
"--format",
22452243
"pretty",
22462244
"-i",
2247-
miniatures.links,
2245+
miniatures.fake_language_and_os_with_links,
22482246
)
22492247
assert stdout == dedent(
2250-
f"""\
2251-
name: "links"
2252-
language: "python"
2253-
version: "v1.0.0"
2254-
homepage: "urn:example"
2255-
issues: "urn:example"
2256-
language_version: "{platform.python_version()}"
2257-
os: "{platform.system()}"
2258-
os_version: "{platform.release()}"
2259-
source: "urn:example"
2260-
dialects: [
2261-
"http://json-schema.org/draft-07/schema#"
2262-
]
2263-
links: [
2264-
{{
2265-
"description": "foo",
2266-
"url": "urn:example:foo"
2267-
}},
2268-
{{
2269-
"description": "bar",
2270-
"url": "urn:example:bar"
2271-
}}
2272-
]
2273-
""",
2248+
"""\
2249+
╭────────────────┬─────────────────────────────────────────────────────────────╮
2250+
│ implementation │ fake_language_and_os_with_links v1.0.0 │
2251+
│ language │ python 1.2.3 │
2252+
│ dialects │ Draft 2020-12 │
2253+
│ │ Draft 7 │
2254+
├────────────────┼─────────────────────────────────────────────────────────────┤
2255+
│ links │ homepage https://bowtie.report/ │
2256+
│ │ source https://github.com/bowtie-json-schema/bowtie │
2257+
│ │ issues https://github.com/bowtie-json-schema/bowtie/iss… │
2258+
│ │ foo urn:example:bar │
2259+
│ │ hello urn:example:world │
2260+
╰────────────────┴─────────────────────────────────────────────────────────────╯
2261+
Ran on Linux 4.5.6 \n\n
2262+
""", # noqa: E501
22742263
)
22752264
assert stderr == ""
22762265

0 commit comments

Comments
 (0)