class TRecs::TgzSource

Attributes

trecs_backend[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 14
def initialize(options={})
  backend = options.fetch(:trecs_backend)
  file = Pathname(backend).expand_path.to_s
  @trecs_backend = file
end

Public Instance Methods

[]=(key, value) click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 129
def []=(key, value)
  manifest[key.to_sym] = value
end
add_audio_file(file_name) click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 120
def add_audio_file(file_name)
  Dir.mkdir("audio") unless File.exist? "audio"

  orig_file = file_name
  file_name = "./audio/" + Pathname(file_name).basename.to_s
  FileUtils.symlink(orig_file, file_name)
  add_file(file_name)
end
add_file(file_name) click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 115
def add_file(file_name)
  @files_to_add ||= []
  @files_to_add << file_name
end
audio_files() click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 133
def audio_files
  return @audio_files if @audio_files

  in_tmp_dir do
    audiodir = tmpdir + "/audio"
    if Dir.exist?(audiodir)
      (Dir.open(audiodir).entries - %w[. ..]).map { |file|
        Pathname("./audio/" + file).expand_path
      }

    end
  end
end
build_reader(options={}) click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 44
def build_reader(options={})
  reader = nil
  options[:source] = self
  read do |source|
    json_string = source.read_entry("manifest.json")
    @manifest = JSON.parse(json_string, symbolize_names: true)

    format = manifest[:format]
    reader_file = "readers/#{format}_reader"
    require reader_file
    reader_class_name = [
      "TRecs::",
      format.split(/[-_\s]/).map(&:capitalize),
      "Reader"
    ].join
    reader_class = reader_class_name.split("::").reduce(Object) { |a, e| a.const_get e }
    reader = reader_class.new(options)
  end
  reader
end
create_file(file_name) { |f| ... } click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 108
def create_file(file_name)
  File.open(file_name, File::CREAT|File::TRUNC|File::RDWR, 0644) do |f|
    yield f
  end
  add_file(file_name)
end
create_recording() { |self| ... } click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 20
def create_recording
  in_tmp_dir do
    @tgz = Zlib::GzipWriter.new(File.open(trecs_backend, 'wb'))

    yield self

    create_file('manifest.json') do |f|
      f.write manifest.to_json
    end

    Minitar.pack(@files_to_add.flatten, @tgz)
  end
  FileUtils.rm_rf(tmpdir)
end
file_name() click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 95
def file_name
  filename = Pathname(trecs_backend)
  filename.basename.to_s.delete(filename.extname)
  name = filename.basename.to_s
  ext = filename.extname
  name[ext] = ""
  name
end
finalize() click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 79
def finalize
  Pathname(tmpdir).delete if tmpdir_exists?
end
generate_tmpdir_name() click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 87
def generate_tmpdir_name
  "#{Dir.tmpdir}/#{timestamp}-#{file_name}"
end
in_tmp_dir() { || ... } click to toggle source

Investigar como hacer para descomprimir en memoria

# File lib/trecs/sources/tgz_source.rb, line 66
def in_tmp_dir
  unless tmpdir_exists?
    Pathname(tmpdir).mkdir
  end
  Dir.chdir(tmpdir) do
    yield
  end
end
manifest() click to toggle source

TODO: Este es el punto de interseccion entre read y create_recording

# File lib/trecs/sources/tgz_source.rb, line 148
def manifest
  @manifest ||= {}
end
read() { |self| ... } click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 35
def read
  in_tmp_dir do
    tgz = Zlib::GzipReader.new(File.open(trecs_backend, 'rb'))
    Minitar.unpack(tgz, "./")

    yield self
  end
end
read_entry(file_name) click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 104
def read_entry(file_name)
  File.read(file_name)
end
timestamp() click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 91
def timestamp
  Time.now.strftime("%Y%M%d%H%m%s")
end
tmpdir() click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 83
def tmpdir
  @tmpdir ||= generate_tmpdir_name
end
tmpdir_exists?() click to toggle source
# File lib/trecs/sources/tgz_source.rb, line 75
def tmpdir_exists?
  tmpdir && Pathname(tmpdir).exist?
end