-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add a JSON output for pyodide xbuildenv search
, better tabular output
#28
Merged
agriyakhetarpal
merged 19 commits into
pyodide:main
from
agriyakhetarpal:feat/xbuildenv-search-json-option
Sep 18, 2024
Merged
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d679bac
Suppress urllib3 logging for JSON response
agriyakhetarpal 6c3b73c
Add `--json` option, use helper functions for output
agriyakhetarpal a1a3a43
Add fixture to test JSON validity (non-schema)
agriyakhetarpal 1793256
Convert `json` import to a top-level one
agriyakhetarpal 65807d4
Test JSON output from xbuildenv search
agriyakhetarpal c02e70c
Add CHANGELOG entry for #28
agriyakhetarpal 095f1a7
Fix a failing test
agriyakhetarpal 8f8de85
Standardise return type from JSON output
agriyakhetarpal ce1904a
Use Unicode box-drawing characters for printing
agriyakhetarpal a778a08
Fix failing table formatting test
agriyakhetarpal f8f6396
Move note to "Added" section
agriyakhetarpal a90b612
Shift to a `MetadataView` dataclass
agriyakhetarpal 4876f29
Reorder `to_table()` and `to_json()` methods
agriyakhetarpal 180295c
Use `MetadataView` classmethods for outputs
agriyakhetarpal 9325e75
Run [integration] tests just to be sure
agriyakhetarpal b754dfa
Add note about tabular output improvements
agriyakhetarpal 5370c25
Return `str` output from `MetadataView`
agriyakhetarpal a9f9a61
Don't make `is_valid_json` a fixture
agriyakhetarpal 5489a55
Merge branch 'main' into feat/xbuildenv-search-json-option
agriyakhetarpal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]) -> None: | ||
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) | ||
print("\n".join(table)) | ||
agriyakhetarpal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@classmethod | ||
def to_json(cls, views: list["MetadataView"]) -> None: | ||
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, | ||
) | ||
print(result) | ||
agriyakhetarpal marked this conversation as resolved.
Show resolved
Hide resolved
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Why is it defined as a fixture? Doesn't calling
is_valid_json
work?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I was using it in the test invocation, which meant that it needed to be a fixture for
pytest
to recognise it. But we don't need that, necessarily. In a9f9a61, I've removed the fixture and called it directly, as you suggest.