Class: Roebe::StdRenamer

Inherits:
Base show all
Defined in:
lib/roebe/classes/std_renamer.rb

Overview

Roebe::StdRenamer.new

Constant Summary collapse

SIMULATE_ONLY =
#

SIMULATE_ONLY

#
false
REMOVE_UNDERSCORES =
#

REMOVE_UNDERSCORES

#
false
ARRAY_GSUB_THOSE_ENTRIES =
#

ARRAY_GSUB_THOSE_ENTRIES

#
[
  'AUD_','GUI_','VID_','LIB_','MUD_','FREUNDE_',
  'GAMES_','RPG_','KDE_THEMES','KDE_THEME',
  'KATZEN_','XYLLO_','MAP_',
  'extracted_from_',
  'Strack_','RAVE_','RAP_',
  'RUS_','SLO_','SPA_',
  'POP_','ITA_',
  'JAP_','JAZZ_',
  'ITA_','HOP_','HUN_',
  'Bdance_','Choir_','ELE_',
  'AUT_','Classic_',
  'DONE_','.dvdrip.xvid-fqm'
]

Constants inherited from Base

Base::COLOURS, Base::HOME_DIRECTORY_OF_USER_X, Base::N, Base::NAMESPACE

Instance Method Summary collapse

Methods inherited from Base

#actions, #append_this_onto_that_file, #append_what_into, #be_silent, #be_verbose?, #beautiful_url, #burlywood, #call_next, #chdir, #cheerful_guy, #cliner, #complex_esystem, #convert_global_env, #copy_directory, #copy_file, #cornflowerblue, #create_directory, #crimson, #current_month?, #current_time?, #current_year?, #cyan, #darkcyan, #darkgreen, #darkkhaki, #darkslateblue, #deeppink, #delete_symlink, #do_not_use_the_base_colours, #do_use_the_base_colours, #e, #ecomment, #editor_to_use?, #efancy, #eimp, #emphasis, #ensure_main_encoding, #ensure_utf_encoding, #eparse, #erev, #esteelblue, #etomato, #ewarn, #exit_program, #firebrick, #get_current_day, #get_current_month, #get_files_and_directories, #get_files_from, #get_german_name_for_this_weekday, #gold, #green, #grey, #hh_mm_ss, #hh_mm_ss_day_month_year, #home_dir?, #home_directory_of_user_x?, #infer_the_namespace, #internal_hash?, #is_a_directory?, #is_a_file?, #is_a_jpg_file?, #is_an_image_file?, #is_archive?, #is_audio_file?, #is_in_studium_dir?, #is_multimedia_file?, #is_on_roebe?, #is_on_windows?, #is_studium_available?, #is_symlink?, #is_this_a_ruby_file?, #is_video_file?, #iso_encoding?, #le, #left_colour, #lightblue, #lightgreen, #lightseagreen, #lightsteelblue, #lime, #limegreen, #localhost_to_data, #log_directory?, #main_encoding?, #mediumorchid, #mediumpurple, #mediumseagreen, #mediumslateblue, #mediumspringgreen, #mediumturquoise, #mkdir_p, #mv, #n_days_in_this_month, #n_pages_in_this_pdf_file?, #namespace?, #no_file_exists_at, #ogrey, #olive, #olivedrab, #open_in_browser, #opne, #opnesystem, #opnn, #orange, #orchid, #orev, #palegoldenrod, #palevioletred, #pink, #powderblue, #programs_dir?, #project_base_dir?, #random_html_colour, #rds, #read_file_in_iso_encoding, #read_file_via_the_default_encoding, #read_lines_via_iso_encoding, #readlines_with_main_encoding, #red, #register_sigint, #remove, #remove_directory, #remove_file, #rename_kde_konsole_tab, #replace_localhost_with_data, #report_pwd, #require_rescue, #reset_the_internal_hash, #return_all_directories_from_this_directory, #return_all_files_from_this_directory, #return_current_directory, #return_dd_mm_yyyy, #return_file_or_directory_of, #return_files_from_pwd, #return_last_part_of_the_current_directory, #return_utc, #rev, #right_arrow?, #right_colour, #roebe_log_directory?, #rosybrown, #royalblue, #ruby_base_directory?, #run_in_background, #run_rcfiles_then_run_ata_via_qdbus, #sandybrown, #sdir, #seagreen, #set_be_verbose, #set_xorg_buffer, #sfancy, #sfile, #silent_redirection?, #simp, #simple_esystem, #skyblue, #slateblue, #slategray, #springgreen, #steelblue, #string_italic, #swarn, #symlink, #teal, #temp_dir?, #to_camelcase, #to_counted_hash, #tomato, #touch, #try_to_require_the_beautiful_url_gem, #try_to_require_the_html_template, #try_to_require_the_open_gem, #try_to_require_the_program_information_gem, #try_to_require_the_xorg_buffer, #use_colours?, #verbose_truth, #weekday?, #word_wrap, #write_what_into, #yellow

