Skip to content

Commit

Permalink
RaP captions: Fix usage of JSON.parse, use a proc to capture local vars
Browse files Browse the repository at this point in the history
  • Loading branch information
kepstin committed Jun 4, 2019
1 parent f04bf29 commit f693915
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
13 changes: 6 additions & 7 deletions record-and-playback/core/scripts/rap-caption-inbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class InvalidCaptionError < CaptionError

# Implementation

def caption_file_notify(json_filename)
caption_file_notify = proc do |json_filename|
# There's a possible race condition where we can be notified twice for a new
# file. That's fine, just do nothing the second time.
return unless File.exist?(json_filename)
Expand All @@ -67,7 +67,7 @@ def caption_file_notify(json_filename)
# queue job (resque?) that does the actual work.

captions_work_base = File.join(props['recording_dir'], 'caption', 'inbox')
new_caption_info = File.open(json_filename) { |file| JSON.parse(file) }
new_caption_info = File.open(json_filename) { |file| JSON.parse(file.read) }
record_id = new_caption_info['record_id']
logger.tag(record_id: record_id) do
begin
Expand All @@ -76,7 +76,7 @@ def caption_file_notify(json_filename)
index_filename = File.join(captions_dir, record_id, 'captions.json')
captions_info =
begin
File.open(index_filename) { |file| JSON.parse(file) }
File.open(index_filename) { |file| JSON.parse(file.read) }
rescue StandardError
# No captions file or cannot be read, assume none present
[]
Expand Down Expand Up @@ -117,8 +117,7 @@ def caption_file_notify(json_filename)

# Finally, save the updated index file that references the new caption
File.open(index_filename, 'w') do |file|
result = JSON.pretty_generate(captions_info)
file.write(result)
file.write(JSON.pretty_generate(captions_info))
end

caption_scripts = File.glob(File.expand_path('captions/*', __dir__))
Expand Down Expand Up @@ -150,12 +149,12 @@ def caption_file_notify(json_filename)
notifier.watch(captions_inbox_dir, :moved_to, :create) do |event|
next unless event.name.end_with?('-track.json')

handle_caption_file(event.absolute_name)
caption_file_notify.call(event.absolute_name)
end

logger.info('Checking for missed/skipped caption files')
Dir.glob(File.join(captions_inbox_dir, '*-track.json')).each do |filename|
caption_file_notify(filename)
caption_file_notify.call(filename)
end

logger.info('Waiting for new caption files...')
Expand Down
4 changes: 2 additions & 2 deletions record-and-playback/presentation/scripts/caption/presentation
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ begin
logger.info('Loading recording playback captions list')
playback_captions_path = File.join(publish_dir, recording_id, 'captions.json')
playback_captions = File.open(playback_captions_path) do |json|
JSON.parse(json)
JSON.parse(json.read)
end
rescue Errno::ENOENT
logger.info("Playback doesn't have a captions.json - old playback format version?")
Expand All @@ -87,7 +87,7 @@ begin
logger.info('Loading captions index file')
captions_path = File.join(captions_dir, recording_id, 'captions.json')
captions = File.open(captions_path) do |json|
JSON.parse(json)
JSON.parse(json.read)
end
rescue Errno::ENOENT
captions = []
Expand Down

0 comments on commit f693915

Please sign in to comment.