multi_exiftool 0.19.0 → 1.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c0c11e4d78ddfece60352716273188dbdb365174be7215d2df1136de2a938ab1
4
- data.tar.gz: 8258baf1ae7de0dc1c2d9b633e51f06b7b5e07417713cbcbbdba99960c591027
3
+ metadata.gz: fbd60b9c3d3d1d3a0d3aec0787ace37ee29199e96c0a86d151382af9471842ad
4
+ data.tar.gz: 93982800c2ad3fa29454af2b1597c16930d64467f7c5c28f68b24a07b471326a
5
5
  SHA512:
6
- metadata.gz: 82db0ba81f5259a4aa7f16edaac6a7cc25eefc8779f512edcdacff51fc503e8a08548be381e0c2643ebfe3357f15eda569945129d70aae81c1539ddb88ca7fbc
7
- data.tar.gz: 1155c81594420037d1e12c3086346d47ccba93fc0a3d5be0f264f1855ecc48441d2a54aee9d166735f6517e1da8b049ccb6c6f83c6e6fd4e36621972df0fee6a
6
+ metadata.gz: de743ed9fef2a208f75b27c4406894e777a0f1198cce55c377e4ab26f07df2cce839056b23811e0f7e1492647b9a193132375fee4040ca0c64cd3e44c60dbb2b
7
+ data.tar.gz: f1f729a77f830ba995f133f2681c942db81297ede87b4bae949cd644db86c24c1cf7a227c410128e477ed6d63843b91c90842aa64aa0361be025d53905c25668
data/Changelog CHANGED
@@ -1,3 +1,11 @@
1
+ 1.0.0
2
+ Better API with flexible parameters.
3
+
4
+ Breaking changes:
5
+ - Ruby 2.3 or higher is required.
6
+ - Methods that previously returned an array of error strings now return a message object instead (e.g., change `errors = writer.write; puts errors` to `messages = writer.write; puts messages.errors_or_warnings`).
7
+ - Options in `Reader` or `Writer` instances are now accessible via the options or opts attribute readers (e.g., change `reader.numerical = true` to `reader.opts.numerical = true`).
8
+
1
9
  0.19.0
2
10
  Maintenance release: Upgrade to rim 3.0 for development and minimize gem file
3
11
  size.
data/README.md CHANGED
@@ -8,6 +8,14 @@ It is designed for dealing with multiple files at once by creating
8
8
  commands to call exiftool with various arguments, call it and parsing
9
9
  the results.
10
10
 
11
+ ## Upgrade from multi_exiftool version < 1.0
12
+
13
+ - Starting with version 1.0, Ruby 2.3 or higher is required.
14
+ - Methods that previously returned an array of error strings now return a message object instead (e.g., change `errors = writer.write; puts errors` to `messages = writer.write; puts messages.errors_or_warnings`).
15
+ - Options in `Reader` or `Writer` instances are now accessible via the options or opts attribute readers (e.g., change `reader.numerical = true` to `reader.opts.numerical = true`).
16
+
17
+ See also the examples below and in the `examples` directory, as well as the test suite.
18
+
11
19
  ## Examples
12
20
 
13
21
  ### Reading
@@ -19,17 +27,18 @@ require 'multi_exiftool'
19
27
  reader = MultiExiftool::Reader.new
20
28
  reader.filenames = Dir['*.jpg']
21
29
  results = reader.read
22
- unless reader.errors.empty?
23
- $stderr.puts reader.errors
30
+ messages = reader.messages
31
+ if messages.errors_or_warnings?
32
+ $stderr.puts messages.errors_and_warnings
24
33
  end
25
34
  results.each do |values|
26
35
  puts "#{values.file_name}: #{values.comment}"
27
36
  end
28
37
 
29
38
  # Functional approach
30
- results, errors = MultiExiftool.read(Dir['*.jpg'])
31
- unless errors.empty?
32
- $stderr.puts reader.errors
39
+ results, messages = MultiExiftool.read(Dir['*.jpg'])
40
+ if messages.errors_or_warnings?
41
+ $stderr.puts messages.errors_and_warnings
33
42
  end
34
43
  results.each do |values|
35
44
  puts "#{values.file_name}: #{values.comment}"
@@ -48,15 +57,15 @@ writer.values = {creator: 'Jan Friedrich', copyright: 'Public Domain'}
48
57
  if writer.write
49
58
  puts 'ok'
50
59
  else
51
- puts writer.errors
60
+ puts writer.messages.errors_and_warnings
52
61
  end
53
62
 
54
63
  # Functional approach
55
- errors = MultiExiftool.write(Dir['*.jpg'], {creator: 'Jan Friedrich', copyright: 'Public Domain'})
56
- if errors.empty?
64
+ messages = MultiExiftool.write(Dir['*.jpg'], {creator: 'Jan Friedrich', copyright: 'Public Domain'})
65
+ if !messages.warnings_or_errors?
57
66
  puts 'ok'
58
67
  else
59
- puts writer.errors
68
+ puts messages.warnings_and_errors
60
69
  end
