Repository navigation
Add formatting options to controller CLI output #527
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
71f0a90
WIP formatting options
tpoliaw 39bffc8
Make it testable and add tests
tpoliaw 941c785
Changes from review comments
tpoliaw d4773d9
Add type hints to format module
tpoliaw 3260b59
Use builtin types instead of typeshed
tpoliaw 4b2dc32
Update formatting of 'allOf' schemas
tpoliaw 79366b5
Add test coverage for fallback printing
tpoliaw 68e5012
Reorder imports
tpoliaw 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 hidden or 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 hidden or 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,106 @@ | ||
| import builtins | ||
| import enum | ||
| import json | ||
| import sys | ||
| import textwrap | ||
| from functools import partial | ||
| from pprint import pprint | ||
| from textwrap import dedent, indent | ||
| from typing import Any, TextIO | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
| from blueapi.service.model import DeviceResponse, PlanResponse | ||
|
|
||
| FALLBACK = pprint | ||
|
|
||
| Stream = TextIO | None | ||
|
|
||
|
|
||
| class OutputFormat(str, enum.Enum): | ||
| JSON = "json" | ||
| FULL = "full" | ||
| COMPACT = "compact" | ||
|
|
||
| def display(self, obj: Any, out: Stream = None): | ||
| out = out or sys.stdout | ||
| match self: | ||
| case OutputFormat.FULL: | ||
| display_full(obj, out) | ||
| case OutputFormat.COMPACT: | ||
| display_compact(obj, out) | ||
| case OutputFormat.JSON: | ||
| display_json(obj, out) | ||
|
|
||
|
|
||
| def display_full(obj: Any, stream: Stream): | ||
| print = partial(builtins.print, file=stream) | ||
| match obj: | ||
| case PlanResponse(plans=plans): | ||
| for plan in plans: | ||
| print(plan.name) | ||
| if desc := plan.description: | ||
| print(indent(dedent(desc).strip(), " ")) | ||
| if schema := plan.parameter_schema: | ||
| print(" Schema") | ||
| print(indent(json.dumps(schema, indent=2), " ")) | ||
| case DeviceResponse(devices=devices): | ||
| for dev in devices: | ||
| print(dev.name) | ||
| for proto in dev.protocols: | ||
| print(" " + proto) | ||
| case other: | ||
| FALLBACK(other, stream=stream) | ||
|
|
||
|
|
||
| def display_json(obj: Any, stream: Stream): | ||
| print = partial(builtins.print, file=stream) | ||
| match obj: | ||
| case PlanResponse(plans=plans): | ||
| print(json.dumps([p.dict() for p in plans], indent=2)) | ||
| case DeviceResponse(devices=devices): | ||
| print(json.dumps([d.dict() for d in devices], indent=2)) | ||
|
tpoliaw marked this conversation as resolved.
Resolved
|
||
| case BaseModel(): | ||
| print(json.dumps(obj.dict(), indent=2)) | ||
| case _: | ||
| print(json.dumps(obj)) | ||
|
|
||
|
|
||
| def display_compact(obj: Any, stream: Stream): | ||
| print = partial(builtins.print, file=stream) | ||
| match obj: | ||
| case PlanResponse(plans=plans): | ||
| for plan in plans: | ||
| print(plan.name) | ||
| if desc := plan.description: | ||
| print(indent(dedent(desc.split("\n\n")[0].strip("\n")), " ")) | ||
| if schema := plan.parameter_schema: | ||
| print(" Args") | ||
| for arg, spec in schema.get("properties", {}).items(): | ||
| req = arg in schema.get("required", {}) | ||
| print(f" {arg}={_describe_type(spec, req)}") | ||
| case DeviceResponse(devices=devices): | ||
| for dev in devices: | ||
| print(dev.name) | ||
| print(indent(textwrap.fill(", ".join(dev.protocols), 80), " ")) | ||
| case other: | ||
| FALLBACK(other, stream=stream) | ||
|
|
||
|
|
||
| def _describe_type(spec: dict[Any, Any], required: bool = False): | ||
| disp = "" | ||
| match spec.get("type"): | ||
| case None: | ||
| if all_of := spec.get("allOf"): | ||
| items = (_describe_type(f, False) for f in all_of) | ||
| disp += f'{" & ".join(items)}' | ||
| else: | ||
| disp += "Any" | ||
| case "array": | ||
| element = spec.get("items", {}).get("type", "Any") | ||
| disp += f"[{element}]" | ||
| case other: | ||
| disp += other | ||
| if required: | ||
| disp += " (Required)" | ||
| return disp | ||
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.