mini_exiftool 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Changelog CHANGED
@@ -1,3 +1,16 @@
1
+ Version 0.2.0
2
+ - Better error handling (i.e. error messages)
3
+ - Checking if the exiftool command can be executed at loading the lib
4
+ - New class method exiftool_version
5
+ - Added tests
6
+ - Documentation completed
7
+
8
+ Version 0.1.2
9
+ - Bugfix for Windows (Tempfile)
10
+ Thanks to Jérome Soika for testing
11
+ - Regexes optimized (a little bit)
12
+ - New class-method MiniExiftool.writable_tags
13
+
1
14
  Version 0.1.1
2
15
  - Fixing bug [#8073]
3
16
  Handling the '-' in tag Self-timer
data/lib/mini_exiftool.rb CHANGED
@@ -13,63 +13,70 @@
13
13
  #
14
14
 
15
15
  require 'fileutils'
16
- require 'open3'
17
16
  require 'tempfile'
17
+ require 'set'
18
18
 
19
19
  # Simple OO access to the Exiftool command-line application.
20
20
  class MiniExiftool
21
21
 
22
22
  # Name of the exiftool command
23
- ProgramName = 'exiftool'
23
+ @@cmd = 'exiftool'
24
24
 
25
25
  attr_reader :filename
26
26
  attr_accessor :numerical
27
27
 
28
- VERSION = '0.1.1'
28
+ VERSION = '0.2.0'
29
29
 
30
30
  # opts at the moment only support :numerical for numerical values
31
31
  # (the -n parameter in the command line)
32
32
  def initialize filename, *opts
33
- @prog = ProgramName
34
33
  @numerical = opts.include? :numerical
35
34
  load filename
36
35
  end
37
36
 
37
+ # Load the tags of filename
38
38
  def load filename
39
- raise MiniExiftool::Error unless File.exists? filename
39
+ unless File.exists? filename
40
+ raise MiniExiftool::Error.new("File '#{filename}' does not exist.")
41
+ end
40
42
  @filename = filename
41
43
  @values = {}
42
44
  @tag_names = {}
43
45
  @changed_values = {}
44
46
  opt_params = @numerical ? '-n' : ''
45
- cmd = %Q(#@prog -e -q -q -s -t #{opt_params} "#{filename}")
47
+ cmd = %Q(#@@cmd -e -q -q -s -t #{opt_params} "#{filename}")
46
48
  if run(cmd)
47
49
  parse_output
48
50
  else
49
- raise MiniExiftool::Error
51
+ raise MiniExiftool::Error.new(@error_text)
50
52
  end
51
53
  self
52
54
  end
53
55
 
56
+ # Reload the tags of an already readed file
54
57
  def reload
55
58
  load @filename
56
59
  end
57
60
 
61
+ # Returns the value of a tag
58
62
  def [] tag
59
63
  unified_tag = unify tag
60
64
  @changed_values[unified_tag] || @values[unified_tag]
61
65
  end
62
66
 
67
+ # Set the value of a tag
63
68
  def []=(tag, val)
64
69
  unified_tag = unify tag
65
70
  converted_val = convert val
66
71
  opt_params = converted_val.kind_of?(Numeric) ? '-n' : ''
67
- cmd = %Q(#@prog -q -q -P -overwrite_original #{opt_params} -#{unified_tag}="#{converted_val}" "#{temp_filename}")
72
+ cmd = %Q(#@@cmd -q -q -P -overwrite_original #{opt_params} -#{unified_tag}="#{converted_val}" "#{temp_filename}")
68
73
  if run(cmd)
69
74
  @changed_values[unified_tag] = val
70
75
  end
71
76
  end
72
77
 
78
+ # Return true if any tag value is changed or if the value of a
79
+ # given tag is changed
73
80
  def changed? tag=false
74
81
  if tag
75
82
  @changed_values.include? tag
@@ -78,6 +85,7 @@ class MiniExiftool
78
85
  end
79
86
  end
80
87
 
88
+ # Revert all changes or the change of a given tag
81
89
  def revert tag=nil
82
90
  if tag
83
91
  unified_tag = unify tag
@@ -90,21 +98,24 @@ class MiniExiftool
90
98
  res
91
99
  end
92
100
 
101
+ # Returns an array of the tags (original tag names) of the readed file
93
102
  def tags
94
103
  @values.keys.map { |key| @tag_names[key] }
95
104
  end
96
105
 
106
+ # Returns an array of all changed tags
97
107
  def changed_tags
98
108
  @changed_values.keys.map { |key| @tag_names[key] }
99
109
  end
100
110
 
111
+ # Save the changes to the file
101
112
  def save
102
113
  result = false
103
114
  @changed_values.each do |tag, val|
104
115
  unified_tag = unify tag
105
116
  converted_val = convert val
106
117
  opt_params = converted_val.kind_of?(Numeric) ? '-n' : ''
107
- cmd = %Q(#@prog -q -q -P -overwrite_original #{opt_params} -#{unified_tag}="#{converted_val}" "#{filename}")
118
+ cmd = %Q(#@@cmd -q -q -P -overwrite_original #{opt_params} -#{unified_tag}="#{converted_val}" "#{filename}")
108
119
  run(cmd)
109
120
  result = true
110
121
  end
@@ -112,12 +123,56 @@ class MiniExiftool
112
123
  result
113
124
  end
114
125
 
126
+ # Returns the command name of the called Exiftool application
127
+ def self.command
128
+ @@cmd
129
+ end
130
+
131
+ # Setting the command name of the called Exiftool application
132
+ def self.command= cmd
133
+ @@cmd = cmd
134
+ end
135
+
136
+ @@writable_tags = Set.new
137
+
138
+ # Returns a set of all possible writable tags of Exiftool
139
+ def self.writable_tags
140
+ if @@writable_tags.empty?
141
+ lines = `#{@@cmd} -listw`
142
+ @@writable_tags = Set.new
143
+ lines.each do |line|
144
+ next unless line =~ /^\s/
145
+ @@writable_tags |= line.chomp.split
146
+ end
147
+ end
148
+ @@writable_tags
149
+ end
150
+
151
+ # Returns the version of the Exiftool command-line application
152
+ def self.exiftool_version
153
+ output = `#{MiniExiftool.command} -ver 2>&1`
154
+ if $?.exitstatus == 0
155
+ return output
156
+ else
157
+ raise MiniExiftool::Error.new("Command '#{MiniExiftool.command}' not found")
158
+ end
159
+ end
160
+
115
161
  private
116
162
 
163
+ @@error_file = Tempfile.new 'errors'
164
+ @@error_file.close
165
+
117
166
  def run cmd
118
- @output = `#{cmd}`
167
+ @output = `#{cmd} 2>#{@@error_file.path}`
119
168
  @status = $?
120
- @status.exitstatus == 0
169
+ unless @status.exitstatus == 0
170
+ @error_text = File.readlines(@@error_file.path).join
171
+ return false
172
+ else
173
+ @error_text = ''
174
+ return true
175
+ end
121
176
  end
122
177
 
123
178
  def unify name
@@ -134,8 +189,8 @@ class MiniExiftool
134
189
 
135
190
  def method_missing symbol, *args
136
191
  tag_name = symbol.id2name
137
- if tag_name =~ /=$/
138
- self[tag_name.gsub(/=$/, '')] = args.first
192
+ if tag_name.sub!(/=$/, '')
193
+ self[tag_name] = args.first
139
194
  else
140
195
  self[tag_name]
141
196
  end
@@ -151,7 +206,7 @@ class MiniExiftool
151
206
  end
152
207
 
153
208
  def parse_line line
154
- if line =~ /^([^\t]+?)\t(.*)$/
209
+ if line =~ /^([^\t]+)\t(.*)$/
155
210
  tag, value = $1, $2
156
211
  case value
157
212
  when /^\d{4}:\d\d:\d\d \d\d:\d\d:\d\d$/
@@ -174,6 +229,7 @@ class MiniExiftool
174
229
  def temp_filename
175
230
  unless @temp_filename
176
231
  temp_file = Tempfile.new('mini-exiftool')
232
+ temp_file.close
177
233
  FileUtils.cp(@filename, temp_file.path)
178
234
  @temp_filename = temp_file.path
179
235
  end
@@ -183,3 +239,6 @@ class MiniExiftool
183
239
  class MiniExiftool::Error < Exception; end
184
240
 
185
241
  end
242
+
243
+ # Test if we can run the Exiftool command
244
+ MiniExiftool.exiftool_version
@@ -0,0 +1,28 @@
1
+ require 'mini_exiftool'
2
+ require 'test/unit'
3
+ begin
4
+ require 'turn'
5
+ rescue LoadError
6
+ end
7
+
8
+ class TestOther < Test::Unit::TestCase
9
+
10
+ def test_command
11
+ cmd = MiniExiftool.command
12
+ assert_equal 'exiftool', cmd
13
+ MiniExiftool.command = 'non_existend'
14
+ assert_equal 'non_existend', MiniExiftool.command
15
+ assert_raises MiniExiftool::Error do
16
+ met = MiniExiftool.new(File.join(File.dirname(__FILE__),
17
+ 'data/test.jpg'))
18
+ end
19
+ MiniExiftool.command = cmd
20
+ end
21
+
22
+ def test_writable_tags
23
+ w_tags = MiniExiftool.writable_tags
24
+ assert w_tags.include?('ISO')
25
+ assert_equal false, w_tags.include?('xxxxxx')
26
+ end
27
+
28
+ end
data/test/test_read.rb CHANGED
@@ -21,12 +21,22 @@ class TestRead < Test::Unit::TestCase
21
21
  assert_raises MiniExiftool::Error do
22
22
  MiniExiftool.new 'not_existing_file'
23
23
  end
24
+ begin
25
+ MiniExiftool.new 'not_existing_file'
26
+ rescue MiniExiftool::Error => e
27
+ assert_match /File 'not_existing_file' does not exist/, e.message
28
+ end
24
29
  end
25
30
 
26
31
  def test_wrong_file
27
32
  assert_raises MiniExiftool::Error do
28
33
  MiniExiftool.new __FILE__ # file type wich Exiftool can not handle
29
34
  end
35
+ begin
36
+ MiniExiftool.new __FILE__ # file type wich Exiftool can not handle
37
+ rescue MiniExiftool::Error => e
38
+ assert_match /Error: Unknown image type/, e.message
39
+ end
30
40
  end
31
41
 
32
42
  def test_access
data/test/test_write.rb CHANGED
@@ -12,6 +12,7 @@ class TestWrite < Test::Unit::TestCase
12
12
 
13
13
  def setup
14
14
  @temp_file = Tempfile.new('test')
15
+ @temp_file.close
15
16
  @temp_filename = @temp_file.path
16
17
  @org_filename = File.dirname(__FILE__) + '/data/test.jpg'
17
18
  FileUtils.cp(@org_filename, @temp_filename)
@@ -20,7 +21,7 @@ class TestWrite < Test::Unit::TestCase
20
21
  end
21
22
 
22
23
  def teardown
23
- @temp_file.close
24
+ @temp_file.delete
24
25
  end
25
26
 
26
27
  def test_access_existing_tags
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.1
3
3
  specification_version: 1
4
4
  name: mini_exiftool
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.1
7
- date: 2007-02-01 00:00:00 +01:00
6
+ version: 0.2.0
7
+ date: 2007-02-22 00:00:00 +01:00
8
8
  summary: A library for nice OO access to the Exiftool command-line application written by Phil Harvey.
9
9
  require_paths:
10
10
  - lib
@@ -33,6 +33,7 @@ files:
33
33
  - test/test_write.rb
34
34
  - test/test_read.rb
35
35
  - test/test_special.rb
36
+ - test/test_other.rb
36
37
  - Rakefile
37
38
  - COPYING
38
39
  - Changelog
@@ -44,6 +45,7 @@ test_files:
44
45
  - test/test_write.rb
45
46
  - test/test_read.rb
46
47
  - test/test_special.rb
48
+ - test/test_other.rb
47
49
  rdoc_options:
48
50
  - --title
49
51
  - MiniExiftool API documentation