61
70
  ```
62
71
 
@@ -75,32 +84,32 @@ end
75
84
  if batch.execute
76
85
  puts 'ok'
77
86
  else
78
- puts batch.errors
87
+ puts batch.messages.errors_and_warnings
79
88
  end
80
89
 
81
90
  # Functional approach
82
91
 
83
- errors = MultiExiftool.batch do
92
+ messages = MultiExiftool.batch do
84
93
  Dir['*.jpg'].each_with_index do |filename, i|
85
94
  values = {creator: 'Jan Friedrich', copyright: 'Public Domain', comment: "This is file number #{i+1}."}
86
95
  write filename, values
87
96
  end
88
97
  end
89
- if errors.empty?
98
+ if !messages.errors_or_warnings?
90
99
  puts 'ok'
91
100
  else
92
- puts errors
101
+ puts messages.errors_and_warnings
93
102
  end
94
103
 
95
104
  # or alternative with block parameter as yielded Batch instance
96
105
 
97
- errors = MultiExiftool.batch do |batch|
106
+ messages = MultiExiftool.batch do |batch|
98
107
  Dir['*.jpg'].each_with_index do |filename, i|
99
108
  values = {creator: 'Jan Friedrich', copyright: 'Public Domain', comment: "This is file number #{i+1}."}
100
109
  batch.write filename, values
101
110
  end
102
111
  end
103
- if errors.empty?
112
+ if !messages.errors_or_warnings?
104
113
  puts 'ok'
105
114
  else
106
115
  puts errors
@@ -112,19 +121,19 @@ end
112
121
 
113
122
  ```ruby
114
123
  # Delete ALL values
115
- errors = MultiExiftool.delete_values(Dir['*.jpg'])
116
- if errors.empty?
124
+ messages = MultiExiftool.delete_values(Dir['*.jpg'])
125
+ if !messages.errors_or_warnings?
117
126
  puts 'ok'
118
127
  else
119
- puts writer.errors
128
+ puts messages.errors_and_warnings
120
129
  end
121
130
 
122
131
  # Delete values for tags Author and Title
123
- errors = MultiExiftool.delete_values(Dir['*.jpg'], tags: %w(author title))
124
- if errors.empty?
132
+ messages = MultiExiftool.delete_values(Dir['*.jpg'], tags: %w(author title))
133
+ if !messages.warnings_or_errors?
125
134
  puts 'ok'
126
135
  else
127
- puts writer.errors
136
+ puts messages.errors_and_warnings
128
137
  end
129
138
  ```
130
139
 
@@ -141,7 +150,7 @@ following conversions are implemented at the moment:
141
150
  * Timestamps => Time (with local time zone of no one given)
142
151
  * values of form "n/m" => Rational except PartOfSet and Track
143
152
 
144
- The conversion is done in the method Values#convert. So you can change it's
153
+ The conversion is done in the method ReaderValues#convert. So you can change it's
145
154
  behaviour as following examples show.
146
155
 
147
156
  ### Example 1
@@ -153,7 +162,7 @@ module MyConversion
153
162
  end
154
163
  end
155
164
 
156
- MultiExiftool::Values.prepend MyConversion
165
+ MultiExiftool::ReaderValues.prepend MyConversion
157
166
  ```
158
167
 
159
168
  ### Example 2
@@ -174,7 +183,7 @@ module MultiExiftool
174
183
  end
175
184
  end
176
185
 
177
- Values.prepend MyConversion
186
+ ReaderValues.prepend MyConversion
178
187
  end
179
188
  ```
180
189
 
@@ -183,17 +192,17 @@ end
183
192
  ```ruby
184
193
  m = Module.new do
185
194
  def convert tag, val
186
- if val =~ MultiExiftool::Values::REGEXP_TIMESTAMP
195
+ if val =~ MultiExiftool::ReaderValues::REGEXP_TIMESTAMP
187
196
  val # no conversion
188
197
  else
189
198
  super # use default conversion
190
199
  end
191
200
  end
192
201
  end
