Class: Rutaci::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/rutaci/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



25
26
27
28
29
30
31
32
33
34
# File 'lib/rutaci/application.rb', line 25

def initialize
  @options = OpenStruct.new(
  # default values
  :command => :get,
  :fields => [:track, :title, :artist, :album, :year, :genre, :comment], # FIXME: patch tagalib to provilde a list of all available fields
  :destination => ".",
  :files => []
  )
  #puts "default options: #{@options}"
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/rutaci/application.rb', line 23

def options
  @options
end

Instance Method Details

#parse_optionsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rutaci/application.rb', line 36

def parse_options
  # The general form of the parameters to parse is:
  # "<general options> <command> <field otions> <filenames...>"
  # Since GetoptLong and ParseOptions don't know handle this structure (options os a
  # command in the middle) but I don't want to do all by hand, ARGV is split  at the command
  # position in two, the command is analysed manualy and the two parts are passed to GetoptLong
  # seperately.
  command_position = (ARGV.index('set') or ARGV.index('get') or ARGV.index('rename'))
  if command_position
    field_options = ARGV.slice! command_position..ARGV.length # splitt ARGV at the command_position
    @options.fields = nil # don't use the default files when a command is given
    @options.command = field_options.shift.to_sym
  else
    field_options = []
  end
  
  get_parser = OptionParser.new do |opts|
    opts.banner = "fields to use with the get command (or set with --format/--muscibrains):"
    opts.on("--title", "-t", "get the title") do 
      @options.fields ||= Array.new
      @options.fields.push :title
    end
    opts.on("--artist", "-i", "get the artist") do 
      @options.fields ||= Array.new
      @options.fields.push :artist
    end
    opts.on("--album", "-a", "get the album") do 
      @options.fields ||= Array.new
      @options.fields.push :album
    end
    opts.on("--track", "-n", "get the tracknumer") do 
      @options.fields ||= Array.new
      @options.fields.push :track
    end
    opts.on("--year", "-y", "get the year") do 
      @options.fields ||= Array.new
      @options.fields.push :year
    end
    opts.on("--genre", "-g", "get the genre") do
      @options.fields ||= Array.new
      @options.fields.push :genre
    end
    opts.on("--comment", "-c", "get the comment") do 
      @options.fields ||= Array.new
      @options.fields.push :comment
    end
  end # get_parser
  
  set_parser = OptionParser.new do |opts|
    opts.banner = "fields to use with the set command:"
    opts.on("--title TITLE", "-t", "set the title to TITLE") do |title|
      @options.fields ||= Hash.new
      @options.fields[:title] = title
    end
    opts.on("--artist ARTIST", "-i", "set the artist to ARTIST") do |artist|
      @options.fields ||= Hash.new
      @options.fields[:artist] = artist
    end
    opts.on("--album ALBUM", "-a", "set the album to ALBUM") do |album|
      @options.fields ||= Hash.new
      @options.fields[:album] = album
    end
    opts.on("--track N", "-n", "set the tracknumer to N") do |track|
      @options.fields ||= Hash.new
      @options.fields[:track] = track
    end
    opts.on("--year YEAR", "-y", "set the year to YEAR") do |year|
      @options.fields ||= Hash.new
      @options.fields[:year] = year
    end
    opts.on("--genre GENRE", "-g", "set the genre to GENRE") do |genre|
      @options.fields ||= Hash.new
      @options.fields[:genre] = genre
    end
    opts.on("--comment COMMENT", "-c", "set the comment to COMMENT") do |comment|
      @options.fields ||= Hash.new
      @options.fields[:comment] = comment
    end

    opts.separator ""
    opts.separator "Note: When using the --format option, the set parameters take no argument"
    opts.separator "      (like when used with get) The value is extracted via FORMAT from the"
    opts.separator "      the path/filename. Without parameters, every field appearing in FORMAT"
    opts.separator "      is used."
#        opts.separator ""
#        opts.separator "Note: When using the --musicbrainz option, the set parameters take no argument"
#        opts.separator "      (like when used with get) The value are looked up at musicbrains.org"
#        opts.separator "      using an acoustic fingerprint of the music file. Without parameters,"
#        opts.separator "      every field provided by musicbrains.org is used."
  end # set_parser

  general_parser = OptionParser.new do |opts|
    opts.banner = "general options:"
    opts.on("--dry-run", "-d", "Don't really execute the command, just show what would happen",
            "Has no effect with the get command") do
      @options.dry_run = true
    end
    opts.on("--interactive", "-i", "Give the user the chance to edit any value/filename",
            "Has no effect with the get command; requires GNU readline") do
      @options.interactive = true
    end
    opts.on("--keep-empty-fields", "-k", "Keep empty fields instead of removeing them when",
            "displaying or setting an empty value.",
            "Don't affects the rename command, we will never set an empty filename") do
      @options.keep_empty_fields = true
    end
    opts.on("--format FORMAT", "-f", "Use FORMAT to parse input or format output and filenames",
            "Used with a get command, it will format the output",
            "Used with a set command, it's the pattern for parsing the filename",
            "Used with a rename command, it will give the new filename",
            "An example is '%n - %t.mp3', see the manual for details") do |format|
      @options.format = format
    end
#        opts.on("--musicbrainz", "-m", "use musicbrainz.org to find the right taggs for you music",
#                "This is just an other source for the metadata") do
#          @options.musicbrainz = true
#        end

    opts.separator ""
    opts.separator "options to use with the get command:"
    opts.on("--values-only", "-o", "Print out only the values themselfs (one per line)",
            "Fieldnames and the normaly displayed filename are ommited.") do
      @options.values_only = true
    end

    opts.separator ""
    opts.separator "options to use with the rename command:"
    opts.on("--destination DESTINATION", "-D", "Set the destination directory to DESTINATION",
            "default '#{@options.destination}'") do |destination|
      @options.destination = destination
    end

    opts.separator ""
    opts.separator "generic options:"

    opts.on("--help", "-h", "-?", "Shows this message and quit") do
      puts "rutaci is a cli tool for manipulating music file's metadata"
      puts
      puts "Useage: #{$0} [<options>] [get [<fields>]] <filenames...>"
      puts "        #{$0} [<options>] set <fields and values> <filenames...>"
      puts "        #{$0} [<options>] --format FORMAT rename <filenames...>"
      puts
      puts general_parser
      puts
      puts get_parser
      puts
      puts set_parser
      exit
    end
    opts.on("--version", "Shows version and exit") do
      puts VERSION_STRING
      exit
    end
  end #general_parser

  general_parser.parse! ARGV
  @options.files.push *ARGV # the remaining args are filenames, when no command is given

  if @options.command == :get or (@options.command == :set and (@options.format or @options.musicbrainz))
    # note: when a format is given, we parse get-style,
    # even with set since the values come from the filename or muscibrains (not the command line)
    get_parser.parse! field_options
  else
    set_parser.parse! field_options if @options.command == :set
  end
  @options.files.push *field_options # the remaining args are filenames

  if @options.files.empty?
    puts "sorry, I need at least one filename to work with!"
    puts "try the manpage or #{$0} --help"
    exit 1
  end

  if @options.command == :rename and not @options.format
    puts "sorry, I need a format for the rename command!"
    puts "try the manpage or #{$0} --help"
    exit 1
  end
end

#runObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rutaci/application.rb', line 216

def run
  parse_options
  executor = case @options.command
             when :get
               require 'rutaci/getter'
               Getter.new @options
             when :set
               require 'rutaci/setter'
               Setter.new @options
             when :rename
               require 'rutaci/renamer'
               Renamer.new @options
             end
  executor.run @options.files
end