-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1444 from willmcgugan/fixes-10.8.0
added print_json
- Loading branch information
Showing
11 changed files
with
155 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
rich.json | ||
========= | ||
|
||
.. automodule:: rich.json | ||
:members: | ||
|
||
|
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
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,53 @@ | ||
from json import loads, dumps | ||
|
||
from .text import Text | ||
from .highlighter import JSONHighlighter, NullHighlighter | ||
|
||
|
||
class JSON: | ||
"""A rebderable which pretty prints JSON. | ||
Args: | ||
json (str): JSON encoded data. | ||
indent (int, optional): Number of characters to indent by. Defaults to True. | ||
highlight (bool, optional): Enable highlighting. Defaults to True. | ||
""" | ||
|
||
def __init__(self, json: str, indent: int = 4, highlight: bool = True) -> None: | ||
data = loads(json) | ||
json = dumps(data, indent=indent) | ||
highlighter = JSONHighlighter() if highlight else NullHighlighter() | ||
self.text = highlighter(json) | ||
self.text.no_wrap = True | ||
self.text.overflow = None | ||
|
||
def __rich__(self) -> Text: | ||
return self.text | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
import argparse | ||
import sys | ||
|
||
parser = argparse.ArgumentParser(description="Pretty print json") | ||
parser.add_argument( | ||
"path", | ||
metavar="PATH", | ||
help="path to file, or - for stdin", | ||
) | ||
args = parser.parse_args() | ||
|
||
from rich.console import Console | ||
|
||
console = Console() | ||
error_console = Console(stderr=True) | ||
|
||
try: | ||
with open(args.path, "rt") as json_file: | ||
json_data = json_file.read() | ||
except Exception as error: | ||
error_console.print(f"Unable to read {args.path!r}; {error}") | ||
sys.exit(-1) | ||
|
||
console.print(JSON(json_data), soft_wrap=True) |
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