forked from datacenter/acirb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
genrubyfrompy.rb
executable file
·319 lines (271 loc) · 8.25 KB
/
genrubyfrompy.rb
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env ruby
# frozen_string_literal: true
#
# Generates the Ruby models from the Python SDK pulled from the APIC
#
# SPDX-FileCopyrightText: 2023 Georg Gadinger <[email protected]>
# SPDX-License-Identifier: Apache-2.0
require "fileutils"
require "set"
# transforms the relevant bits from an ACI python sdk model to a Ruby model
# the output of a class should be source-compatible to the output of the old
# genrubyfrompy.py script
class PyTonRuby
def initialize(python_code)
@python_code = python_code
@class_name = nil
@class_meta = nil
@current_prop = nil
@props = {}
@rn_prefixes = []
@ordered_naming_props = []
end
def transform
return unless @python_code
@python_code
.lines
# get rid of last \ns
.map(&:chomp)
# select only code that matters (should be in a `def create` block)
.select { _1.start_with?(" ") }
# ignore some control statements
.reject { _1 =~ /^\s*(if|return|def|global)\s+/ }
.each(&method(:transform_line))
@python_code = nil
end
def generate_class
<<~RUBY
class #{ruby_class_name} < MO
@class_name = #{@class_name.inspect}
@ruby_class = #{ruby_class_name.inspect}
@prefix = #{prefix.inspect}
@prefixes = #{prefixes.inspect}
@rn_format = #{rn_format.inspect}
@containers = #{containers.inspect}
@props = { #{formatted_props} }
@child_classes = #{children.inspect}
@label = #{label.inspect}
@naming_props = #{naming_props.inspect}
@read_only = #{read_only.inspect}
def rn
#{rn}
end
end
RUBY
end
def class_name
@class_name
end
def ruby_class_name(class_name = @class_name)
class_name.tr(".", "").sub(/^./, &:upcase)
end
def prefix
return "" if @rn_prefixes.empty?
@rn_prefixes.first.first
end
def prefixes
@rn_prefixes
end
def rn_format
@class_meta.fetch("rnFormat", "")
end
def containers
@class_meta.fetch("containers", []).map(&method(:ruby_class_name))
end
def props
@props
end
# formats the properties in a way the python version did
def formatted_props
props.map do |name, flags|
formatted_flags = flags
.inspect
.gsub("=>", " => ")
.gsub("{", "{ ")
.gsub("}", " }")
"#{name.inspect} => #{formatted_flags}"
end.join(",\n ")
end
def children
@class_meta.fetch("children", []).map(&method(:ruby_class_name))
end
def label
@class_meta.fetch("label", "")
end
def naming_props
@ordered_naming_props
end
def read_only
@class_meta.fetch("isReadOnly", false)
end
def rn
rn_format.inspect.gsub(/%\(([a-zA-Z]+)\)s/, %q(#{@attributes['\1']}))
end
private
PROPS_OF_INTEREST = %w[
isAdmin isImplicit isCreateOnly isDn isRn isExplicit
].freeze
private_constant :PROPS_OF_INTEREST
CLASS_PROPS_OF_INTEREST = %w[
rnFormat label isReadOnly
].freeze
private_constant :CLASS_PROPS_OF_INTEREST
def transform_line(line)
case line
when %r{= Py.*ClassMeta\('(?<class_name>[^']+)'}
# init class
raise ArgumentError, "class was already set!" if @class_name
@class_name = Regexp.last_match[:class_name]
@class_meta = {}
when %r{prop = PyPropMeta}
# init property
raise ArgumentError, "property was already set!" if @current_prop
@current_prop = PROPS_OF_INTEREST.map { [_1, false] }.to_h
when %r{prop._(?<prop_name>\S+) = (?<value>.+)}
# normal property
prop_name = Regexp.last_match[:prop_name]
return unless PROPS_OF_INTEREST.include?(prop_name)
value = Regexp.last_match[:value]
@current_prop[prop_name] = python_to_ruby value
when %r{_classMeta._props\['(?<prop_name>[^']+)'\] =}
# finalise property and reset
prop_name = Regexp.last_match[:prop_name]
@props[prop_name] = @current_prop
@current_prop = nil
when %r{_classMeta._(?<kind>children|containers)\[(?<class_name>[^\]]+)\]}
# child class or containers, we only need the name
kind = Regexp.last_match[:kind]
class_name = Regexp.last_match[:class_name]
@class_meta[kind] ||= Set.new
@class_meta[kind] << python_to_ruby(class_name)
when %r{_classMeta._(?<prop_name>\S+) = (?<value>.+)}
# class property
prop_name = Regexp.last_match[:prop_name]
return unless CLASS_PROPS_OF_INTEREST.include?(prop_name)
value = Regexp.last_match[:value]
@class_meta[prop_name] = python_to_ruby value
when %r{_classMeta._rnPrefixes.append\((?<prefixes>.+)\)$}
prefixes = Regexp.last_match[:prefixes]
@rn_prefixes << python_to_ruby(prefixes)
when %r{_classMeta._orderedNamingProps.append\((?<naming_prop>.+)\)$}
naming_prop = Regexp.last_match[:naming_prop]
@ordered_naming_props << python_to_ruby(naming_prop)
# else puts "no match for #{line}"
end
rescue
puts "====================\nfailed at line:\n#{line}\n===================="
raise
end
def python_to_ruby(value)
case value.strip
when "True"
true
when "False"
false
when "None"
nil
when /^(?<hex>0x[0-9a-f]+)/i
Integer(Regexp.last_match[:hex])
when /^\((?<ary>.+)\)$/
Regexp.last_match[:ary].split(',').map(&method(:python_to_ruby))
else
yolo value
end
end
alias yolo eval
end
# small helper class for printing the current status to see what's going on
class MiniProgress
def initialize(size)
@size = size
end
def progress(value, text)
percentage = value.to_f / @size * 100
print "[%3d%%] #{text}\r" % [percentage]
yield
print "[%3d%%] #{' ' * text.size}\r" % [percentage]
end
def done(text)
puts "[100%] #{text}"
end
end
###############################################################################
models = Dir["./pysdk/insieme/model/*/*.py"]
.reject { _1.end_with?("__init__.py") }
parsed_models = {}
$stdout.sync = true
start_time = Time.now
progress = MiniProgress.new(models.size)
models.each_with_index do |path, i|
progress.progress(i.succ, "Parsing #{path}") do
model = PyTonRuby.new(File.read(path)).tap(&:transform)
parsed_models[path] = model
end
end
end_time = Time.now
progress.done("Parsing done in %.4f seconds" % [end_time - start_time])
puts "creating Ruby models..."
# group models by package
packages = {}
progress = MiniProgress.new(parsed_models.size)
parsed_models.values.each.with_index do |model, i|
progress.progress(i.succ, "Grouping #{model.class_name}") do
package_name = model.class_name.split(".").first
packages[package_name] ||= {}
packages[package_name][model.ruby_class_name] = model.generate_class
end
end
progress.done "Grouped models by package"
# write models by package
FileUtils.mkdir_p "./lib/acirb/model"
progress = MiniProgress.new(packages.size)
packages.each_with_index do |(package_name, package_contents), i|
progress.progress(i.succ, "Creating #{package_name}.rb") do
File.open("./lib/acirb/model/#{package_name}.rb", "w") do |f|
f.puts <<~RUBY
# auto-generated code
require "acirb/mo"
module ACIrb
RUBY
package_contents.values.each do |code|
f.puts code.gsub(/^/m, ' ')
end
f.puts "end"
end
end
end
progress.done "Created Ruby models"
puts "creating lookup map..."
File.open("./lib/acirb/lookup.rb", "w") do |f|
class_map = parsed_models.values.map do |model|
key = model.class_name.tr('.', '').inspect
value = model.ruby_class_name.inspect
%(CLASSMAP[#{key}] = #{value})
end
f.puts <<~RUBY
# auto-generated code
module ACIrb
CLASSMAP = Hash.new 'None'
#{class_map.join("\n ")}
def lookupClass(classname)
return CLASSMAP[classname]
end
end
RUBY
end
puts "creating autoload map..."
File.open("./lib/acirb/autoloadmap.rb", "w") do |f|
f.puts <<~RUBY
# auto-generated code
module ACIrb
RUBY
packages.each do |package_name, package_contents|
package_contents.keys.each do |ruby_class_name|
autoload_path = "acirb/model/#{package_name}.rb"
f.puts %( ACIrb.autoload(#{ruby_class_name.inspect}, #{autoload_path.inspect}))
end
end
f.puts "end"
end
total_time = Time.now - start_time
puts "🎉 generating done, total time spent: %.4f seconds" % [total_time]