Methods included from Base::CommandlineArguments

#append_onto_the_commandline_arguments, #clear_commandline_arguments, #commandline_arguments?, #commandline_arguments_as_string?, #commandline_arguments_without_leading_hyphens?, #first_argument?, #first_argument_without_leading_hyphens?, #has_an_argument_been_passed?, #remove_hyphened_arguments_from_the_commandline_arguments, #return_commandline_arguments_with_leading_hyphens, #second_argument?, #set_commandline_arguments

Constructor Details

#initialize(optional_set_simulate_only = nil, run_already = true) ⇒ StdRenamer

#

initialize

Please use ” instead of “” for strings.

#


54
55
56
57
58
59
60
61
62
63
# File 'lib/roebe/classes/std_renamer.rb', line 54

def initialize(
    optional_set_simulate_only = nil,
    run_already                = true
  )
  reset
  if optional_set_simulate_only # Right now, if any argument was given, we will set @simulate_only to true.
    set_simulate_only(optional_set_simulate_only)
  end
  run if run_already
end

Instance Method Details

#debug?Boolean

#

debug?

#

Returns:

  • (Boolean)


82
83
84
# File 'lib/roebe/classes/std_renamer.rb', line 82

def debug?
  @simulate_only == :DEBUG
end

#enter_interactive_modeObject

#

enter_interactive_mode (interactive tag)

#


235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/roebe/classes/std_renamer.rb', line 235