193
- MultiExiftool::Values.prepend m
202
+ MultiExiftool::ReaderValues.prepend m
194
203
  ```
195
204
 
196
- The method Values#convert is called each time a value is fetched.
205
+ The method ReaderValues#convert is called each time a value is fetched.
197
206
 
198
207
 
199
208
  ## Requirements
@@ -10,10 +10,10 @@ comment = $stdin.gets.chomp
10
10
 
11
11
  writer = MultiExiftool::Writer.new
12
12
  writer.filenames = ARGV
13
- writer.overwrite_original = true
13
+ writer.options.overwrite_original = true
14
14
  writer.values = {comment: comment}
15
15
  if writer.write
16
16
  puts 'ok'
17
17
  else
18
- puts writer.errors.join
18
+ puts writer.messages.warnings_and_errors.join("\n")
19
19
  end
@@ -7,7 +7,7 @@ end
7
7
 
8
8
  reader = MultiExiftool::Reader.new
9
9
  reader.filenames = ARGV
10
- reader.group = 0
10
+ reader.opts.group = 0
11
11
  results = reader.read
12
12
  results.each do |values|
13
13
  # direct access
@@ -29,5 +29,5 @@ END
29
29
  if writer.write
30
30
  puts 'ok'
31
31
  else
32
- puts writer.errors.join
32
+ puts writer.messages.warnings_and_errors.join("\n")
33
33
  end
@@ -6,10 +6,10 @@ if ARGV.empty?
6
6
  end
7
7
 
8
8
  # Reading only the tags Orientation and Rotation using the -n option of exiftool
9
- results, errors = MultiExiftool.read(ARGV, tags: %w[orientation rotation], numerical: true)
9
+ results, messages = MultiExiftool.read(ARGV, tags: %w[orientation rotation], numerical: true)
10
10
 
11
- unless errors.empty?
12
- $stderr.puts reader.errors
11
+ if messages.errors_or_warnings?
12
+ $stderr.puts reader.messages.warnings_and_errors
13
13
  exit 1
14
14
  end
15
15
 
@@ -21,15 +21,17 @@ update_orientation_to_6 = results.select {|r| r.rotation == 76 && r.orientation
21
21
  update_orientation_to_8 = results.select {|r| r.rotation == 82 && r.orientation != 8}.map {|r| r.sourcefile}
22
22
 
23
23
  # Update
24
- errors = []
24
+ errors_and_warnings = []
25
25
  unless update_orientation_to_6.empty?
26
- errors += MultiExiftool.write(update_orientation_to_6, {orientation: 6}, numerical: true, overwrite_original: true)
26
+ messages = MultiExiftool.write(update_orientation_to_6, {orientation: 6}, numerical: true, overwrite_original: true)
27
+ errors_and_warnings += messages.errors_and_warnings
27
28
  end
28
29
  unless update_orientation_to_8.empty?
29
- errors += MultiExiftool.write(update_orientation_to_8, {orientation: 8}, numerical: true, overwrite_original: true)
30
+ messages = MultiExiftool.write(update_orientation_to_8, {orientation: 8}, numerical: true, overwrite_original: true)
31
+ errors_and_warnings += messages.errors_and_warnings
30
32
  end
31
33
 
32
- unless errors.empty?
34
+ unless errors_and_warnings.empty?
33
35
  $stderr.puts reader.errors
34
36
  exit 1
35
37
  end
@@ -13,13 +13,15 @@ module MultiExiftool
13
13
 
14
14
  # Define batch operation inside a block
15
15
  def initialize &blk
16
+ super()
16
17
  @writers = []
17
18
  instance_exec &blk if block_given?
18
19
  end
19
20
 
20
21
  # Define a write operation for the batch.
21
- def write filenames, values, options={}
22
- w = MultiExiftool::Writer.new(filenames, values, options)
22
+ def write filenames=[], values=nil, **vals_and_opts
23
+ w = MultiExiftool::Writer.new(filenames, values, **vals_and_opts)
24
+ w.options = options.merge(w.options)
23
25
  @writers << w
24
26
  end
25
27
 
@@ -30,6 +32,13 @@ module MultiExiftool
30
32
  @writers.map {|w| w.exiftool_args + ['-execute']}.flatten
31
33
  end
32
34
 
35
+ private
36
+
37
+ def prepare_execution
38
+ super
39
+ fail MultiExiftool::Error if @writers.empty?
40
+ end
41
+
33
42
  end
34
43
 
35
44
  end
@@ -1,29 +1,29 @@
1
1
  # encoding: utf-8
2
2
  # frozen_string_literal: true
3
3
 
4
+ require_relative 'options'
4
5
  require 'open3'
5
6
 
7
+ require_relative 'messages'
8
+
6
9
  module MultiExiftool
7
10
 
8
11
  # Mixin for Reader and Writer.
9
12
  module Executable
10
13
 
11
- attr_reader :errors
12
- attr_accessor :config, :filenames, :numerical, :options
14
+ attr_reader :messages
15
+ attr_accessor :filenames, :options
13
16
 
14
- def initialize filenames=[], options={}
15
- @options = options
16
- @filenames = filenames
17
- @option_mapping = {numerical: :n}
18
- if val = options.delete(:config)
19
- @config = val
20
- end
17
+ def initialize filenames=[], **options
18
+ self.options = options
19
+ self.filenames = filenames
21
20
  end
22
21
 
23
- def options
24
- opts = @options.dup
25
- opts[:n] = true if @numerical
26
- opts
22
+ def options= opts
23
+ unless opts.kind_of? Options
24
+ opts = Options.new(opts)
25
+ end
26
+ @options = opts
27
27
  end
28
28
 
29
29
  def filenames= value
@@ -36,33 +36,22 @@ module MultiExiftool
36
36
  parse_results
37
37
  end
38
38
 
39
+ alias opts options
40
+
39
41
  private
40
42
 
41
43
  def exiftool_command
42
44
  MultiExiftool.exiftool_command
43
45
  end
44
46
 
45
- def options_args
46
- opts = options
47
- return [] if opts.empty?
48
- opts.map do |opt, val|
49
- arg = @option_mapping[opt] || opt
50
- if val == true
51
- "-#{arg}"
52
- else
53
- %W[-#{arg} #{val}]
54
- end
55
- end
56
- end
57
-
58
47
  def prepare_execution
59
- @errors = []
48
+ @messages = Messages.new([])
60
49
  end
61
50
 
62
51
  def execute_command
63
52
  args = ['-@', '-']
64
- if @config
65
- args = ['-config', @config] + args
53
+ if c = @options.config
54
+ args = ['-config', c] + args
66
55
  end
67
56
  stdin, @stdout, @stderr = Open3.popen3(exiftool_command, *args)
68
57
  exiftool_args.each do |part|
@@ -73,12 +62,12 @@ module MultiExiftool
73
62
  end
74
63
 
75
64
  def parse_results
76
- parse_errors
77
- @errors.empty?
65
+ parse_messages
66
+ !@messages.errors_or_warnings?
78
67
  end
79
68
 
80
- def parse_errors
81
- @errors = @stderr.read.lines(chomp: true).select {|l| l =~ /^(Error|Warning):/}
69
+ def parse_messages
70
+ @messages = Messages.new(@stderr.readlines(chomp: true) + @stdout.readlines(chomp: true))
82
71
  end
83
72
 
84
73
  end
@@ -0,0 +1,69 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module MultiExiftool
5
+
6
+ # Represents messages of different types of the execution of ExifTool calls
7
+ class Messages
8
+
9
+ attr_reader :all, :errors, :infos, :warnings
10
+
11
+ def initialize messages
12
+ @all = messages
13
+ end
14
+
15
+ def errors
16
+ @errors ||= @all.select {|m| m =~ /^Error: /}
17
+ end
18
+
19
+ def infos
20
+ @infos ||= @all.reject {|m| m =~ /^(Error|Warning): /}
21
+ end
22
+
23
+ def warnings
24
+ @warnings ||= @all.select {|m| m =~ /^Warning: /}
25
+ end
26
+
27
+ def errors_and_warnings
28
+ @errors_and_warnings ||= @all.select {|m| m =~ /^(Error|Warning): /}
29
+ end
30
+
31
+ alias warnings_and_errors errors_and_warnings
32
+
33
+ def deconstruct_keys keys
34
+ if keys.nil?
35
+ return deconstruct_keys(%i(all errors infos warnings errors_and_warnings warnings_and_errors))
36
+ end
37
+ res = {}
38
+ keys.each do |k|
39
+ if self.respond_to? k
40
+ res[k] = self.send(k)
41
+ else
42
+ return {}
43
+ end
44
+ end
45
+ return res
46
+ end
47
+
48
+ def errors?
49
+ !errors.empty?
50
+ end
51
+
52
+ def infos?
53
+ !infos.empty?
54
+ end
55
+
56
+ def warnings?
57
+ !warnings.empty?
58
+ end
59
+
60
+ def errors_or_warnings?
61
+ !errors_and_warnings.empty?
62
+ end
63
+
64
+ alias warnings_or_errors? errors_or_warnings?
65
+
66
+ end
67
+
68
+
69
+ end
@@ -0,0 +1,207 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module MultiExiftool
5
+
6
+ # Represents option for the ExifTool command-line application
7
+ class Options
8
+
9
+ # ExifTool option +-b+ (+-binary+) Output metadata in binary format
10
+ attr_accessor :b
11
+
12
+ # ExifTool option <code>-c FMT</code> (+-coordFormat+) Set format for GPS coordinates
13
+ attr_accessor :c
14
+
15
+ # ExifTool option <code>-charset exif=CHARSET</code> Specify encoding for special characters
16
+ attr_accessor :exif_charset
17
+
18
+ # ExifTool option <code>-charset id3=CHARSET</code> Specify encoding for special characters
19
+ attr_accessor :id3_charset
20
+
21
+ # ExifTool option <code>-charset iptc=CHARSE</code> Specify encoding for special characters
22
+ attr_accessor :iptc_charset
23
+
24
+ # ExifTool option <code>-charset photoshop=CHARSET</code> Specify encoding for special characters
25
+ attr_accessor :photoshop_charset
26
+
27
+ # ExifTool option <code>-charset quicktime=CHARSET</code> Specify encoding for special characters
28
+ attr_accessor :quicktime_charset
29
+
30
+ # ExifTool option <code>-charset riff=CHARSET</code> Specify encoding for special characters
31
+ attr_accessor :riff_charset
32
+
33
+ # Exiftool option +-g[NUM...]+ (+-groupHeadings+) Organize output by tag group
34
+ attr_accessor :g
35
+
36
+ # ExifTool option <code>-lang [LANG]</code> Set current language
37
+ attr_accessor :lang
38
+
39
+ # ExifTool option +-n+ (+--printConv+) No print conversion
40
+ attr_accessor :n
41
+
42
+ # ExifTool option +-sort+ Sort output alphabetically
43
+ attr_accessor :sort
44
+
45
+ # ExifTool option +-e+ (+--composite+) Do not generate composite tags
46
+ attr_accessor :e
47
+
48
+ # ExifTool option +-ee[NUM]+ (+-extractEmbedded+) Extract information from embedded files
49
+ attr_accessor :ee
50
+
51
+ # ExifTool option +-F[OFFSET]+ (+-fixBase+) Fix the base for maker notes offsets
52
+ attr_accessor :F
53
+
54
+ # ExifTool option +-fast[NUM]+ Increase speed when extracting metadata
55
+ attr_accessor :fast
56
+
57
+ # ExifTool option +-m+ (+-ignoreMinorErrors+) Ignore minor errors and warnings
58
+ attr_accessor :m
59
+
60
+ # ExifTool option <code>-o OUTFILE</code> (+-out+) Set output file or directory name
61
+ attr_accessor :o
62
+
63
+ # ExifTool option <code>-password PASSWD</code> Password for processing protected files
64
+ attr_accessor :password
65
+
66
+ # ExifTool option <code>-globalTimeShift SHIFT</code> Shift all formatted date/time values
67
+ attr_accessor :globaltimeshift
68
+
69
+ # ExifTool option <code>-config CFGFILE</code> Specify configuration file name
70
+ attr_accessor :config
71
+
72
+ # ExifTool option +-overwrite_original+ Overwrite original by renaming tmp file
73
+ attr_accessor :overwrite_original
74
+
75
+ # ExifTool option +-overwrite_original_in_place+ Overwrite original by copying tmp file
76
+ attr_accessor :overwrite_original_in_place
77
+
78
+ # ExifTool option +-P+ (+-preserve+) Preserve file modification date/time
79
+ attr_accessor :P
80
+
81
+ # ExifTool option <code>-geotag TRKFILE</code> Geotag images from specified GPS log
82
+ attr_accessor :geotag
83
+
84
+ alias :binary :b
85
+ alias :binary= :b= # :nodoc:
86
+ alias :coordformat :c
87
+ alias :coordformat= :c= # :nodoc:
88
+ alias :groupheadings :g
89
+ alias :groupheadings= :g= # :nodoc:
90
+ alias :group :g
91
+ alias :group= :g= # :nodoc:
92
+ alias :noprintconv :n
93
+ alias :noprintconv= :n= # :nodoc:
94
+ alias :numerical :n
95
+ alias :numerical= :n= # :nodoc:
96
+ alias :nocomposite :e
97
+ alias :nocomposite= :e= # :nodoc:
98
+ alias :extractembedded :ee
99
+ alias :extractembedded= :ee= # :nodoc:
100
+ alias :fixbase :F
101
+ alias :fixbase= :F= # :nodoc:
102
+ alias :ignoreminorerrors :m
103
+ alias :ignoreminorerrors= :m= # :nodoc:
104
+ alias :out :o
105
+ alias :out= :o= # :nodoc:
106
+ alias :preserve :P
107
+ alias :preserve= :P= # :nodoc:
108
+
109
+ undef p
110
+
111
+ def initialize values={}
112
+ values.each_pair do |k, v|
113
+ self.send "#{k}=", v
114
+ end
115
+ end
116
+
117
+ def [] opt
118
+ self.send(opt)
119
+ end
120
+
121
+ def []= opt, val
122
+ self.send("#{opt}=", val)
123
+ end
124
+
125
+ def merge other
126
+ Options.new(self.to_h.merge(other.to_h))
127
+ end
128
+
129
+ def to_h
130
+ res = {}
131
+ self.instance_variables.sort.each do |var|
132
+ val = instance_variable_get var
133
+ unless val.nil?
134
+ res[var.to_s.sub(/^@/, '').to_sym] = val
135
+ end
136
+ end
137
+ res
138
+ end
139
+
140
+ alias to_hash to_h
141
+
142
+ # Gets an array of the command-line arguments for this instance (internal
143
+ # use)
144
+ def options_args
145
+ args = []
146
+ to_h.each_pair do |k, v|
147
+ case k.to_s
148
+ when /^(.+)_charset$/
149
+ args << ['-charset', "#{$1}=#{v}"]
150
+ when 'g', 'ee', 'F', 'fast'
151
+ args << "-#{k}#{v}"
152
+ else
153
+ case v
154
+ when true
155
+ args << "-#{k}"
156
+ when nil, false
157
+ else
158
+ args << ["-#{k}", v]
159
+ end
160
+ end
161
+ end
162
+ args.flatten.map(&:to_s)
163
+ end
164
+
165
+ def == other
166
+ to_h == other.to_h
167
+ end
168
+
169
+ private
170
+
171
+ def method_missing meth, *args
172
+ m = try_find_real_method_name(meth)
173
+ if m
174
+ return self.send(m, *args)
175
+ end
176
+ opt = meth.to_s.sub(/=$/, '')
177
+ raise Error.new("option #{opt} is not supported")
178
+ end
179
+
180
+ def respond_to_missing? meth, *args
181
+ !try_find_real_method_name(meth).nil?
182
+ end
183
+
184
+
185
+ def try_find_real_method_name meth
186
+ m = +meth.to_s
187
+ if m =~ /^[a-zA-Z]=?$/
188
+ return nil
189
+ end
190
+ if m =~ /[A-Z]/
191
+ m.downcase!
192
+ if self.respond_to?(m)
193
+ return m
194
+ end
195
+ end
196
+ if m =~ /^[a-z]+(?:_[a-z0-9]+)+/
197
+ m.gsub!('_', '')
198
+ if self.respond_to? m
199
+ return m
200
+ end
201
+ end
202
+ nil
203
+ end
204
+
205
+ end
206
+
207
+ end
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require_relative 'executable'
5
+ require_relative 'reader_values'
5
6
  require 'json'
6
7
 
7
8
  module MultiExiftool
@@ -11,36 +12,19 @@ module MultiExiftool
11
12
  # the results as well as possible errors.
12
13
  class Reader
13
14
 
14
- attr_accessor :tags, :group
15
+ attr_accessor :tags
15
16
 
16
17
  include Executable
17
18
 
18
- def initialize filenames=[], opts={}
19
- super(filenames, opts)
20
- if val = opts.delete(:tags)
21
- @tags = val
22
- else
23
- @tags = []
24
- end
25
- if val = opts.delete(:group)
26
- @group = val
27
- end
28
- @options = opts unless opts.empty?
19
+ def initialize filenames=[], *tags_pos, tags: nil, **options
20
+ super(filenames, **options)
21
+ self.tags = tags_pos.flatten + Array(tags)
29
22
  end
30
23
 
31
24
  def self.mandatory_args
32
25
  %w(-J -charset FileName=utf8 -charset utf8)
33
26
  end
34
27
 
35
- # Options to use with the exiftool command.
36
- def options
37
- opts = super
38
- if @group
39
- opts["g#@group"] = true
40
- end
41
- opts
42
- end
43
-
44
28
  # Getting the command-line arguments which would be executed
45
29
  # when calling #read. It could be useful for logging, debugging or
46
30
  # maybe even for creating a batch-file with exiftool command to be
@@ -49,7 +33,7 @@ module MultiExiftool
49
33
  fail MultiExiftool::Error, 'No filenames.' if filenames.empty?
50
34
  cmd = []
51
35
  cmd << Reader.mandatory_args
52
- cmd << options_args
36
+ cmd << options.options_args
53
37
  cmd << tags_args
54
38
  cmd << filenames
55
39
  cmd.flatten
@@ -70,13 +54,17 @@ module MultiExiftool
70
54
 
71
55
  def parse_results
72
56
  stdout = @stdout.read
73
- parse_errors
57
+ parse_messages
74
58
  json = JSON.parse(stdout)
75
- json.map {|values| Values.new(values)}
59
+ json.map {|values| ReaderValues.new(values)}
76
60
  rescue JSON::ParserError
77
61
  return []
78
62
  end
79
63
 
64
+ def parse_messages
65
+ @messages = Messages.new(@stderr.readlines(chomp: true))
66
+ end
67
+
80
68
  end
81
69
 
82
70
  end
@@ -6,10 +6,10 @@ require 'set'
6
6
 
7
7
  module MultiExiftool
8
8
 
9
- # Representing (tag, value) pairs of metadata.
9
+ # Representing (tag, value) pairs of metadata as result of Reader#read.
10
10
  # Access via bracket-methods or dynamic method-interpreting via
11
11
  # method_missing.
12
- class Values
12
+ class ReaderValues
13
13
 
14
14
  # Regular expression to determine timestamp values
15
15
  REGEXP_TIMESTAMP = /^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d)(?::((?:\d\d)(?:\.\d+)?))?((?:[-+]\d\d:\d\d)|(?:Z))?(?: *DST)?$/
@@ -22,9 +22,9 @@ module MultiExiftool
22
22
  def initialize values
23
23
  @values = {}
24
24
  values.map do |tag,val|
25
- unified_tag = Values.unify_tag(tag)
26
- Values.tag_map[unified_tag] = tag
27
- val = val.kind_of?(Hash) ? Values.new(val) : val
25
+ unified_tag = MultiExiftool.unify_tag(tag)
26
+ ReaderValues.tag_map[unified_tag] = tag
27
+ val = val.kind_of?(Hash) ? ReaderValues.new(val) : val
28
28
  @values[unified_tag] = val
29
29
  end
30
30
  end
@@ -32,13 +32,13 @@ module MultiExiftool
32
32
  # Gets the (posible converted) value for a tag
33
33
  # (tag will be unified, i.e. FNumber, fnumber or f_number
34
34
  # can be used for FNumber)
35
- def [](tag)
36
- unified_tag = Values.unify_tag(tag)
35
+ def [] tag
36
+ unified_tag = MultiExiftool.unify_tag(tag)
37
37
  convert(unified_tag, @values[unified_tag])
38
38
  end
39
39
 
40
40
  # Converts values on the basis of unified tag name and value. It is called
41
- # each time a value is fethed from a Values instance.
41
+ # each time a value is fethed from a ReaderValues instance.
42
42
  # @return (maybe) converted value
43
43
  def convert tag, val
44
44
  return val unless val.kind_of?(String)
@@ -71,12 +71,12 @@ module MultiExiftool
71
71
  # Checks if a tag is present
72
72
  # @param Tag as string or symbol (will be unified)
73
73
  def has_tag? tag
74
- @values.has_key?(Values.unify_tag(tag.to_s))
74
+ @values.has_key?(MultiExiftool.unify_tag(tag))
75
75
  end
76
76
 
77
77
  # Gets the original tag names of this instance
78
78
  def tags
79
- @values.keys.map {|tag| Values.tag_map[tag]}
79
+ @values.keys.map {|tag| ReaderValues.tag_map[tag]}
80
80
  end
81
81
 
82
82
  # Generates a hash representation of this instance
@@ -85,7 +85,7 @@ module MultiExiftool
85
85
  def to_h
86
86
  @values.inject(Hash.new) do |h, a|
87
87
  tag, val = a
88
- h[Values.tag_map[tag]] = convert(Values.unify_tag(tag), val)
88
+ h[ReaderValues.tag_map[tag]] = convert(MultiExiftool.unify_tag(tag), val)
89
89
  h
90
90
  end
91
91
  end
@@ -98,14 +98,10 @@ module MultiExiftool
98
98
 
99
99
  attr_reader :tag_map
100
100
 
101
- def unify_tag tag
102
- tag.gsub(/[-_]/, '').downcase
103
- end
104
-
105
101
  end
106
102
 
107
- def method_missing tag, *args, &block
108
- res = self[Values.unify_tag(tag.to_s)]
103
+ def method_missing meth, *args, &block
104
+ res = self[MultiExiftool.unify_method(meth)]
109
105
  if res && block_given?
110
106
  if block.arity > 0
111
107
  yield res
@@ -116,9 +112,10 @@ module MultiExiftool
116
112
  res
117
113
  end
118
114
 
119
- def respond_to_missing? tag, *args
120
- has_tag?(tag) || super
115
+ def respond_to_missing? meth, *args
116
+ has_tag?(MultiExiftool.unify_method(meth)) || super
121
117
  end
118
+
122
119
  end
123
120
 
124
121
  end
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require_relative 'executable'
5
+ require_relative 'writer_values'
5
6
 
6
7
  module MultiExiftool
7
8
 
@@ -10,26 +11,32 @@ module MultiExiftool
10
11
  # possible errors.
11
12
  class Writer
12
13
 
13
- attr_accessor :overwrite_original, :values
14
+ attr_accessor :values
14
15
 
15
16
  include Executable
16
17
 
17
- def initialize filenames=[], values={}, opts={}
18
- super(filenames, opts)
19
- @values = values
18
+ def initialize filenames=[], values=nil, **vals_and_opts
19
+ if values
20
+ options = vals_and_opts
21
+ else
22
+ options = Options.new
23
+ values = {}
24
+ vals_and_opts.each_pair do |k, v|
25
+ if options.respond_to? k
26
+ options[k] = v
27
+ else
28
+ values[k] = v
29
+ end
30
+ end
31
+ end
32
+ super(filenames, **options)
33
+ self.values = values
20
34
  end
21
35
 
22
36
  def self.mandatory_args
23
37
  %w(-charset FileName=utf8 -charset utf8)
24
38
  end
25
39
 
26
- # Options to use with the exiftool command.
27
- def options
28
- opts = super
29
- opts[:overwrite_original] = true if @overwrite_original
30
- opts
31
- end
32
-
33
40
  # Getting the command-line arguments which would be executed
34
41
  # when calling #write. It could be useful for logging, debugging or
35
42
  # maybe even for creating a batch-file with exiftool command to be
@@ -38,33 +45,22 @@ module MultiExiftool
38
45
  fail MultiExiftool::Error, 'No filenames.' if filenames.empty?
39
46
  cmd = []
40
47
  cmd << Writer.mandatory_args
41
- cmd << options_args
42
- cmd << values_args
48
+ cmd << options.options_args
49
+ cmd << values.values_args
43
50
  cmd << filenames
44
51
  cmd.flatten
45
52
  end
46
53
 
47
54
  alias write execute # :nodoc:
48
55
 
49
- private
56
+ alias vals values
57
+ alias vals= values=
50
58
 
51
- def values_args
52
- raise MultiExiftool::Error.new('No values.') if values.empty?
53
- values_to_param_array(@values).map {|arg| "-#{arg}"}
54
- end
55
-
56
- def values_to_param_array hash
57
- res = []
58
- hash.each do |tag, val|
59
- if val.respond_to? :to_hash
60
- res << values_to_param_array(val.to_hash).map {|arg| "#{tag}:#{arg}"}
61
- elsif val.respond_to? :to_ary
62
- res << val.map {|v| "#{tag}=#{v}"}
63
- else
64
- res << "#{tag}=#{val}"
65
- end
59
+ def values= vals
60
+ unless vals.kind_of? WriterValues
61
+ vals = WriterValues.new(vals)
66
62
  end
67
- res.flatten
63
+ @values = vals
68
64
  end
69
65
 
70
66
  end
@@ -0,0 +1,91 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module MultiExiftool
5
+
6
+ # Representing (tag, value) pairs of metadata to use with Writer#write.
7
+ # Access via bracket-methods or dynamic method-interpreting via
8
+ # method_missing.
9
+ class WriterValues
10
+
11
+ def initialize values={}
12
+ @values = {}
13
+ values.each_pair do |k, v|
14
+ self[k] = v
15
+ end
16
+ end
17
+
18
+ # Gets the value for a tag (tag will be unified, i.e. FNumber, fnumber or
19
+ # f_number can be used for FNumber)
20
+ def [] tag
21
+ unified_tag = MultiExiftool.unify_tag(tag)
22
+ @values[unified_tag]
23
+ end
24
+
25
+ # Sets the value for a tag (tag will be unified, i.e. FNumber, fnumber or
26
+ # f_number can be used for FNumber)
27
+ def []= tag, val
28
+ unified_tag = MultiExiftool.unify_tag(tag)
29
+ if val.respond_to?(:to_hash) && !val.kind_of?(WriterValues)
30
+ val = WriterValues.new(val)
31
+ end
32
+ @values[unified_tag] = val
33
+ end
34
+
35
+ def empty?
36
+ @values.empty?
37
+ end
38
+
39
+ # Gets a hash representation of this instance
40
+ def to_h
41
+ @values
42
+ end
43
+
44
+ alias to_hash to_h
45
+
46
+ # Gets an array of the command-line arguments for this instance (internal
47
+ # use)
48
+ def values_args
49
+ raise MultiExiftool::Error.new('No values.') if @values.empty?
50
+ values_to_param_array(@values).map {|arg| "-#{arg}"}
51
+ end
52
+
53
+ private
54
+
55
+ def values_to_param_array hash
56
+ res = []
57
+ hash.each do |tag, val|
58
+ if val.respond_to? :to_hash
59
+ res << values_to_param_array(val.to_hash).map {|arg| "#{tag}:#{arg}"}
60
+ elsif val.respond_to? :to_ary
61
+ res << val.map {|v| "#{tag}=#{v}"}
62
+ else
63
+ res << "#{tag}=#{val}"
64
+ end
65
+ end
66
+ res.flatten
67
+ end
68
+
69
+ def method_missing meth, val=nil, &block
70
+ unified_meth = MultiExiftool.unify_method(meth)
71
+ if unified_meth =~ /^(.+)=$/
72
+ self[$1] = val
73
+ else
74
+ res = self[unified_meth]
75
+ if block_given?
76
+ res = WriterValues.new unless res
77
+ self[unified_meth] = res
78
+ if block.arity > 0
79
+ yield res
80
+ else
81
+ res.instance_eval &block
82
+ end
83
+ end
84
+ res
85
+ end
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+
@@ -1,14 +1,14 @@
1
1
  # encoding: utf-8
2
2
  # frozen_string_literal: true
3
3
 
4
- require_relative 'multi_exiftool/values'
4
+ require_relative 'multi_exiftool/options'
5
5
  require_relative 'multi_exiftool/reader'
6
6
  require_relative 'multi_exiftool/writer'
7
7
  require_relative 'multi_exiftool/batch'
8
8
 
9
9
  module MultiExiftool
10
10
 
11
- VERSION = '0.19.0'
11
+ VERSION = '1.0.0'
12
12
 
13
13
  @exiftool_command = 'exiftool'
14
14
 
@@ -19,30 +19,30 @@ module MultiExiftool
19
19
  # values, errors
20
20
  #
21
21
  # Example:
22
- # values, errors = MultiExiftool.read(Dir['*.jpg'])
23
- # if errors.empty?
24
- # values.each {|val| do_something_with(val) }
25
- # else
22
+ # values, messages = MultiExiftool.read(Dir['*.jpg'])
23
+ # if messages.errors_or_warnings?
26
24
  # # error handling
25
+ # else
26
+ # values.each {|val| do_something_with(val) }
27
27
  # end
28
- def read filenames, opts={}
29
- reader = Reader.new(filenames, opts)
28
+ def read filenames, *tags_pos, tags: nil, **opts
29
+ reader = Reader.new(filenames, *tags_pos, tags: tags, **opts)
30
30
  values = reader.read
31
- [values, reader.errors]
31
+ [values, reader.messages]
32
32
  end
33
33
 
34
34
  # Writing metadata
35
35
  # Returns an array of the error messages
36
36
  #
37
37
  # Example:
38
- # errors = MultiExiftool.write(Dir['*.jpg'], {author: 'Jan Friedrich'})
39
- # unless errors.empty?
38
+ # messages = MultiExiftool.write(Dir['*.jpg'], {author: 'Jan Friedrich'})
39
+ # if messages.errors_or_warnings?
40
40
  # # do error handling
41
41
  # end
42
- def write filenames, values, opts={}
43
- writer = Writer.new(filenames, values, opts)
42
+ def write filenames, values=nil, **vals_and_opts
43
+ writer = Writer.new(filenames, values, **vals_and_opts)
44
44
  writer.write
45
- writer.errors
45
+ writer.messages
46
46
  end
47
47
 
48
48
  # Deleting metadata
@@ -50,18 +50,17 @@ module MultiExiftool
50
50
  #
51
51
  # Examples:
52
52
  # # delete values for all tags
53
- # errors = MultiExiftool.delete_values(Dir['*.jpg'])
54
- # unless errors.empty?
53
+ # messages = MultiExiftool.delete_values(Dir['*.jpg'])
54
+ # if messages.errors_or_warnings?
55
55
  # # do error handling
56
56
  # end
57
57
  #
58
58
  # # delete values for tags Author and Title
59
- # errors = MultiExiftool.delete_values(Dir['*.jpg'], %w[author title])
60
- # unless errors.empty?
59
+ # messages = MultiExiftool.delete_values(Dir['*.jpg'], %w[author title])
60
+ # if messages.errors_or_warnings?
61
61
  # # do error handling
62
62
  # end
63
- def delete_values filenames, opts={}
64
- tags = opts.fetch(:tags, :all)
63
+ def delete_values filenames, tags: :all
65
64
  values = Array(tags).inject(Hash.new) {|h,tag| h[tag] = nil; h}
66
65
  write(filenames, values)
67
66
  end
@@ -71,11 +70,11 @@ module MultiExiftool
71
70
  # Returns an array of the error messages
72
71
  #
73
72
  # Example:
74
- # errors = MultiExiftool.batch do
73
+ # messages = MultiExiftool.batch do
75
74
  # Dir['*.jpg'].each_with_index do |filename, i|
76
75
  # write filename, {author: 'Jan Friedrich', comment: "This is file number #{i+1}."}
77
76
  # end
78
- # unless errors.empty?
77
+ # if messages.errors_or_warnings?
79
78
  # # do error handling
80
79
  # end
81
80
  def batch &block
@@ -86,7 +85,7 @@ module MultiExiftool
86
85
  yield batch
87
86
  end
88
87
  batch.execute
89
- batch.errors
88
+ batch.messages
90
89
  end
91
90
 
92
91
  attr_accessor :exiftool_command
@@ -103,6 +102,20 @@ module MultiExiftool
103
102
  @exiftool_version ||= `#{exiftool_command} -ver`.to_f
