forked from cloudtools/troposphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.py
109 lines (87 loc) · 2.92 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
import argparse
import json
# 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-2017, 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',
}
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 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()):
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 process_file(filename):
f = open(filename)
j = json.load(f)
if 'PropertyTypes' in j:
for property_name, property_dict in j['PropertyTypes'].items():
class_name = property_name.split('.')[1]
properties = property_dict['Properties']
output_class(class_name, properties)
for resource_name, resource_dict in j['ResourceType'].items():
class_name = resource_name.split(':')[4]
properties = resource_dict['Properties']
output_class(class_name, properties, resource_name)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('filename', nargs='+')
args = parser.parse_args()
print copyright_header,
for f in args.filename:
process_file(f)