Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

New method MiniExiftool.opts for manupilationg the standard opts; enc…

…oding conversion using the -L-option of the exiftool command-line application.
  • Loading branch information...
commit 513de6a149f198b5d2f0cef92fa3cdf84b5e3389 1 parent fe62775
Jan Friedrich authored
View
8 Changelog
@@ -1,3 +1,11 @@
+Version 0.7.0
+- Changing composite behaviour: Composite tags are now included as standard!
+- New method MiniExiftool.opts which returns a hash of the standard
+ options used for MiniExiftool.new
+- New option :convert_encoding for MiniExiftool.new which uses the -L-option
+ of the exiftool command-line application (see online documentation for it)
+ Thanks to Henning Kulander for the causing of this change.
+
Version 0.6.0
- New methods for serialization:
- MiniExiftool.from_hash
View
40 lib/mini_exiftool.rb
@@ -7,7 +7,7 @@
# Read and write access is done in a clean OO manner.
#
# Author: Jan Friedrich
-# Copyright (c) 2007 by Jan Friedrich
+# Copyright (c) 2007, 2008 by Jan Friedrich
# Licensed under the GNU LESSER GENERAL PUBLIC LICENSE,
# Version 2.1, February 1999
#
@@ -24,15 +24,20 @@ class MiniExiftool
# Name of the Exiftool command-line application
@@cmd = 'exiftool'
+ # Hash of the standard options used when call MiniExiftool.new
+ @@opts = { :numerical => false, :composite => true, :convert_encoding => false, :timestamps => Time }
+
attr_reader :filename
- attr_accessor :numerical, :composite, :errors, :timestamps
+ attr_accessor :numerical, :composite, :convert_encoding, :errors, :timestamps
- VERSION = '0.6.0'
+ VERSION = '0.7.0'
# +opts+ support at the moment
# * <code>:numerical</code> for numerical values, default is +false+
# * <code>:composite</code> for including composite tags while loading,
- # default is +false+
+ # default is +true+
+ # * <code>:convert_encoding</code> convert encoding (See -L-option of
+ # the exiftool command-line application, default is +false+
# * <code>:timestamps</code> generating DateTime objects instead of
# Time objects if set to <code>DateTime</code>, default is +Time+
#
@@ -40,10 +45,10 @@ class MiniExiftool
# therefore they use <em>your local timezone</em>, DateTime objects instead
# are created <em>without timezone</em>!
def initialize filename=nil, opts={}
- std_opts = {:numerical => false, :composite => false, :timestamps => Time}
- opts = std_opts.update opts
+ opts = @@opts.merge opts
@numerical = opts[:numerical]
@composite = opts[:composite]
+ @convert_encoding = opts[:convert_encoding]
@timestamps = opts[:timestamps]
@values = TagHash.new
@tag_names = TagHash.new
@@ -75,6 +80,7 @@ def load filename
opt_params = ''
opt_params << (@numerical ? '-n ' : '')
opt_params << (@composite ? '' : '-e ')
+ opt_params << (@convert_encoding ? '-L ' : '')
cmd = %Q(#@@cmd -q -q -s -t #{opt_params} "#{filename}")
if run(cmd)
parse_output
@@ -143,7 +149,9 @@ def save
@changed_values.each do |tag, val|
original_tag = MiniExiftool.original_tag(tag)
converted_val = convert val
- opt_params = converted_val.kind_of?(Numeric) ? '-n' : ''
+ opt_params = ''
+ opt_params << (converted_val.kind_of?(Numeric) ? '-n ' : '')
+ opt_params << (@convert_encoding ? '-L ' : '')
cmd = %Q(#@@cmd -q -P -overwrite_original #{opt_params} -#{original_tag}="#{converted_val}" "#{temp_filename}")
result = run(cmd)
unless result
@@ -198,6 +206,11 @@ def self.command= cmd
@@cmd = cmd
end
+ # Returns the options hash.
+ def self.opts
+ @@opts
+ end
+
# Returns a set of all known tags of Exiftool.
def self.all_tags
unless defined? @@all_tags
@@ -320,15 +333,10 @@ def set_value tag, value
end
def set_attributes_by_heuristic
- if tags.include? 'ImageSize'
- self.composite = true
- end
- if self.file_size.kind_of? Integer
- self.numerical = true
- end
- if self.FileModifyDate.kind_of? DateTime
- self.timestamps = DateTime
- end
+ self.composite = tags.include?('ImageSize') ? true : false
+ self.numerical = self.file_size.kind_of?(Integer) ? true : false
+ # TODO: Is there a heuristic to determine @convert_encoding?
+ self.timestamps = self.FileModifyDate.kind_of?(DateTime) ? DateTime : Time
end
def temp_filename
View
BIN  test/data/test.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
18 test/test_class_methods.rb
@@ -49,6 +49,24 @@ def test_command
MiniExiftool.command = cmd
end
+ def test_opts
+ opts = MiniExiftool.opts
+ assert_kind_of Hash, opts
+ begin
+ org = MiniExiftool.opts[:composite]
+ met1 = MiniExiftool.new
+ MiniExiftool.opts[:composite] = !org
+ met2 = MiniExiftool.new
+ MiniExiftool.opts[:composite] = org
+ met3 = MiniExiftool.new
+ assert_equal org, met1.composite
+ assert_equal !org, met2.composite
+ assert_equal org, met1.composite
+ ensure
+ MiniExiftool.opts[:composite] = org
+ end
+ end
+
def test_all_tags
all_tags = MiniExiftool.all_tags
assert all_tags.include?('ISO')
View
4 test/test_composite.rb
@@ -5,8 +5,8 @@ class TestComposite < TestCase
def setup
@data_dir = File.dirname(__FILE__) + '/data'
@filename_test = @data_dir + '/test.jpg'
- @mini_exiftool = MiniExiftool.new @filename_test
- @mini_exiftool_c = MiniExiftool.new @filename_test, :composite => true
+ @mini_exiftool = MiniExiftool.new @filename_test, :composite => false
+ @mini_exiftool_c = MiniExiftool.new @filename_test
end
def test_composite_tags
View
10 test/test_dumping.rb
@@ -60,11 +60,11 @@ def test_from_yaml
def test_heuristics_for_restoring_composite
standard = @mini_exiftool.to_hash
- composite = MiniExiftool.new(@filename_test, :composite => true).to_hash
- assert_equal false, MiniExiftool.from_hash(standard).composite
- assert_equal true, MiniExiftool.from_hash(composite).composite
- assert_equal false, MiniExiftool.from_yaml(standard.to_yaml).composite
- assert_equal true, MiniExiftool.from_yaml(composite.to_yaml).composite
+ no_composite = MiniExiftool.new(@filename_test, :composite => false).to_hash
+ assert_equal true, MiniExiftool.from_hash(standard).composite
+ assert_equal false, MiniExiftool.from_hash(no_composite).composite
+ assert_equal true, MiniExiftool.from_yaml(standard.to_yaml).composite
+ assert_equal false, MiniExiftool.from_yaml(no_composite.to_yaml).composite
end
def test_heuristics_for_restoring_numerical
View
6 test/test_read.rb
@@ -28,4 +28,10 @@ def test_conversion
assert_kind_of Array, @mini_exiftool['SubjectLocation']
end
+ def test_encoding_conversion
+ @mini_exiftool_converted = MiniExiftool.new @filename_test, :convert_encoding => true
+ assert_equal 'Abenddämmerung', @mini_exiftool.title
+ assert_equal "Abendd\344mmerung", @mini_exiftool_converted.title
+ end
+
end
View
13 test/test_save.rb
@@ -48,4 +48,17 @@ def test_no_changing_of_file_when_error
assert_equal @org_md5, Digest::MD5.hexdigest(File.read(@temp_filename))
end
+ def test_encoding_conversion
+ special_string = 'ÄÖÜ'
+ @mini_exiftool.title = special_string
+ assert @mini_exiftool.save
+ assert_equal false, @mini_exiftool.convert_encoding
+ assert_equal special_string, @mini_exiftool.title
+ @mini_exiftool.convert_encoding = true
+ @mini_exiftool.title = special_string
+ assert @mini_exiftool.save
+ assert_equal true, @mini_exiftool.convert_encoding
+ assert_equal special_string, @mini_exiftool.title
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.