104
103
  end
105
104
 
105
+ # :nodoc:
106
+
107
+ # Converts a tag name into a unified form by converting it with to_s and
108
+ # downcasing it
109
+ def unify_tag name
110
+ name.to_s.downcase
111
+ end
112
+
113
+ # Converts a method name into a unified form by converting it with to_s and
114
+ # removing underscores and downcasing it
115
+ def unify_method name
116
+ name.to_s.gsub('_', '').downcase
117
+ end
118
+
106
119
  end
107
120
 
108
121
  class Error < ::StandardError; end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multi_exiftool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
- original_platform: ''
7
6
  authors:
8
7
  - Jan Friedrich
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-11-23 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: contest
@@ -114,9 +113,12 @@ files:
114
113
  - lib/multi_exiftool.rb
115
114
  - lib/multi_exiftool/batch.rb
116
115
  - lib/multi_exiftool/executable.rb
116
+ - lib/multi_exiftool/messages.rb
117
+ - lib/multi_exiftool/options.rb
117
118
  - lib/multi_exiftool/reader.rb
118
- - lib/multi_exiftool/values.rb
119
+ - lib/multi_exiftool/reader_values.rb
119
120
  - lib/multi_exiftool/writer.rb
121
+ - lib/multi_exiftool/writer_values.rb
120
122
  homepage: https://github.com/janfri/multi_exiftool
121
123
  licenses:
122
124
  - MIT
@@ -135,7 +137,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
135
137
  requirements:
136
138
  - - ">="
137
139
  - !ruby/object:Gem::Version
138
- version: 1.9.1
140
+ version: '2.3'
139
141
  required_rubygems_version: !ruby/object:Gem::Requirement
140
142
  requirements:
141
143
  - - ">="
@@ -143,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
145
  version: '0'
144
146
  requirements:
145
147
  - exiftool, version 11.10 or higher
146
- rubygems_version: 3.6.0.dev
148
+ rubygems_version: 4.0.6
147
149
  specification_version: 4
148
150
  summary: This library is a wrapper for the ExifTool command-line application (https://exiftool.org).
149
151
  test_files: []