def enter_interactive_mode
  e
  e 'Entering inactive mode next ...'
  e
  e 'Please input which string you wish to '+swarn('remove')+' from '
  e 'the files in the current working directory:'
  e
  user_input = $stdin.gets.chomp
  e 'Next removing the string '+orange(user_input)+' from all files'
  e 'in the current working directory.'
  obtain_entries_from_this_directory # Obtain @entries here.
  @entries.each {|old_filename|
    next unless old_filename.include? user_input
    new_filename = old_filename.sub(/#{user_input}/,'')
    rename_file(old_filename, new_filename)
  }
end

#fetch_all_entriesObject

#

fetch_all_entries

This is fetched from the method run(). It will assign to the

#


92
93
94
95
96
97
98
99
100
# File 'lib/roebe/classes/std_renamer.rb', line 92

def fetch_all_entries
  @entries = Dir[@base_directory+'*']
  if debug?
    opne 'Debugging now.'
    pp @entries
    opne 'Exiting now.'
    exit 1 # Non-zero.
  end
end

#obtain_entries_from_this_directory(i = return_pwd) ⇒ Object

#

obtain_entries_from_this_directory

#


105
106
107
108
109
# File 'lib/roebe/classes/std_renamer.rb', line 105

def obtain_entries_from_this_directory(i = return_pwd)
  @entries = Dir[i+'/**'].select {|entry|
    File.file? entry
  }
end

#pad_with_quotes(i) ⇒ Object Also known as: pad

#

pad_with_quotes

This will return a variant that has ‘“’ characters on both ends.

#


126
127
128
# File 'lib/roebe/classes/std_renamer.rb', line 126

def pad_with_quotes(i)
  return '"'+i+'"'
end

#rename(old, new) ⇒ Object Also known as: rename_file

#

rename

This is the rename-action of this class.

#


181
182
183
184
185
186
187
188
189
190
# File 'lib/roebe/classes/std_renamer.rb', line 181

def rename(old, new) # mv tag
  unless old == new
    opne '  mv '+old+' '+new
    if File.exist? old
      FileUtils.mv(old, new)
    else
      ewarn 'The file at `'+old+'` does not seem to exist.'
    end
  end
end

#resetObject

#

reset (reset tag)

#


68
69
70
71
72
73
74
75
76
77
# File 'lib/roebe/classes/std_renamer.rb', line 68

def reset
  super()
  infer_the_namespace
  set_base_directory
  set_simulate_only
  # === @entries
  @entries = nil # Contains the entries in our base directory.
  @remove_underscores = REMOVE_UNDERSCORES
  @shall_we_replace_blanks = true
end

#runObject

#

run (run tag)

#


267
268
269
270
# File 'lib/roebe/classes/std_renamer.rb', line 267

def run
  fetch_all_entries
  start_to_rename_the_entries
end

#run_system_command(i) ⇒ Object

#

run_system_command

#


133
134
135
136
# File 'lib/roebe/classes/std_renamer.rb', line 133

def run_system_command(i)
  e '  '+i
  system i unless simulate_only?
end

#set_base_directory(i = return_pwd) ⇒ Object

#

set_base_directory

#


114
115
116
117
118
119
# File 'lib/roebe/classes/std_renamer.rb', line 114

def set_base_directory(
    i = return_pwd
  ) # Set the @base_directory variable here.
  i << '/' unless i.end_with? '/'
  @base_directory = i # This var denotes the directory from where we will start the renaming.
end

#set_simulate_only(i = SIMULATE_ONLY) ⇒ Object

#

set_simulate_only

#


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/roebe/classes/std_renamer.rb', line 195

def set_simulate_only(
    i = SIMULATE_ONLY
  ) # if set to true then we simulate, but dont do anything.
  if i.is_a? Array
    i.each {|entry| set_simulate_only(entry) }
  else
    i = SIMULATE_ONLY if i.nil?
    case i # case tag, menu tag
    # ======================================================================= #
    # === srename --interactive
    # ======================================================================= #
    when /^-?-?interactive$/
      enter_interactive_mode
      exit
    # ======================================================================= #
    # === srename --help
    # ======================================================================= #
    when 'HELP',/-?-?help/
      show_help
      exit
    # ======================================================================= #
    # === srename --simulate
    # ======================================================================= #
    when 'SIMULATE',/^SIM/,'TEST',/simulate/
      i = true
    end
    @simulate_only = i # Untaint ARGV here.
  end
end

#show_helpObject

#

show_help (help tag)

#


256
257
258
259
260
261
262
# File 'lib/roebe/classes/std_renamer.rb', line 256

def show_help
  e
  opne 'You can pass SIMULATE.'
  opne '  --interactive # interactively decide '\
          'which string is to be removed from files'
  e
end

#simulate_only?Boolean

#

simulate_only?

#

Returns:

  • (Boolean)


228
229
230
# File 'lib/roebe/classes/std_renamer.rb', line 228

def simulate_only?
  @simulate_only
end

#start_to_rename_the_entriesObject Also known as: start_to_rename

#

start_to_rename_the_entries

#


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
# File 'lib/roebe/classes/std_renamer.rb', line 141

def start_to_rename_the_entries # Here we start the renaming.
  if @entries
    opne 'We are starting to rename the entries, starting from the directory'
    opne sdir(@base_directory)+'. We will work on '+
            simp(@entries.size.to_s)+' entries.'
    cliner
    @entries.each {|entry|
      next if entry.start_with? '.' # Ignore entries with leading .
      new_name = ''.dup # This is the new name of the file.
      new_name = entry.to_s.dup
      new_name.gsub!(/!/,'')
      new_name.gsub!(/ /,'') if @shall_we_replace_blanks
      new_name.gsub!(/_/,'') if @remove_underscores
      new_name.gsub!(/\.PDF$/,'.pdf')
      new_name.gsub!(/BC2/,'BCII')
      new_name.gsub!(/get\.smart\.1965/,'get.smart-1965')
      new_name.squeeze!('_') if entry.include? '__'
      # =================================================================== #
      # Here we do the gsubs, based on the main Array.
      # =================================================================== #
      ARRAY_GSUB_THOSE_ENTRIES.each { |the_entry|
        the_entry = Regexp.quote(the_entry)
        new_name.gsub!(/#{the_entry}/, '')
      }
      cmd_line  = 'mv '.dup
      cmd_line << pad_with_quotes(
        @base_directory+File.basename(entry)
      )
      cmd_line << ' '
      cmd_line << pad(new_name)
      rename(entry, new_name)
    }
  end
end