Skip to content

Commit

Permalink
feat: add support for yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
dermasmid committed Jun 19, 2023
1 parent d1e9303 commit ca87d6e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand All @@ -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 |

Expand Down
2 changes: 1 addition & 1 deletion jsoneditor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .jsoneditor import editjson, main

__version__ = "1.5.1"
__version__ = "1.6.0"
17 changes: 16 additions & 1 deletion jsoneditor/jsoneditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import csv
import json
import mimetypes
import yaml
import os
import random
import subprocess
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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,
Expand All @@ -273,6 +283,7 @@ def editjson(
keep_running,
run_in_thread,
is_csv,
is_yaml,
is_ndjson,
is_js_object,
title,
Expand Down Expand Up @@ -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"
)
Expand Down Expand Up @@ -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

Expand All @@ -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:

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
requests
pyperclip
pyyaml

0 comments on commit ca87d6e

Please sign in to comment.