4
4
import json
5
5
import os
6
6
import fnmatch
7
- import sys
8
7
from collections import OrderedDict
8
+ import argparse
9
+ from argparse import ArgumentDefaultsHelpFormatter
9
10
10
- base_dir = os . path . abspath ( os . path . dirname ( sys . argv [ 0 ]) )
11
+ parser = argparse . ArgumentParser ( formatter_class = ArgumentDefaultsHelpFormatter )
11
12
12
- displayNamePrefix = "Appsody"
13
- if len (sys .argv ) > 1 :
14
- displayNamePrefix = sys .argv [1 ]
13
+ parser .add_argument ("-n" , "--namePrefix" , help = "Display name prefix." , default = "Appsody" )
14
+ parser .add_argument ("-f" , "--file" , help = "Absolute or relative path, to a yaml file or directory of yaml files." , default = os .getcwd ())
15
15
16
- # directory to store assets for test or release
17
- assets_dir = base_dir + "/assets/"
16
+ args = parser .parse_args ()
18
17
19
- for file in os .listdir (assets_dir ):
20
- if fnmatch .fnmatch (file , '*index.yaml' ):
21
- with open (assets_dir + file , 'r' ) as yamlFile , open (assets_dir + os .path .splitext (file )[0 ] + ".json" , 'wb' ) as jsonFile :
18
+ displayNamePrefix = args .namePrefix
19
+
20
+ yaml_dir = os .path .normpath (args .file )
21
+
22
+ def generate_json ():
23
+ with open (inputFile , 'r' ) as yamlFile , open (inputFile .rsplit ('.' , 1 )[0 ] + ".json" , 'wb' ) as jsonFile :
22
24
try :
23
25
doc = yaml .safe_load (yamlFile )
24
26
list = []
32
34
template = ""
33
35
else :
34
36
template = " " + item ['templates' ][n ]['id' ]
35
-
37
+
36
38
# populate stack details
37
39
res = (OrderedDict ([
38
40
("displayName" , displayNamePrefix + " " + item ['name' ] + template + " template" ),
46
48
item ['id' ] + "/devfile.yaml" )
47
49
]))
48
50
]))
51
+
52
+ if ('deprecated' in item ):
53
+ res .update ([("displayName" , "[Deprecated] " + displayNamePrefix + " " + item ['name' ] + template + " template" ),
54
+ ("deprecated" , item ['deprecated' ])])
55
+
49
56
list .append (res )
50
57
51
58
jsonFile .write (json .dumps (list , indent = 4 , ensure_ascii = False ).encode ('utf8' ))
52
- print ("Generated: " + os .path .splitext (file )[0 ] + ".json" )
53
-
59
+ print ("Generated: " + inputFile .rsplit ('.' , 1 )[0 ] + ".json" )
54
60
except yaml .YAMLError as exc :
55
61
print (exc )
62
+
63
+ if os .path .isdir (yaml_dir ):
64
+ for file in os .listdir (yaml_dir ):
65
+ if fnmatch .fnmatch (file , '*.yaml' ):
66
+ inputFile = yaml_dir + "/" + file
67
+ generate_json ()
68
+ else :
69
+ inputFile = yaml_dir
70
+ generate_json ()
0 commit comments