Skip to content

Commit 8a9aac2

Browse files
committed
more reliable progress tracking
1 parent 0a3bceb commit 8a9aac2

3 files changed

Lines changed: 163 additions & 147 deletions

File tree

‎lib/obf/external.rb‎

Lines changed: 82 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -41,81 +41,85 @@ def self.to_obf(hash, dest_path, path_hash=nil, to_include=nil)
4141
buttons = hash['buttons'] #board.settings['buttons']
4242
button_count = buttons.length
4343

44-
buttons.each_with_index do |original_button, idx|
45-
button = {
46-
'id' => original_button['id'],
47-
'label' => original_button['label'],
48-
'vocalization' => original_button['vocalization'],
49-
'action' => original_button['action'],
50-
'actions' => original_button['actions'],
51-
'left' => original_button['left'],
52-
'top' => original_button['top'],
53-
'width' => original_button['width'],
54-
'height' => original_button['height'],
55-
'border_color' => OBF::Utils.fix_color(original_button['border_color'] || "#aaa", 'rgb'),
56-
'background_color' => OBF::Utils.fix_color(original_button['background_color'] || "#fff", 'rgb')
57-
}
58-
if original_button['load_board']
59-
button['load_board'] = {
60-
'id' => original_button['load_board']['id'],
61-
'url' => original_button['load_board']['url'],
62-
'data_url' => original_button['load_board']['data_url']
44+
OBF::Utils.as_progress_percent(0.0, 0.3) do
45+
buttons.each_with_index do |original_button, idx|
46+
button = {
47+
'id' => original_button['id'],
48+
'label' => original_button['label'],
49+
'vocalization' => original_button['vocalization'],
50+
'action' => original_button['action'],
51+
'actions' => original_button['actions'],
52+
'left' => original_button['left'],
53+
'top' => original_button['top'],
54+
'width' => original_button['width'],
55+
'height' => original_button['height'],
56+
'border_color' => OBF::Utils.fix_color(original_button['border_color'] || "#aaa", 'rgb'),
57+
'background_color' => OBF::Utils.fix_color(original_button['background_color'] || "#fff", 'rgb')
6358
}
64-
if path_hash && path_hash['included_boards'] && path_hash['included_boards'][original_button['load_board']['id']]
65-
button['load_board']['path'] = "board_#{original_button['load_board']['id']}.obf"
59+
if original_button['load_board']
60+
button['load_board'] = {
61+
'id' => original_button['load_board']['id'],
62+
'url' => original_button['load_board']['url'],
63+
'data_url' => original_button['load_board']['data_url']
64+
}
65+
if path_hash && path_hash['included_boards'] && path_hash['included_boards'][original_button['load_board']['id']]
66+
button['load_board']['path'] = "board_#{original_button['load_board']['id']}.obf"
67+
end
6668
end
67-
end
68-
if original_button['translations']
69-
original_button['translations'].each do |loc, hash|
70-
next unless hash.is_a?(Hash)
71-
button['translations'] ||= {}
72-
button['translations'][loc] ||= {}
73-
button['translations'][loc]['label'] = hash['label'].to_s if hash['label']
74-
button['translations'][loc]['vocalization'] = hash['vocalization'].to_s if hash['vocalization']
75-
(hash['inflections'] || {}).each do |key, val|
76-
if key.match(/^ext_/)
77-
button['translations'][loc]['inflections'] ||= {}
78-
button['translations'][loc]['inflections'][key] = val
79-
else
80-
button['translations'][loc]['inflections'] ||= {}
81-
button['translations'][loc]['inflections'][key] = val.to_s
69+
if original_button['translations']
70+
original_button['translations'].each do |loc, hash|
71+
next unless hash.is_a?(Hash)
72+
button['translations'] ||= {}
73+
button['translations'][loc] ||= {}
74+
button['translations'][loc]['label'] = hash['label'].to_s if hash['label']
75+
button['translations'][loc]['vocalization'] = hash['vocalization'].to_s if hash['vocalization']
76+
(hash['inflections'] || {}).each do |key, val|
77+
if key.match(/^ext_/)
78+
button['translations'][loc]['inflections'] ||= {}
79+
button['translations'][loc]['inflections'][key] = val
80+
else
81+
button['translations'][loc]['inflections'] ||= {}
82+
button['translations'][loc]['inflections'][key] = val.to_s
83+
end
84+
end
85+
hash.keys.each do |key|
86+
button['translations'][loc][key] = hash[key] if key.to_s.match(/^ext_/)
8287
end
83-
end
84-
hash.keys.each do |key|
85-
button['translations'][loc][key] = hash[key] if key.to_s.match(/^ext_/)
8688
end
8789
end
88-
end
89-
if original_button['hidden']
90-
button['hidden'] = original_button['hidden']
91-
end
92-
if original_button['url']
93-
button['url'] = original_button['url']
94-
end
95-
original_button.each do|key, val|
96-
if key.match(/^ext_/)
97-
button[key] = val
90+
if original_button['hidden']
91+
button['hidden'] = original_button['hidden']
92+
end
93+
if original_button['url']
94+
button['url'] = original_button['url']
95+
end
96+
original_button.each do|key, val|
97+
if key.match(/^ext_/)
98+
button[key] = val
99+
end
98100
end
99-
end
100101

101-
if original_button['image_id'] && hash['images']
102-
image = hash['images'].detect{|i| i['id'] == original_button['image_id']}
103-
if image
104-
images << image
105-
button['image_id'] = image['id']
102+
if original_button['image_id'] && hash['images']
103+
image = hash['images'].detect{|i| i['id'] == original_button['image_id']}
104+
if image
105+
images << image
106+
button['image_id'] = image['id']
107+
end
106108
end
107-
end
108-
if original_button['sound_id']
109-
sound = hash['sounds'].detect{|s| s['id'] == original_button['sound_id']}
110-
if sound
111-
sounds << sound
112-
button['sound_id'] = sound['id']
109+
if original_button['sound_id']
110+
sound = hash['sounds'].detect{|s| s['id'] == original_button['sound_id']}
111+
if sound
112+
sounds << sound
113+
button['sound_id'] = sound['id']
114+
end
113115
end
116+
res['buttons'] << trim_empties(button)
117+
OBF::Utils.update_current_progress(idx.to_f / button_count.to_f, "generated button #{button['id']} for #{res['id']}")
114118
end
115-
res['buttons'] << trim_empties(button)
116-
OBF::Utils.update_current_progress(idx.to_f / button_count.to_f)
117119
end
118120

121+
OBF::Utils.update_current_progress(0.35, "images for board #{res['id']}")
122+
119123
# board_id 1_527892 has some svg's still, not pre-rasterized versions
120124
# 1_531854 convert-im6.q16: non-conforming drawing primitive definition `Helvetica''' @ error/draw.c/DrawImage/3265.
121125
if to_include[:images]
@@ -215,6 +219,7 @@ def self.to_obf(hash, dest_path, path_hash=nil, to_include=nil)
215219

216220
end
217221

222+
OBF::Utils.update_current_progress(0.75, "sounds for board #{res['id']}")
218223
if to_include[:sounds]
219224
sounds.each do |original_sound|
220225
sound = {
@@ -266,6 +271,8 @@ def self.to_obf(hash, dest_path, path_hash=nil, to_include=nil)
266271
end
267272
end
268273

274+
OBF::Utils.update_current_progress(0.85, "grid for board #{res['id']}")
275+
269276
res['grid'] = OBF::Utils.parse_grid(hash['grid']) # TODO: more robust parsing here
270277
if path_hash && path_hash['zip']
271278
zip_path = "board_#{res['id']}.obf"
@@ -278,6 +285,7 @@ def self.to_obf(hash, dest_path, path_hash=nil, to_include=nil)
278285
File.open(dest_path, 'w') {|f| f.write(JSON.pretty_generate(res)) }
279286
end
280287
OBF::Utils.log(" done compressing board #{res['name'] || res['id']}")
288+
OBF::Utils.update_current_progress(1.0, "done for board #{res['id']}")
281289
return dest_path
282290
end
283291

@@ -333,19 +341,24 @@ def self.to_obz(content, dest_path, opts)
333341
content['images'] ||= boards.map{|b| b['images'] }.flatten.uniq
334342
content['sounds'] ||= boards.map{|b| b['sounds'] }.flatten.uniq
335343
root_board = boards[0]
344+
incr = 1.0 / boards.length.to_f
345+
tally = 0.0
336346
OBF::Utils.build_zip(dest_path) do |zipper|
337347
paths['zip'] = zipper
338348
paths['included_boards'] = {}
339349
boards.each do |b|
340350
paths['included_boards'][b['id']] = b
341351
end
342352
boards.each do |b|
343-
b = paths['included_boards'][b['id']]
344-
if b
345-
b['images'] = content['images'] || []
346-
b['sounds'] = content['sounds'] || []
347-
to_obf(b, nil, paths, opts[:to_include])
353+
OBF::Utils.as_progress_percent(tally, tally + incr) do
354+
b = paths['included_boards'][b['id']]
355+
if b
356+
b['images'] = content['images'] || []
357+
b['sounds'] = content['sounds'] || []
358+
to_obf(b, nil, paths, opts[:to_include])
359+
end
348360
end
361+
tally += incr
349362
end
350363
manifest = {
351364
'format' => OBF::OBF::FORMAT,
@@ -424,7 +437,7 @@ def self.to_pdf(board, dest_path, opts)
424437
end
425438
tmp_path = OBF::Utils.temp_path("stash")
426439
if opts['packet']
427-
OBF::Utils.as_progress_percent(0, 0.3) do
440+
OBF::Utils.as_progress_percent(0.1, 0.3) do
428441
opts[:to_include] = {images: true}
429442
OBF::External.to_obz(board, tmp_path, opts)
430443
end

0 commit comments

Comments
 (0)