forked from cloudtools/troposphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.py
199 lines (164 loc) · 5.41 KB
/
gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import argparse
import json
import sys
# Python code generator to create new troposphere classes from the
# AWS resource specification.
#
# Todo:
# - Currently only handles the single files (not the all-in-one)
# (Note: but will deal with things like spec/GuardDuty*)
# - Handle adding in validators
# - Verify propery dependency/ordering in the file
# - Needs better error checking
# - Need to figure out the correct Timestamp type
copyright_header = """\
# Copyright (c) 2012-2018, Mark Peek <[email protected]>
# All rights reserved.
#
# See LICENSE file for full license.
from . import AWSObject, AWSProperty
from .validators import boolean, integer
"""
def get_required(value):
return value['Required']
map_type = {
'Boolean': 'boolean',
'Double': 'float',
'Integer': 'integer',
'Json': 'dict',
'Long': 'integer',
'String': 'basestring',
'Timestamp': 'basestring',
}
map_type3 = {
'Boolean': 'bool',
'Double': 'float',
'Integer': 'int',
'Json': 'dict',
'Long': 'int',
'String': 'str',
'Timestamp': 'str',
}
def get_type(value):
if 'PrimitiveType' in value:
return map_type.get(value['PrimitiveType'], value['PrimitiveType'])
if value['Type'] == 'List':
if 'ItemType' in value:
return "[%s]" % value['ItemType']
else:
return "[%s]" % map_type.get(value['PrimitiveItemType'])
elif value['Type'] == 'Map':
return 'dict'
else:
# Non-primitive (Property) name
return value['Type']
import pprint
pprint.pprint(value)
raise ValueError("get_type")
def get_type3(value):
if 'PrimitiveType' in value:
return map_type3.get(value['PrimitiveType'], value['PrimitiveType'])
if value['Type'] == 'List':
if 'ItemType' in value:
return "[%s]" % value['ItemType']
else:
return "[%s]" % map_type3.get(value['PrimitiveItemType'])
elif value['Type'] == 'Map':
return 'dict'
else:
# Non-primitive (Property) name
return value['Type']
import pprint
pprint.pprint(value)
raise ValueError("get_type")
def output_class(class_name, properties, resource_name=None):
print
print
if resource_name:
print 'class %s(AWSObject):' % class_name
print ' resource_type = "%s"' % resource_name
print
else:
print 'class %s(AWSProperty):' % class_name
# Output the props dict
print ' props = {'
for key, value in sorted(properties.iteritems()):
if key == 'Tags':
value_type = "Tags"
else:
value_type = get_type(value)
# Wrap long names for pycodestyle
if len(key) + len(value_type) < 55:
print " '%s': (%s, %s)," % (
key, value_type, get_required(value))
else:
print " '%s':\n (%s, %s)," % (
key, value_type, get_required(value))
print ' }'
def output_class_stub(class_name, properties, resource_name=None):
print
print
if resource_name:
print 'class %s(AWSObject):' % class_name
print ' resource_type: str'
print
sys.stdout.write(' def __init__(self, title')
else:
print 'class %s(AWSProperty):' % class_name
print
sys.stdout.write(' def __init__(self')
for key, value in sorted(properties.iteritems()):
if key == 'Tags':
value_type = "Tags"
else:
value_type = get_type3(value)
if value_type.startswith("["): # Means that args are a list
sys.stdout.write(', %s:List%s=...' % (key, value_type))
else:
sys.stdout.write(', %s:%s=...' % (key, value_type))
print ') -> None: ...'
print
for key, value in sorted(properties.iteritems()):
if key == 'Tags':
value_type = "Tags"
else:
value_type = get_type3(value)
if value_type.startswith("["): # Means that args are a list
print ' %s: List%s' % (key, value_type)
else:
print ' %s: %s' % (key, value_type)
def process_file(filename, stub=False):
f = open(filename)
j = json.load(f)
if 'PropertyTypes' in j:
for property_name, property_dict in j['PropertyTypes'].items():
if property_name == "Tag":
print "from troposphere import Tags"
print
continue
class_name = property_name.split('.')[1]
properties = property_dict['Properties']
if stub:
output_class_stub(class_name, properties)
else:
output_class(class_name, properties)
for resource_name, resource_dict in j['ResourceType'].items():
class_name = resource_name.split(':')[4]
properties = resource_dict['Properties']
if stub:
output_class_stub(class_name, properties, resource_name)
else:
output_class(class_name, properties, resource_name)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--stub', action='store_true', default=False)
parser.add_argument('filename', nargs='+')
args = parser.parse_args()
if args.stub:
print copyright_header,
for f in args.filename:
process_file(f, stub=True)
else:
print copyright_header,
for f in args.filename:
process_file(f)