Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1472d61

Browse files
JSON - YAML Converter modification
1 parent 28eb859 commit 1472d61

File tree

1 file changed

+44
-37
lines changed

1 file changed

+44
-37
lines changed

‎JSON - YAML Converter/converter.py

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,29 @@ def json2yaml(files):
1313
# Get the filename and extension
1414
filename, extension = os.path.splitext(filename_full)
1515

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}")
2036

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
3037
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}")
3539

3640

3741
def yaml2json(files):
@@ -43,25 +47,28 @@ def yaml2json(files):
4347
# Get the filename and extension
4448
filename, extension = os.path.splitext(filename_full)
4549

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}")
6070
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}")
6572

6673

6774
def main():
@@ -71,10 +78,10 @@ def main():
7178
parser.add_argument('-f', dest='file', nargs='+', type=str, help='Submit a file(s)', required=True)
7279

7380
# 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')
7582

7683
# 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')
7885

7986
args = parser.parse_args()
8087

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /