33import csv
44import json
55import mimetypes
6+ import yaml
67import os
78import random
89import subprocess
@@ -52,6 +53,7 @@ def __init__(
5253 keep_running : bool = False ,
5354 run_in_thread : bool = False ,
5455 is_csv : bool = False ,
56+ is_yaml : bool = False ,
5557 is_ndjson : bool = False ,
5658 is_js_object : bool = False ,
5759 title : str = None ,
@@ -64,6 +66,7 @@ def __init__(
6466 self .keep_running = keep_running
6567 self .run_in_thread = run_in_thread
6668 self .is_csv = is_csv
69+ self .is_yaml = is_yaml
6770 self .is_ndjson = is_ndjson
6871 self .is_js_object = is_js_object
6972 self .title = title
@@ -129,6 +132,8 @@ def is_file(source: str) -> bool:
129132 def detect_source_by_filename (self , source : str ):
130133 if source .endswith (".csv" ):
131134 self .is_csv = True
135+ elif source .endswith (".yaml" ):
136+ self .is_yaml = True
132137 elif any (source .endswith (ext ) for ext in [".ndjson" , ".jsonl" ]):
133138 self .is_ndjson = True
134139
@@ -140,6 +145,10 @@ def load_json(self, source):
140145 result = list (csv .DictReader (source .split ("\n " )))
141146 elif isinstance (source , TextIOWrapper ):
142147 result = list (csv .DictReader (source ))
148+ elif self .is_yaml :
149+ result = list (filter (bool , yaml .load_all (source , Loader = yaml .UnsafeLoader )))
150+ if len (result ) == 1 :
151+ result = result [0 ]
143152 elif self .is_ndjson :
144153 if isinstance (source , str ):
145154 lines = source .splitlines ()
@@ -257,6 +266,7 @@ def editjson(
257266 keep_running : bool = False ,
258267 run_in_thread : bool = False ,
259268 is_csv : bool = False ,
269+ is_yaml : bool = False ,
260270 is_ndjson : bool = False ,
261271 is_js_object : bool = False ,
262272 title : str = None ,
@@ -273,6 +283,7 @@ def editjson(
273283 keep_running ,
274284 run_in_thread ,
275285 is_csv ,
286+ is_yaml ,
276287 is_ndjson ,
277288 is_js_object ,
278289 title ,
@@ -332,6 +343,7 @@ def main() -> None:
332343 parser .add_argument ("--out" , help = "File to output when in edit mode" )
333344 parser .add_argument ("-t" , help = "Title to display in browser window" )
334345 parser .add_argument ("--csv" , help = "Input is CSV" , action = "store_true" )
346+ parser .add_argument ("--yaml" , help = "Input is YAML" , action = "store_true" )
335347 parser .add_argument (
336348 "--js" , help = "Input is a JavaScript Object" , action = "store_true"
337349 )
@@ -362,6 +374,9 @@ def main() -> None:
362374 if args .csv :
363375 options ["is_csv" ] = True
364376
377+ if args .yaml :
378+ options ["is_yaml" ] = True
379+
365380 if args .js :
366381 options ["is_js_object" ] = True
367382
@@ -376,7 +391,7 @@ def main() -> None:
376391 elif args .c :
377392 options ["data" ] = pyperclip .paste ()
378393 else :
379- raise ValueError ("No data passed" )
394+ raise ValueError ("No data passed. " )
380395
381396 if args .e :
382397
0 commit comments