@@ -13,25 +13,29 @@ def json2yaml(files):
13
13
# Get the filename and extension
14
14
filename , extension = os .path .splitext (filename_full )
15
15
16
- # Opening the file
17
- source_file = open (filename_full , "r" )
18
- source_content = json .load (source_file )
19
- source_file .close ()
16
+ if extension == '.json' :
17
+ # Opening the file
18
+ source_file = open (filename_full , "r" )
19
+ source_content = json .load (source_file )
20
+ source_file .close ()
21
+
22
+ # Processing the conversion
23
+ output = yaml .dump (source_content )
24
+
25
+ # If the target file already exists exit
26
+ new_filename = f'{ filename } .yaml'
27
+ if os .path .exists (new_filename ):
28
+ print ("ERROR: " + new_filename + " already exists" )
29
+
30
+ # Otherwise write to the specified file
31
+ else :
32
+ target_file = open (new_filename , "w" )
33
+ target_file .write (output )
34
+ target_file .close ()
35
+ print (f"YAML File Converted: { new_filename } " )
20
36
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
37
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 } " )
38
+ print (f"Extension NOT Valid for file: { file } " )
35
39
36
40
37
41
def yaml2json (files ):
@@ -43,25 +47,28 @@ def yaml2json(files):
43
47
# Get the filename and extension
44
48
filename , extension = os .path .splitext (filename_full )
45
49
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
50
+ if extension == ".yaml" :
51
+ # Opening the file
52
+ source_file = open (filename_full , "r" )
53
+ source_content = yaml .safe_load (source_file )
54
+ source_file .close ()
55
+
56
+ # Processing the conversion
57
+ output = json .dumps (source_content )
58
+
59
+ # If the target file already exists exit
60
+ new_filename = f'{ filename } .json'
61
+ if os .path .exists (new_filename ):
62
+ print (f"ERROR: { new_filename } already exists" )
63
+
64
+ # Otherwise write to the specified file
65
+ else :
66
+ target_file = open (new_filename , "w" )
67
+ target_file .write (output )
68
+ target_file .close ()
69
+ print (f"JSON File Converted: { new_filename } " )
60
70
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 } " )
71
+ print (f"Extension NOT Valid for file: { file } " )
65
72
66
73
67
74
def main ():
@@ -71,10 +78,10 @@ def main():
71
78
parser .add_argument ('-f' , dest = 'file' , nargs = '+' , type = str , help = 'Submit a file(s)' , required = True )
72
79
73
80
# Argument is to convert file to json
74
- parser .add_argument ('-j' , dest = 'json' , action = 'store_true' , help = 'Convert YAML to JSON' , required = True )
81
+ parser .add_argument ('-j' , dest = 'json' , action = 'store_true' , help = 'Convert YAML to JSON' )
75
82
76
83
# Argument is to convert file to yaml
77
- parser .add_argument ('-y' , dest = 'yaml' , action = 'store_true' , help = 'Convert JSON to YAML' , required = True )
84
+ parser .add_argument ('-y' , dest = 'yaml' , action = 'store_true' , help = 'Convert JSON to YAML' )
78
85
79
86
args = parser .parse_args ()
80
87
0 commit comments