Class: MediaOrganizer::Renamer
- Inherits:
-
Object
- Object
- MediaOrganizer::Renamer
- Defined in:
- lib/renamer.rb
Overview
Renamer: primary class to use for renaming files. Allows renaming of a given list of files to a user-defined scheme based on each file’s metadata.
Key Methods
*naming_scheme() *generate() *overwrite()
Example Usage
old_uris = [‘./test/data/hs-2003-24-a-full_tif.tif’]
scheme = [“Test-”, :date_time]
r = Renamer.new()
r.set_naming_scheme(scheme)
new_uris = r.generate(old_uris)
r.overwrite(new_uris) #new filename: “./test/data/Test-2003-09-03 12_52_43 -0400.tif”)
Constant Summary collapse
- DISALLOWED_CHARACTERS =
Characters that are not allowed in file names by many file systems. Replaced with @subchar character.
/[\\:\?\*<>\|"\/]/
Instance Attribute Summary collapse
-
#naming_scheme ⇒ Object
readonly
Array of strings and literals used to construct filenames.
-
#subchar ⇒ Object
Character with which to substitute disallowed characters.
Instance Method Summary collapse
-
#generate(uri_list = [], args = {}) ⇒ Object
Renamer.generate(): Creates a hash mapping the original filenames to the new (renamed) filenames.
-
#get_metadata(file) ⇒ Object
Routes metadata scrape based on file type (currently relies on extension - future version should use MIME) currently assumes file was checked for validity in calling code.
-
#initialize(_args = {}) ⇒ Renamer
constructor
A new instance of Renamer.
-
#overwrite(renames_hash = {}) ⇒ Object
Renamer.overwrite(): Writes new file names based upon mapping provided in hash argument.
-
#set_naming_scheme(arr = []) ⇒ Object
Renamer.naming_scheme(): sets the naming scheme for the generate method.
Constructor Details
#initialize(_args = {}) ⇒ Renamer
Returns a new instance of Renamer.
40 41 42 43 |
# File 'lib/renamer.rb', line 40 def initialize(_args = {}) @naming_scheme = ['Renamed-default-'] @subchar = '_' end |
Instance Attribute Details
#naming_scheme ⇒ Object (readonly)
Array of strings and literals used to construct filenames. Set thruough naming_scheme as opposed to typical/default accessor.
37 38 39 |
# File 'lib/renamer.rb', line 37 def naming_scheme @naming_scheme end |
#subchar ⇒ Object
Character with which to substitute disallowed characters
38 39 40 |
# File 'lib/renamer.rb', line 38 def subchar @subchar end |
Instance Method Details
#generate(uri_list = [], args = {}) ⇒ Object
Renamer.generate(): Creates a hash mapping the original filenames to the new (renamed) filenames
Inputs
1. List of URIs in the form of an array 2. Optional hash of arguments. *:scheme - array of strings and symbols specifying file naming convention
Outputs
Hash of “file name pairs.” old_file => new_file
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 |
# File 'lib/renamer.rb', line 69 def generate(uri_list = [], args = {}) scheme = if !args[:scheme].nil? && args[:scheme].is_a?(Array) && !args[:scheme].empty? set_scheme(args[:scheme]) else @naming_scheme end raise InvalidArgumentError unless !uri_list.nil? && uri_list.is_a?(Array) filename_pairs = {} uri_list.each do |i| new_string = handle_file(i, scheme) # If this is a valid file path, add it to the filename_pairs # puts "New file rename added: #{new_string}" filename_pairs[i] = new_string if !new_string.nil? && new_string != '' end return filename_pairs rescue InvalidArgumentError => arg_e puts arg_e puts 'Invalid arguments provided. Expected: uri_list = [], args = {}' rescue => e puts e. puts e.backtrace.inspect end |
#get_metadata(file) ⇒ Object
Routes metadata scrape based on file type (currently relies on extension - future version should use MIME) currently assumes file was checked for validity in calling code.
Inputs
String containing full file URI (path and filename)
Outputs
Returns hash of metadata for file, or nil if none/error.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/renamer.rb', line 130 def (file) # LOAD EXIF DATA case File.extname(file).downcase when '.jpg' Image.get_jpeg_data(file) when '.tif' Image.get_tiff_data(file) when '.mp3', '.wav', '.flac', '.aiff', '.ogg', '.m4a', '.asf' Music.get_music_data(file) else raise UnsupportedFileTypeError, "Error processing #{file}" end rescue UnsupportedFileTypeError => e puts "Could not process file: Extension #{File.extname(file)} is not supported." puts e.backtrace.inspect end |
#overwrite(renames_hash = {}) ⇒ Object
Renamer.overwrite(): Writes new file names based upon mapping provided in hash argument. NOTE: this will create changes to file names!
Inputs
1. Hash containing mappings between old filenames (full URI) and new filenames (full URI). Example: => “/path/to/newfile.jpg”
Outputs
none (file names are overwritten)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/renamer.rb', line 102 def overwrite(renames_hash = {}) renames_hash.each do |old_name, new_name| begin # error/integrity checking on old_name and new_name raise FileNotValidError, "Could not access specified source file: #{i}." unless old_name.is_a?(String) && File.exist?(old_name) raise FileNotValidError, 'New file name provided is not a string' unless new_name.is_a?(String) # puts (File.dirname(File.absolute_path(old_name)) + "/" + new_name) #Comment this line out unless testing File.rename(File.absolute_path(old_name), File.dirname(File.absolute_path(old_name)) + '/' + new_name) # check that renamed File.exist? - Commented out because this currently does not work. # unless new_name.is_a?(String) && File.exist?(new_name) # raise RenameFailedError, "Could not successfuly rename file: #{old_name} => #{new_name}. Invalid URI or file does not exist." # end rescue => e puts "Ignoring rename for #{old_name} => #{new_name}" end end end |
#set_naming_scheme(arr = []) ⇒ Object
Renamer.naming_scheme(): sets the naming scheme for the generate method.
Inputs
1. Array containing strings and symbols.
Outputs
None (sets instance variable @naming_scheme)
Example
naming_scheme([“Vacation_Photos_”, :date_taken]). This will rename files into a format like “Vacation_Photos_2014_05_22.png” based on the file’s date_taken metadata field.
56 57 58 |
# File 'lib/renamer.rb', line 56 def set_naming_scheme(arr = []) @naming_scheme = set_scheme(arr) end |