diff --git a/README.md b/README.md index cf3ed1a..d30d8ab 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ You can pass the json in any of the following formats: | `keep_running`| `bool` | ✔️ | Whether to keep the server running. Defaults to `False`. | | `run_in_thread`| `bool` | ✔️ | Whether to run the server in a separate thread. Defaults to `False`. | | `is_csv`| `bool` | ✔️ | Whether the data is csv data. Defaults to `False`. | +| `is_yaml`| `bool` | ✔️ | Whether the data is yaml data. Defaults to `False`. | | `is_ndjson`| `bool` | ✔️ | Whether the data is Newline Delimited JSON . Defaults to `False`. | | `is_js_object`| `bool` | ✔️ | Whether the data is a JavaScript Object. Defaults to `False`. | | `title`| `str` | ✔️ | A title to display in the browser. | @@ -91,6 +92,7 @@ You can pass the json in any of the following formats: | `--out` | File to output when in edit mode | | `-t` | Title to display in browser window | | `--csv` | Input is CSV | +| `--yaml` | Input is YAML | | `--js` | Input is a JavaScript Object | | `--ndjson`| Input is Newline Delimited JSON | diff --git a/jsoneditor/__init__.py b/jsoneditor/__init__.py index 7102568..791d4f6 100644 --- a/jsoneditor/__init__.py +++ b/jsoneditor/__init__.py @@ -1,3 +1,3 @@ from .jsoneditor import editjson, main -__version__ = "1.5.1" +__version__ = "1.6.0" diff --git a/jsoneditor/jsoneditor.py b/jsoneditor/jsoneditor.py index 3275f36..7f0d1ff 100644 --- a/jsoneditor/jsoneditor.py +++ b/jsoneditor/jsoneditor.py @@ -3,6 +3,7 @@ import csv import json import mimetypes +import yaml import os import random import subprocess @@ -52,6 +53,7 @@ def __init__( keep_running: bool = False, run_in_thread: bool = False, is_csv: bool = False, + is_yaml: bool = False, is_ndjson: bool = False, is_js_object: bool = False, title: str = None, @@ -64,6 +66,7 @@ def __init__( self.keep_running = keep_running self.run_in_thread = run_in_thread self.is_csv = is_csv + self.is_yaml = is_yaml self.is_ndjson = is_ndjson self.is_js_object = is_js_object self.title = title @@ -129,6 +132,8 @@ def is_file(source: str) -> bool: def detect_source_by_filename(self, source: str): if source.endswith(".csv"): self.is_csv = True + elif source.endswith(".yaml"): + self.is_yaml = True elif any(source.endswith(ext) for ext in [".ndjson", ".jsonl"]): self.is_ndjson = True @@ -140,6 +145,10 @@ def load_json(self, source): result = list(csv.DictReader(source.split("\n"))) elif isinstance(source, TextIOWrapper): result = list(csv.DictReader(source)) + elif self.is_yaml: + result = list(filter(bool, yaml.load_all(source, Loader=yaml.UnsafeLoader))) + if len(result) == 1: + result = result[0] elif self.is_ndjson: if isinstance(source, str): lines = source.splitlines() @@ -257,6 +266,7 @@ def editjson( keep_running: bool = False, run_in_thread: bool = False, is_csv: bool = False, + is_yaml: bool = False, is_ndjson: bool = False, is_js_object: bool = False, title: str = None, @@ -273,6 +283,7 @@ def editjson( keep_running, run_in_thread, is_csv, + is_yaml, is_ndjson, is_js_object, title, @@ -332,6 +343,7 @@ def main() -> None: parser.add_argument("--out", help="File to output when in edit mode") parser.add_argument("-t", help="Title to display in browser window") parser.add_argument("--csv", help="Input is CSV", action="store_true") + parser.add_argument("--yaml", help="Input is YAML", action="store_true") parser.add_argument( "--js", help="Input is a JavaScript Object", action="store_true" ) @@ -362,6 +374,9 @@ def main() -> None: if args.csv: options["is_csv"] = True + if args.yaml: + options["is_yaml"] = True + if args.js: options["is_js_object"] = True @@ -376,7 +391,7 @@ def main() -> None: elif args.c: options["data"] = pyperclip.paste() else: - raise ValueError("No data passed") + raise ValueError("No data passed.") if args.e: diff --git a/requirements.txt b/requirements.txt index c098848..3dd7196 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ requests pyperclip +pyyaml