class ExifTagger::TagWriter

batch EXIF tags setter

Constants

DEFAULT_OPTIONS

Attributes

added_files_count[R]
script_name[R]

Public Class Methods

new(script_name: 'exif_tagger.txt', memo: 'Generated by phtools', output: STDOUT, err: STDERR) click to toggle source
# File lib/phtools/exif_tagger/tag_writer.rb, line 15
def initialize(script_name: 'exif_tagger.txt',
               memo: 'Generated by phtools',
               output: STDOUT, err: STDERR)
  @script_name = script_name
  create_script(memo)
  @added_files_count = 0
  @output = output
  @err = err
  @output.puts "*** Preparing exiftool script '#{@script_name}' ..."
end

Public Instance Methods

add_to_script(filename: '', tags: {}, options: DEFAULT_OPTIONS) click to toggle source
# File lib/phtools/exif_tagger/tag_writer.rb, line 26
def add_to_script(filename: '', tags: {}, options: DEFAULT_OPTIONS)
  @script.puts "# **(#{@added_files_count + 1})** Processing file: #{filename} *****"
  # tags
  tags.each do |k|
    @script.puts tags.item(k).to_write_script
  end
  # file to be altered
  @script.puts filename
  # General options
  options.each { |o| @script.puts o }
  @script.puts %(-execute)
  @script.puts
  @added_files_count += 1

rescue => e
  raise WriteTag, "adding item to exiftool script - #{e.message}"
end
close_script() click to toggle source
# File lib/phtools/exif_tagger/tag_writer.rb, line 44
def close_script
  @script.close
  @output.puts "*** Finished preparation of the script '#{script_name}'"
rescue => e
  raise WriteTag, "closing exiftool script - #{e.message}"
end
command() click to toggle source
# File lib/phtools/exif_tagger/tag_writer.rb, line 51
def command
  "exiftool -@ #{@script_name}"
end
run!() click to toggle source
# File lib/phtools/exif_tagger/tag_writer.rb, line 55
def run!
  close_script
  if @added_files_count.positive?
    @output.puts "*** Running #{command} ..."
    ok = system(command, out: @output, err: @err)
    fail if ok.nil?
    @output.puts "*** Finished #{command}"
  else
    @output.puts "*** Nothing to update, skip running #{command} ..."
  end
rescue
  raise WriteTag, "running #{command}"
end

Private Instance Methods

create_script(memo) click to toggle source
# File lib/phtools/exif_tagger/tag_writer.rb, line 71
def create_script(memo)
  @script = File.open(@script_name, 'w+:utf-8')
  @script.puts '# exiftool script for batch tag operations'
  @script.puts "# #{memo}"
  @script.puts "# usage: exiftool -@ #{@script_name}"
rescue => e
  raise ExifTagger::WriteTag, "creating exiftool script - #{e.message}"
end