|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import os |
| 4 | +import yaml |
| 5 | + |
| 6 | + |
| 7 | +def json2yaml(files): |
| 8 | + for file in files: |
| 9 | + # Iterate through the files |
| 10 | + if os.path.isfile(f"{os.getcwd()}\{file}"): |
| 11 | + # Get the base filename |
| 12 | + filename_full = os.path.basename(file) |
| 13 | + # Get the filename and extension |
| 14 | + filename, extension = os.path.splitext(filename_full) |
| 15 | + |
| 16 | + # Opening the file |
| 17 | + source_file = open(filename_full, "r") |
| 18 | + source_content = json.load(source_file) |
| 19 | + source_file.close() |
| 20 | + |
| 21 | + # Processing the conversion |
| 22 | + output = yaml.dump(source_content) |
| 23 | + |
| 24 | + # If the target file already exists exit |
| 25 | + new_filename = f'{filename}.yaml' |
| 26 | + if os.path.exists(new_filename): |
| 27 | + print("ERROR: " + new_filename + " already exists") |
| 28 | + |
| 29 | + # Otherwise write to the specified file |
| 30 | + else: |
| 31 | + target_file = open(new_filename, "w") |
| 32 | + target_file.write(output) |
| 33 | + target_file.close() |
| 34 | + print(f"YAML File Converted: {new_filename}") |
| 35 | + |
| 36 | + |
| 37 | +def yaml2json(files): |
| 38 | + for file in files: |
| 39 | + # Iterate through the files |
| 40 | + if os.path.isfile(f"{os.getcwd()}\{file}"): |
| 41 | + # Get the base filename |
| 42 | + filename_full = os.path.basename(file) |
| 43 | + # Get the filename and extension |
| 44 | + filename, extension = os.path.splitext(filename_full) |
| 45 | + |
| 46 | + # Opening the file |
| 47 | + source_file = open(filename_full, "r") |
| 48 | + source_content = yaml.safe_load(source_file) |
| 49 | + source_file.close() |
| 50 | + |
| 51 | + # Processing the conversion |
| 52 | + output = json.dumps(source_content) |
| 53 | + |
| 54 | + # If the target file already exists exit |
| 55 | + new_filename = f'{filename}.json' |
| 56 | + if os.path.exists(new_filename): |
| 57 | + print(f"ERROR: {new_filename} already exists") |
| 58 | + |
| 59 | + # Otherwise write to the specified file |
| 60 | + else: |
| 61 | + target_file = open(new_filename, "w") |
| 62 | + target_file.write(output) |
| 63 | + target_file.close() |
| 64 | + print(f"JSON File Converted: {new_filename}") |
| 65 | + |
| 66 | + |
| 67 | +def main(): |
| 68 | + parser = argparse.ArgumentParser(description='Convert JSON to YAML and YAML to JSON') |
| 69 | + |
| 70 | + # Argument is to accept a file(s) |
| 71 | + parser.add_argument('-f', dest='file', nargs='+', type=str, help='Submit a file(s)', required=True) |
| 72 | + |
| 73 | + # Argument is to convert file to json |
| 74 | + parser.add_argument('-j', dest='json', action='store_true', help='Convert YAML to JSON', required=True) |
| 75 | + |
| 76 | + # Argument is to convert file to yaml |
| 77 | + parser.add_argument('-y', dest='yaml', action='store_true', help='Convert JSON to YAML', required=True) |
| 78 | + |
| 79 | + args = parser.parse_args() |
| 80 | + |
| 81 | + if args.file: |
| 82 | + # Accepts files |
| 83 | + files = args.file |
| 84 | + |
| 85 | + if args.json: |
| 86 | + # Call to function |
| 87 | + yaml2json(files) |
| 88 | + |
| 89 | + if args.yaml: |
| 90 | + # Call to function |
| 91 | + json2yaml(files) |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == '__main__': |
| 95 | + main() |
0 commit comments