From e28c1f9fc519b2f610a8cd26409368640bd95c85 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Sun, 17 Oct 2010 03:31:04 -0700 Subject: Ability to rename files. --- omptagger | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/omptagger b/omptagger index 78f57a8..07fb2ed 100755 --- a/omptagger +++ b/omptagger @@ -158,10 +158,12 @@ class MetadataError < RuntimeError case @error when :empty str = 'No fields set.' + when :generate + str = 'Filename does not match scheme.' when :invalid str = 'Invalid field name.' - when :scheme - str = 'Filename does not match scheme.' + when :rename + str = 'Insufficient tags.' when :unset str = 'Field not set.' end @@ -191,10 +193,11 @@ class Metadata Output.file(file) @filename = File.expand_path(file) - @write = false + @origname = @filename @file = open(file) @metadata = read + @write = false end def view @@ -272,7 +275,7 @@ class Metadata fields = scheme.scan(regexp).flatten values = @filename.chomp(File.extname(@filename)).scan(regexp).flatten - raise MetadataError, :scheme unless fields.length == values.length + raise MetadataError, :generate unless fields.length == values.length fields.collect! do |field| keys[field[1,1]] @@ -286,6 +289,18 @@ class Metadata end end end + + def rename(scheme) + scheme.scan(/%([#{keys.keys.join}])/).flatten.uniq.each do |field| + raise MetadataError, :rename unless @metadata[keys[field]] + scheme = scheme.gsub('%' + field, @metadata[keys[field]].first) + end + + scheme << File.extname(@filename) + @filename = File.join(File.dirname(@filename), scheme) + + Output.info(@filename) + end end class VorbisComment < Metadata @@ -298,21 +313,23 @@ class VorbisComment < Metadata end def save - return unless @write - - read.each_key do |field| - metadata.removeField(TagLib::String.new(field)) - end + if @write + read.each_key do |field| + metadata.removeField(TagLib::String.new(field)) + end - @metadata.each do |field, value| - field = TagLib::String.new(field) + @metadata.each do |field, value| + field = TagLib::String.new(field) - value.each do |value| - metadata.addField(field, TagLib::String.new(value), false) + value.each do |value| + metadata.addField(field, TagLib::String.new(value), false) + end end + + @file.save end - @file.save + File.rename(@origname, @filename) unless @filename == @origname end private -- cgit v1.2.3-70-g09d2