-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a JSON output for
pyodide xbuildenv search
, better tabular outp…
…ut (#28) ## Description This PR closes #26. It adds a `pyodide xbuildenv search --json` option to print a JSON-based output, along with associated tests. The advantage is that it can be saved to a file, piped to `jq` or shell functions (or any equivalent tools), or simply imported into a Pythonic interface. Additionally, I added a small context manager that does not do anything but stop printing the following lines: ``` Starting new HTTPS connection (1): raw.githubusercontent.com:443 https://raw.githubusercontent.com:443 "GET /pyodide/pyodide/main/pyodide-cross-build-environments.json HTTP/11" 200 917 ``` in the output, because it conflicts with receiving valid JSON. Please let me know if this would be undesirable. If yes, I'll try to work around it so that it gets printed for the non-JSON output (table).
- Loading branch information
1 parent
9b65d5f
commit 6662c28
Showing
5 changed files
with
223 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Class for generating "views", i.e., tabular and JSON outputs from | ||
# metadata objects, currently used in the xbuildenv CLI (search command). | ||
|
||
|
||
import json | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class MetadataView: | ||
version: str | ||
python: str | ||
emscripten: str | ||
pyodide_build: dict[str, str | None] | ||
compatible: bool | ||
|
||
@classmethod | ||
def to_table(cls, views: list["MetadataView"]) -> str: | ||
columns = [ | ||
("Version", 10), | ||
("Python", 10), | ||
("Emscripten", 10), | ||
("pyodide-build", 25), | ||
("Compatible", 10), | ||
] | ||
|
||
# Unicode box-drawing characters | ||
top_left, top_right = "┌", "┐" | ||
bottom_left, bottom_right = "└", "┘" | ||
horizontal, vertical = "─", "│" | ||
t_down, t_up, t_right, t_left = "┬", "┴", "├", "┤" | ||
cross = "┼" | ||
|
||
# Table elements | ||
top_border = ( | ||
top_left | ||
+ t_down.join(horizontal * (width + 2) for _, width in columns) | ||
+ top_right | ||
) | ||
header = ( | ||
vertical | ||
+ vertical.join(f" {name:<{width}} " for name, width in columns) | ||
+ vertical | ||
) | ||
separator = ( | ||
t_right | ||
+ cross.join(horizontal * (width + 2) for _, width in columns) | ||
+ t_left | ||
) | ||
bottom_border = ( | ||
bottom_left | ||
+ t_up.join(horizontal * (width + 2) for _, width in columns) | ||
+ bottom_right | ||
) | ||
|
||
### Printing | ||
table = [top_border, header, separator] | ||
for view in views: | ||
pyodide_build_range = ( | ||
f"{view.pyodide_build['min'] or ''} - {view.pyodide_build['max'] or ''}" | ||
) | ||
row = [ | ||
f"{view.version:<{columns[0][1]}}", | ||
f"{view.python:<{columns[1][1]}}", | ||
f"{view.emscripten:<{columns[2][1]}}", | ||
f"{pyodide_build_range:<{columns[3][1]}}", | ||
f"{'Yes' if view.compatible else 'No':<{columns[4][1]}}", | ||
] | ||
table.append( | ||
vertical + vertical.join(f" {cell} " for cell in row) + vertical | ||
) | ||
table.append(bottom_border) | ||
return "\n".join(table) | ||
|
||
@classmethod | ||
def to_json(cls, views: list["MetadataView"]) -> str: | ||
result = json.dumps( | ||
{ | ||
"environments": [ | ||
{ | ||
"version": view.version, | ||
"python": view.python, | ||
"emscripten": view.emscripten, | ||
"pyodide_build": view.pyodide_build, | ||
"compatible": view.compatible, | ||
} | ||
for view in views | ||
] | ||
}, | ||
indent=2, | ||
) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters