Skip to content

Commit

Permalink
[resotocore][feat] format --json prettifies json (#893)
Browse files Browse the repository at this point in the history
* [resotocore][feat] format --json prettifies json

* define type of dict
  • Loading branch information
aquamatthias committed May 31, 2022
1 parent 99c09e7 commit 973a9be
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
4 changes: 2 additions & 2 deletions resotocore/resotocore/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -1935,9 +1935,9 @@ def args_info(self) -> ArgsInfo:
ArgInfo(expects_value=True, help_text="format definition with {} placeholders", option_group="output"),
]

formats = {
formats: Dict[str, Callable[[AsyncIterator[JsonElement]], AsyncIterator[JsonElement]]] = {
"ndjson": respond_ndjson,
"json": respond_json,
"json": partial(respond_json, indent=2),
"text": respond_text,
"yaml": respond_yaml,
"cytoscape": respond_cytoscape,
Expand Down
14 changes: 4 additions & 10 deletions resotocore/resotocore/web/content_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import re
from collections import defaultdict
from typing import AsyncGenerator, List, Dict, AsyncIterator, Tuple, Callable, Optional
from typing import AsyncGenerator, List, Dict, AsyncIterator, Tuple, Callable, Optional, Any

import yaml
from aiohttp.web import StreamResponse, Request, Response, json_response
Expand All @@ -14,23 +14,17 @@
from resotocore.model.resolve_in_graph import NodePath
from resotocore.model.typed_model import to_json
from resotocore.types import Json, JsonElement
from resotocore.util import (
del_value_in_path,
value_in_path,
value_in_path_get,
count_iterator,
identity,
)
from resotocore.util import del_value_in_path, value_in_path, value_in_path_get, count_iterator, identity

log = logging.getLogger(__name__)


async def respond_json(gen: AsyncIterator[JsonElement]) -> AsyncGenerator[str, None]:
async def respond_json(gen: AsyncIterator[JsonElement], **json_args: Any) -> AsyncGenerator[str, None]:
sep = ","
yield "["
first = True
async for item in gen:
js = json.dumps(to_json(item))
js = json.dumps(to_json(item), **json_args)
if not first:
yield sep
yield js
Expand Down

0 comments on commit 973a9be

Please sign in to comment.