github.com/RyanSiu1995/gcb-visualizer@v1.0.2-0.20211121083050-f618fa372726/test/utils/yamlToJson.py (about) 1 import sys 2 import os 3 import yaml, json 4 from typing import List 5 6 7 def isYaml(file: str) -> bool: 8 # Check if the file has a yaml extension 9 # 10 # Parameters: 11 # file (str): target filename 12 # 13 # Returns: 14 # isYaml (bool) 15 16 _, fileExt = os.path.splitext(file) 17 return fileExt.lower() == ".yaml" or fileExt.lower() == ".yml" 18 19 def listdir_fullpath(d: str) -> List(str): 20 # A better listdir function which returns the full path for list dir function 21 # 22 # Parameters: 23 # d (str): target dirname 24 # 25 # Returns: 26 # listOfFileNames (List(str)): List of file names with full path 27 28 return [os.path.join(d, f) for f in os.listdir(d)] 29 30 if __name__ == "__main__": 31 '''A cli to convert all the YAML files on dir to JSON files''' 32 if len(sys.argv) != 2: 33 print("Please provide the dir containing the yaml test files") 34 sys.exit(1) 35 yamlFiles = list(filter(isYaml, listdir_fullpath(sys.argv[1]))) 36 for yamlFile in yamlFiles: 37 with open(yamlFile) as file: 38 target = yaml.load(file) 39 filename, _ = os.path.splitext(yamlFile) 40 with open(filename + ".json", "w+") as jsonFile: 41 json.dump(target, jsonFile)