This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgenrubyfrompy.py
executable file
·280 lines (223 loc) · 8.07 KB
/
genrubyfrompy.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/python
import pprint
import os
import sys
import re
import time
from string import Template
from collections import OrderedDict
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + '/pysdk')
from insieme.pymit.pyaccess import PyClassDirectory
def rubyClassName(className):
rubyname = list(className)
if len(rubyname) > 0:
rubyname[0] = rubyname[0].upper()
if '.' in rubyname:
rubyname.remove('.')
return ''.join(rubyname)
def getChildren(pyClassDict):
return [rubyClassName(x[0]) for x in pyClassDict['_children'].items()]
def getProps(pyClassDict):
# {
# 'test' => {'isAdmin' => true}, 'test2' => {'isAdmin'=> true}
# }
prop_entries = []
flags_list = ['isAdmin', 'isImplicit', 'isCreateOnly', 'isDn', 'isRn', 'isExplicit']
for prop, flags in pyClassDict['_props'].items():
propflags = []
for flag in flags_list:
if hasattr(flags, '_{}'.format(flag)):
bo0l = 'true' if getattr(flags, '_{}'.format(flag)) else 'false'
propflags.append("'{}' => {}".format(flag, bo0l))
prop_entries.append("'{}' => {{ {} }}".format(prop, ', '.join(propflags)))
return '{{ {} }}'.format(',\n '.join(prop_entries))
# entries = ["%s => {".format(cls, props.get('isAdmin')) for cls,props in pyClassDict['_props'].items()]
def getNamingProps(pyClassDict):
return pyClassDict['_orderedNamingProps']
def getContainers(pyClassDict):
return [rubyClassName(x[0]) for x in pyClassDict['_containers'].items()]
# return ', '.join('\'%s\'' % c for c in pyClassDict['_containers'].keys())
def getRnFormatAsRuby(pyClassDict):
a = pyClassDict['_rnFormat']
return '\'%s\'' % re.sub(
'\%\(([a-zA-Z]+)\)s', '\' << @attributes[\'\\1\'] << \'', a)
def getRnFunc(pyClassDict):
'''
Generates the logic to create the relative name for the object, based on the rnPrefixes and orderedNames
'''
# if len(pyClassDict['_orderedNamingProps']) == 0:
# rnFunc = 'self.class.prefix'
# elif len(pyClassDict['_orderedNamingProps']) == len(pyClassDict['_rnPrefixes']):
# rnFunc = ' + '.join("'%s' + @options[:%s]" % t for t in zip(getRnPrefixes(pyClassDict), pyClassDict['_orderedNamingProps']))
# else:
# rnFunc = getRnFormatAsRuby(pyClassDict)
# print 'len of rnPrefixes is %d' % len(pyClassDict['_rnPrefixes'])
# print 'len of naming props is %d' % len(pyClassDict['_orderedNamingProps'])
# pprint.pprint(pyClassDict)
# sys.exit(0)
# rnFunc = 'self.class.prefix'
rnFunc = getRnFormatAsRuby(pyClassDict)
return rnFunc
def getRnPrefixes(pyClassDict):
if len(pyClassDict['_rnPrefixes']) > 0:
return ', '.join(['[\'{0}\', {1}]'.format(x[0], str(x[1]).lower()) for x in pyClassDict['_rnPrefixes']])
else:
return ''
def getLabel(pyClassDict):
return pyClassDict['_label']
def getRnPrefix(pyClassDict):
if len(pyClassDict['_rnPrefixes']) > 0:
# if len(pyClassDict['_rnPrefixes']) > 1:
# print '%s has %d rnPrefixes' % (pyClassDict['_name'],
# len(pyClassDict['_rnPrefixes']))
return pyClassDict['_rnPrefixes'][0][0]
else:
return ''
def getClassName(pyClassDict):
return pyClassDict['_name']
def getRnFormat(pyClassDict):
return pyClassDict['_rnFormat']
def getReadOnly(pyClassDict):
return 'true' if pyClassDict['_isReadOnly'] else 'false'
def getRubyClassMap(classMap):
rubyCode = Template("""# auto-generated code
module ACIrb
CLASSMAP = Hash.new 'None'
$classMap
def lookupClass(classname)
return CLASSMAP[classname]
end
end
""")
vals = dict(
classMap='\n '.join(
['CLASSMAP[\'{0}\'] = \'{1}\''.format(k, v) for k, v in classMap.items()]),
)
return rubyCode.substitute(vals)
def getRubyClass(pyClassDict):
rubyCode = Template(""" class $rubyClassName < MO
@class_name = '$objectName'
@ruby_class = '$rubyClassName'
@prefix = '$prefix'
@prefixes = [$prefixes]
@rn_format = '$rnFormat'
@containers = $containers
@props = $props
@child_classes = $children
@label = '$label'
@naming_props = $namingProps
@read_only = $readOnly
def rn
$rn
end
end
""")
vals = dict(rubyClassName=rubyClassName(pyClassDict['_name']),
objectName=getClassName(pyClassDict),
prefix=getRnPrefix(pyClassDict),
prefixes=getRnPrefixes(pyClassDict),
rnFormat=getRnFormat(pyClassDict),
containers=getContainers(pyClassDict),
props=getProps(pyClassDict),
children=getChildren(pyClassDict),
rn=getRnFunc(pyClassDict),
label=getLabel(pyClassDict),
namingProps=getNamingProps(pyClassDict),
classNameShort=getClassName(pyClassDict).replace('.', ''),
readOnly=getReadOnly(pyClassDict),
)
return rubyCode.substitute(vals)
def getRubyPackage(classDef):
rubyCode = Template("""# auto-generated code
require 'acirb/mo'
module ACIrb
$rubyClasses
end
""")
vals = dict(rubyClasses=classDef,
)
return rubyCode.substitute(vals)
def getRubyAutoLoad(autoLoaderMap):
rubyAutoLoad = '\n'.join([' ACIrb.autoload(\'{0}\', \'acirb/model/{1}\')'.format(
rubyClass, rubyFile) for rubyClass, rubyFile in autoLoaderMap.items()])
rubyCode = Template("""# auto-generated code
module ACIrb
$rubyAutoLoad
end
""")
vals = dict(rubyAutoLoad=rubyAutoLoad,
)
return rubyCode.substitute(vals)
def prettyprint(item, depth=0):
if getattr(item, '__dict__', None):
prettyprint(item.__dict__, depth + 1)
elif isinstance(item, list):
for i in item:
print '-' * depth, type(i), i
elif isinstance(item, set):
for i in item:
print '-' * depth, type(i), i
elif isinstance(item, dict):
for k, v in item.items():
print '-' * depth, k, ':'
prettyprint(v, depth + 1)
else:
print '-' * depth, type(item), item
def generateRuby(classdir):
classMap = OrderedDict()
packages = OrderedDict()
'''
packages will be a dict of format:
{
packageName: (pkgCode (str), pkgClasses (list))
}
'''
for item in classdir.getClasses():
pkgName = getClassName(item.__dict__).split('.')[0]
className = getClassName(item.__dict__).replace('.', '')
rubyName = rubyClassName(item.__dict__['_name'])
classMap[className] = rubyName
pkgCode, pkgClasses = packages.get(pkgName, ('', []))
pkgCode = pkgCode + getRubyClass(item.__dict__)
pkgClasses.append(rubyName)
packages[pkgName] = (pkgCode, pkgClasses)
autoLoaderMap = OrderedDict()
directory = os.path.join(
os.path.abspath(
os.path.dirname(__file__)),
'lib', 'acirb', 'model')
if not os.path.exists(directory):
os.makedirs(directory)
for pkgname, payload in packages.items():
pkgCode, pkgClasses = payload
for pkgClass in pkgClasses:
autoLoaderMap[pkgClass] = '{0}.rb'.format(pkgname)
fileName = os.path.join(
os.path.abspath(
os.path.dirname(__file__)),
'lib', 'acirb', 'model', pkgname + '.rb')
# Create the package
with open(fileName, 'w') as f:
r = getRubyPackage(pkgCode)
f.write(r)
# Create the lookup map
fileName = os.path.join(
os.path.abspath(
os.path.dirname(__file__)),
'lib', 'acirb', 'lookup.rb')
with open(fileName, 'w') as f:
f.write(getRubyClassMap(classMap))
# Create the autoloader
fileName = os.path.join(
os.path.abspath(
os.path.dirname(__file__)),
'lib', 'acirb', 'autoloadmap.rb')
with open(fileName, 'w') as f:
f.write(getRubyAutoLoad(autoLoaderMap))
def main():
start = time.time()
classdir = PyClassDirectory()
generateRuby(classdir)
print 'Completed in %.2f seconds' % (time.time() - start)
if __name__ == '__main__':
main()