summaryrefslogtreecommitdiff
path: root/omptagger
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2010-10-17 03:31:04 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2010-10-17 03:31:04 -0700
commite28c1f9fc519b2f610a8cd26409368640bd95c85 (patch)
treeb43e79c20ff1ce28fb41fcbd3a070c58e393147c /omptagger
parent6c74f258aca075dea41fd13ec06da9b1955da3c6 (diff)
downloadomptagger-e28c1f9fc519b2f610a8cd26409368640bd95c85.tar.gz
omptagger-e28c1f9fc519b2f610a8cd26409368640bd95c85.tar.xz
Ability to rename files.
Diffstat (limited to 'omptagger')
-rwxr-xr-xomptagger45
1 files 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