rfs 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,10 +24,11 @@ protected
24
24
  end
25
25
 
26
26
  def rename_count
27
- count = 0
27
+ count = opt(:count_start, Numeric, String)
28
28
  rename_each_match do |file, md|
29
- count += 1
30
- subst(file, md, count.to_s)
29
+ r = subst(file, md, count.to_s)
30
+ count = count.succ
31
+ r
31
32
  end
32
33
  end
33
34
 
@@ -63,7 +64,11 @@ protected
63
64
  verbose {"search: #{@options[:search].to_s}"}
64
65
  end
65
66
  rename_each_match do |file, md|
66
- subst(file, md, active_capture(md).toggle_roman) if active_capture(md).length > 0
67
+ begin
68
+ subst(file, md, active_capture(md).toggle_roman) if active_capture(md).length > 0
69
+ rescue RomanNumeralError => e
70
+ [:fileerror, e.message]
71
+ end
67
72
  end
68
73
  end
69
74
 
@@ -136,6 +141,15 @@ protected
136
141
  :delete
137
142
  end
138
143
  end
144
+
145
+ def rename_eval
146
+ raw_expr = opt(:eval_expr, String)
147
+ rename_each_match do |file, md, path|
148
+ s = md.interpolate(raw_expr)
149
+ verbose {"evaluate #{s}"}
150
+ subst(file, md, eval(s).to_s)
151
+ end
152
+ end
139
153
  end
140
154
 
141
155
 
@@ -1,3 +1,5 @@
1
+ require 'rubygems'
2
+ require 'innate/array'
1
3
  require 'innate/mkdirs'
2
4
  require 'innate/roman'
3
5
  require 'innate/titlecase'
@@ -40,6 +42,8 @@ protected
40
42
  end
41
43
 
42
44
  def actual_rename(o, n)
45
+ len = File.expand_path(n).length
46
+ raise NameLengthError.new(len) if len >= 255
43
47
  File.mkdirs File.split(n).first
44
48
  if File.directory? n
45
49
  raise DirectoryExistsError.new("Can't overwrite an existing directory.")
@@ -140,9 +144,10 @@ protected
140
144
  end
141
145
 
142
146
  def rename_each
147
+ filter = opt_if(:filter, Filter)
143
148
  if @options[:action] == :list
144
149
  @options[:file_provider].each do |path, file|
145
- opt_if(:filter, Filter) do |filter|
150
+ if filter
146
151
  unless filter.filter path, file
147
152
  verbose { "filtered: #{file}" }
148
153
  next
@@ -153,7 +158,6 @@ protected
153
158
  elsif @options[:action] == :commit || @options[:action] == :preview
154
159
  catch :cancel do
155
160
  search = opt :search, Regexp
156
- filter = opt_if(:filter, Filter)
157
161
  @options[:file_provider].each do |path, file|
158
162
  verbose {'-------------------------'}
159
163
  if filter; unless filter.filter path, file
@@ -172,6 +176,8 @@ protected
172
176
  rename path, file, text, true if text
173
177
  elsif action == :message
174
178
  result :message, text, path, file
179
+ elsif action == :fileerror
180
+ result :fileerror, text, path, file
175
181
  elsif r == :delete
176
182
  delete path, file
177
183
  elsif r
@@ -183,7 +189,7 @@ protected
183
189
  if md
184
190
  t = md.display
185
191
  end
186
- result :fileerror, "Error: #{e.class} on #{t}\n#{e.message}", path, file
192
+ result :fileerror, "Error: #{e.class} on #{t}: #{e.message}", path, file
187
193
  else
188
194
  raise e
189
195
  end
@@ -3,7 +3,7 @@ class CollectResults
3
3
  attr_accessor :out
4
4
  attr_reader :results
5
5
 
6
- def initialize(out_io)
6
+ def initialize(out_io, width)
7
7
  @verbose = false
8
8
  @quiet = false
9
9
  @silent = false
@@ -13,6 +13,7 @@ class CollectResults
13
13
  @list = false
14
14
 
15
15
  @out = out_io
16
+ @width = width
16
17
 
17
18
  @results = Hash.new do |h, k|
18
19
  case k when :fileerror, :ok, :skipped
@@ -51,14 +52,23 @@ class CollectResults
51
52
  when :skipped: skipped += v.length
52
53
  when :fileerror: errors += v.length
53
54
  end
54
- s = "\n#{File.expand_path(k)}"
55
- s += ": #{mode.to_s.upcase} - #{v.length} files" unless @quiet or @list
56
- @out.puts s
55
+ s = ''
56
+ unless @lineways
57
+ s += "\n#{File.expand_path(k)}"
58
+ s += ": #{mode.to_s.upcase} - #{v.length} files" unless @quiet or @list
59
+ @out.puts s
60
+ end
57
61
  v = v.sort if @sorted
58
62
  if @lineways
59
- @out.puts v.join("\n")
63
+ v.each do |file|
64
+ if @list
65
+ out.puts File.join(k, file)
66
+ else
67
+ @out.puts format('%s: %s', mode.to_s.upcase, File.join(k, file))
68
+ end
69
+ end
60
70
  else
61
- @out.puts v.send(m, 80)
71
+ @out.puts v.send(m, @width)
62
72
  end
63
73
  end
64
74
  else
@@ -66,7 +76,7 @@ class CollectResults
66
76
  if @lineways
67
77
  @out.puts value.join("\n")
68
78
  else
69
- @out.puts value.send(m, 80)
79
+ @out.puts value.send(m, @width)
70
80
  end
71
81
  end
72
82
  end
@@ -0,0 +1,226 @@
1
+ C:\Documents and Settings\Darrick\docs\code\Ruby\RenameFileSet\bin>ruby -rprofile rfs.rb -lR "\Documents and Settings" > \rfs-profile.large.txt
2
+ % cumulative self self total
3
+ time seconds seconds calls ms/call ms/call name
4
+ 15.01 281.14 281.14 23147 12.15 17.58 Integer#times
5
+ 11.83 502.83 221.70 104136 2.13 89.93 Array#each
6
+ 10.67 702.77 199.94 57361 3.49 5.07 Array#collect!
7
+ 5.58 807.22 104.45 29076 3.59 13.33 Options.opt_if
8
+ 5.47 909.64 102.42 3152 32.49 331.70 Integer#upto
9
+ 5.01 1003.46 93.82 29076 3.23 6.34 Options.opt
10
+ 4.00 1078.40 74.94 57361 1.31 8.03 Array#check_cols
11
+ 3.61 1146.03 67.63 29076 2.33 3.54 CollectResults#output
12
+ 3.32 1208.30 62.27 629956 0.10 0.10 Fixnum#+
13
+ 3.02 1264.78 56.48 600691 0.09 0.09 Fixnum#*
14
+ 2.47 1311.11 46.32 482111 0.10 0.10 Array#[]
15
+ 2.34 1354.85 43.74 29076 1.50 3.20 Filter#filter
16
+ 2.06 1393.41 38.56 352182 0.11 0.11 Array#<<
17
+ 1.89 1428.75 35.34 29076 1.22 2.46 Enumerable.find
18
+ 1.83 1463.13 34.37 58152 0.59 0.80 File#directory?
19
+ 1.44 1490.08 26.96 245490 0.11 0.11 String#to_s
20
+ 1.41 1516.52 26.43 29076 0.91 6.07 Renamer#result
21
+ 1.29 1540.66 24.15 32236 0.75 4.56 Proc#call
22
+ 1.19 1562.86 22.20 29076 0.76 0.76 Filter#next_filter
23
+ 1.17 1584.77 21.91 239561 0.09 0.09 String#length
24
+ 1.15 1606.37 21.60 87857 0.25 0.31 Hash#[]
25
+ 1.10 1627.01 20.64 29076 0.71 0.71 Filter#text
26
+ 1.02 1646.05 19.04 29076 0.65 4.20 CommandLineInterface#output
27
+ 0.99 1664.64 18.60 190692 0.10 0.10 Fixnum#>
28
+ 0.99 1683.22 18.57 57361 0.32 1.35 Enumerable.inject
29
+ 0.97 1701.38 18.16 118255 0.15 0.15 Kernel.respond_to?
30
+ 0.86 1717.40 16.02 68238 0.23 0.23 File#join
31
+ 0.64 1729.41 12.01 58243 0.21 0.21 Kernel.block_given?
32
+ 0.53 1739.24 9.84 5042 1.95 1380.72 Provider::File::Recursive#recursive_each
33
+ 0.47 1748.11 8.86 33342 0.27 0.37 Array#*
34
+ 0.44 1756.38 8.27 32643 0.25 0.25 Symbol#===
35
+ 0.42 1764.34 7.96 6308 1.26 1.79 Array#collect
36
+ 0.40 1771.75 7.42 35931 0.21 0.26 Array#<=>
37
+ 0.40 1779.17 7.41 85940 0.09 0.09 Array#length
38
+ 0.38 1786.29 7.12 8631 0.82 927.61 Kernel.send
39
+ 0.38 1793.31 7.02 36110 0.19 0.24 Array#==
40
+ 0.36 1799.99 6.68 29082 0.23 0.23 Kernel.is_a?
41
+ 0.35 1806.59 6.60 29076 0.23 0.23 Regexp#=~
42
+ 0.34 1812.90 6.31 29122 0.22 0.22 Array#empty?
43
+ 0.32 1818.88 5.98 3152 1.90 337.64 Array#columns
44
+ 0.27 1823.98 5.10 57361 0.09 0.09 Fixnum#<
45
+ 0.24 1828.55 4.57 1 4573.00 13941.00 Array#sort
46
+ 0.21 1832.58 4.03 5043 0.80 1.44 Enumerable.collect
47
+ 0.19 1836.14 3.56 43122 0.08 0.08 Array#last
48
+ 0.18 1839.54 3.41 6305 0.54 0.83 IO#puts
49
+ 0.17 1842.75 3.21 5042 0.64 0.64 Dir#each
50
+ 0.15 1845.61 2.86 33342 0.09 0.09 Fixnum#%
51
+ 0.15 1848.42 2.81 33342 0.08 0.08 Fixnum#-
52
+ 0.13 1850.81 2.38 23147 0.10 0.10 Fixnum#/
53
+ 0.12 1853.00 2.19 5107 0.43 0.73 Class#new
54
+ 0.11 1855.08 2.08 3171 0.65 1.70 Hash#default
55
+ 0.10 1857.03 1.95 35931 0.05 0.05 String#<=>
56
+ 0.10 1858.81 1.78 21584 0.08 0.08 Fixnum#==
57
+ 0.08 1860.31 1.50 5042 0.30 0.30 Dir#initialize
58
+ 0.07 1861.71 1.39 13937 0.10 0.10 Kernel.format
59
+ 0.07 1863.07 1.36 12536 0.11 0.11 Array#transpose
60
+ 0.07 1864.37 1.30 19855 0.07 0.07 Kernel.==
61
+ 0.07 1865.65 1.27 12617 0.10 0.10 IO#write
62
+ 0.06 1866.83 1.18 13937 0.08 0.08 String#rstrip
63
+ 0.05 1867.71 0.88 3580 0.25 0.25 Hash#[]=
64
+ 0.05 1868.58 0.87 10195 0.09 0.09 Array#+
65
+ 0.04 1869.30 0.72 8690 0.08 0.08 Fixnum#to_s
66
+ 0.04 1870.01 0.71 6308 0.11 0.11 Array#join
67
+ 0.02 1870.39 0.38 437 0.87 1.01 OptionParser::List#search
68
+ 0.02 1870.72 0.33 52 6.35 53.12 Kernel.require
69
+ 0.02 1871.02 0.30 3153 0.10 0.10 File#expand_path
70
+ 0.01 1871.29 0.27 3153 0.09 0.09 String#*
71
+ 0.01 1871.53 0.24 147 1.63 5.65 Array#reverse_each
72
+ 0.01 1871.76 0.23 23 10.00 23.91 Kernel.require__
73
+ 0.01 1871.94 0.18 34 5.29 10.29 Array#delete_if
74
+ 0.01 1872.05 0.11 2267 0.05 0.05 Kernel.hash
75
+ 0.01 1872.15 0.10 34 2.94 4.12 Hash#update
76
+ 0.00 1872.24 0.09 68 1.32 2.06 Hash#invert
77
+ 0.00 1872.32 0.08 318 0.25 0.31 Kernel.===
78
+ 0.00 1872.39 0.07 147 0.48 6.54 OptionParser#search
79
+ 0.00 1872.46 0.07 152 0.46 0.59 String#gsub!
80
+ 0.00 1872.52 0.06 437 0.14 0.14 Hash#fetch
81
+ 0.00 1872.58 0.06 147 0.41 6.06 OptionParser#visit
82
+ 0.00 1872.64 0.06 1186 0.05 0.05 Module#method_added
83
+ 0.00 1872.69 0.05 34 1.47 45.35 OptionParser#make_switch
84
+ 0.00 1872.74 0.05 1212 0.04 0.04 Module#===
85
+ 0.00 1872.78 0.04 29 1.41 24.17 OptionParser::List#append
86
+ 0.00 1872.82 0.04 21 1.90 1.90 OptionParser#notwice
87
+ 0.00 1872.86 0.04 169 0.24 0.24 Class#inherited
88
+ 0.00 1872.90 0.04 74 0.54 0.54 Array#push
89
+ 0.00 1872.94 0.04 34 1.18 20.29 OptionParser::List#update
90
+ 0.00 1872.98 0.04 152 0.26 0.86 Config.expand
91
+ 0.00 1873.01 0.03 29 1.03 72.17 OptionParser#define
92
+ 0.00 1873.04 0.03 1 30.00 30.00 Profiler__.start_profile
93
+ 0.00 1873.06 0.02 6 3.50 3.50 Hash#each_pair
94
+ 0.00 1873.08 0.02 29 0.69 72.86 OptionParser#on
95
+ 0.00 1873.10 0.02 17 1.18 1.18 Kernel.eval
96
+ 0.00 1873.12 0.02 9 2.22 3.33 Gem::Specification#array_attribute
97
+ 0.00 1873.14 0.02 37 0.54 0.81 Module#attr_accessor
98
+ 0.00 1873.15 0.01 11 0.91 0.91 Module#module_eval
99
+ 0.00 1873.16 0.01 10 1.00 1.00 String#to_i
100
+ 0.00 1873.17 0.01 34 0.29 0.29 String#downcase
101
+ 0.00 1873.18 0.01 19 0.53 1.05 Gem::Specification#attribute
102
+ 0.00 1873.19 0.01 5 2.00 52.00 OptionParser#on_head
103
+ 0.00 1873.20 0.01 108 0.09 0.09 String#initialize_copy
104
+ 0.00 1873.21 0.01 109 0.09 0.18 Kernel.dup
105
+ 0.00 1873.22 0.01 4 2.50 272401.75 Hash#each
106
+ 0.00 1873.23 0.01 1 10.00 120.00 Hash#each_value
107
+ 0.00 1873.24 0.01 20 0.50 0.50 Module#include
108
+ 0.00 1873.25 0.01 47 0.21 0.21 OptionParser#top
109
+ 0.00 1873.26 0.01 2 5.00 5.00 OptionParser::Switch::NoArgument#parse
110
+ 0.00 1873.27 0.01 517 0.02 0.02 Regexp#===
111
+ 0.00 1873.28 0.01 1 10.00 1872282.00 CommandLineInterface#start
112
+ 0.00 1873.28 0.00 1 0.00 0.00 File#dirname
113
+ 0.00 1873.28 0.00 1 0.00 0.00 PrepareRegexp#each
114
+ 0.00 1873.28 0.00 1 0.00 780312.00 Provider::File::Recursive#each
115
+ 0.00 1873.28 0.00 2 0.00 0.00 Gem::Specification#read_only
116
+ 0.00 1873.28 0.00 7 0.00 0.00 Array#unshift
117
+ 0.00 1873.28 0.00 18 0.00 0.00 Module#define_method
118
+ 0.00 1873.28 0.00 20 0.00 0.00 Module#included
119
+ 0.00 1873.28 0.00 1 0.00 0.00 CommandLineInterface#confirm
120
+ 0.00 1873.28 0.00 1 0.00 0.00 OptionParser#base
121
+ 0.00 1873.28 0.00 19 0.00 0.00 String#==
122
+ 0.00 1873.28 0.00 1 0.00 780312.00 Renamer#rename_each
123
+ 0.00 1873.28 0.00 12 0.00 0.00 Module#>=
124
+ 0.00 1873.28 0.00 2 0.00 0.00 OptionParser::Switch#conv_arg
125
+ 0.00 1873.28 0.00 13 0.00 0.77 OptionParser::List#accept
126
+ 0.00 1873.28 0.00 4 0.00 0.00 Proc#new
127
+ 0.00 1873.28 0.00 1 0.00 0.00 Gem::Version::Requirement#initialize
128
+ 0.00 1873.28 0.00 1 0.00 0.00 String#upcase
129
+ 0.00 1873.28 0.00 3 0.00 0.00 Kernel.puts
130
+ 0.00 1873.28 0.00 1 0.00 10.00 OptionParser#parse
131
+ 0.00 1873.28 0.00 5 0.00 4.20 Gem::Security::Policy#initialize
132
+ 0.00 1873.28 0.00 20 0.00 0.00 Module#append_features
133
+ 0.00 1873.28 0.00 1 0.00 780312.00 RenameFunctions.rename_replace
134
+ 0.00 1873.28 0.00 1 0.00 0.00 OptionParser::Arguable.extend_object
135
+ 0.00 1873.28 0.00 19 0.00 0.00 Kernel.freeze
136
+ 0.00 1873.28 0.00 38 0.00 0.00 OptionParser::Switch#initialize
137
+ 0.00 1873.28 0.00 1 0.00 0.00 String#sub
138
+ 0.00 1873.28 0.00 1 0.00 10.00 OptionParser#parse!
139
+ 0.00 1873.28 0.00 3 0.00 0.00 Module#public
140
+ 0.00 1873.28 0.00 2 0.00 0.00 Module#private_class_method
141
+ 0.00 1873.28 0.00 7 0.00 0.00 Regexp#quote
142
+ 0.00 1873.28 0.00 6 0.00 0.00 Kernel.kind_of?
143
+ 0.00 1873.28 0.00 3 0.00 0.00 Numeric#nonzero?
144
+ 0.00 1873.28 0.00 1 0.00 780312.00 Method#call
145
+ 0.00 1873.28 0.00 1 0.00 1873273.00 #toplevel
146
+ 0.00 1873.28 0.00 1 0.00 0.00 Kernel.instance_eval
147
+ 0.00 1873.28 0.00 7 0.00 0.00 Kernel.lambda
148
+ 0.00 1873.28 0.00 2 0.00 0.00 Forwardable.def_instance_delegator
149
+ 0.00 1873.28 0.00 5 0.00 6.00 OptionParser::List#prepend
150
+ 0.00 1873.28 0.00 1 0.00 0.00 Gem.user_home
151
+ 0.00 1873.28 0.00 1 0.00 780312.00 Renamer#run
152
+ 0.00 1873.28 0.00 2 0.00 5.00 Gem::Specification#attributes
153
+ 0.00 1873.28 0.00 1 0.00 0.00 Array#initialize_copy
154
+ 0.00 1873.28 0.00 22 0.00 0.00 OptionParser::Switch::NoArgument#pattern
155
+ 0.00 1873.28 0.00 10 0.00 0.00 Module#remove_method
156
+ 0.00 1873.28 0.00 8 0.00 0.00 Array#first
157
+ 0.00 1873.28 0.00 1 0.00 0.00 Gem.ssl_available?
158
+ 0.00 1873.28 0.00 2 0.00 0.00 NilClass#nil?
159
+ 0.00 1873.28 0.00 1 0.00 0.00 Array#size
160
+ 0.00 1873.28 0.00 12 0.00 0.00 OptionParser::Switch::NoArgument#guess
161
+ 0.00 1873.28 0.00 1 0.00 0.00 Filter#initialize
162
+ 0.00 1873.28 0.00 19 0.00 0.00 Module#class_eval
163
+ 0.00 1873.28 0.00 1 0.00 0.00 ENV.include?
164
+ 0.00 1873.28 0.00 5 0.00 0.00 Hash#initialize_copy
165
+ 0.00 1873.28 0.00 3 0.00 0.00 Module#module_function
166
+ 0.00 1873.28 0.00 1 0.00 0.00 Filter#each
167
+ 0.00 1873.28 0.00 3 0.00 0.00 Fixnum#zero?
168
+ 0.00 1873.28 0.00 1 0.00 1089577.00 CollectResults#display
169
+ 0.00 1873.28 0.00 1 0.00 0.00 OptionParser#initialize
170
+ 0.00 1873.28 0.00 1 0.00 0.00 Forwardable.def_delegators
171
+ 0.00 1873.28 0.00 21 0.00 0.00 Module#private
172
+ 0.00 1873.28 0.00 5 0.00 0.00 Object#initialize
173
+ 0.00 1873.28 0.00 1 0.00 0.00 String#scan
174
+ 0.00 1873.28 0.00 1 0.00 0.00 String#split
175
+ 0.00 1873.28 0.00 10 0.00 0.00 Module#method_removed
176
+ 0.00 1873.28 0.00 1 0.00 10.00 OptionParser#order!
177
+ 0.00 1873.28 0.00 4 0.00 0.00 Array#shift
178
+ 0.00 1873.28 0.00 437 0.00 0.00 Kernel.__send__
179
+ 0.00 1873.28 0.00 3 0.00 0.00 Kernel.proc
180
+ 0.00 1873.28 0.00 4 0.00 0.00 Module#protected
181
+ 0.00 1873.28 0.00 1 0.00 0.00 String#[]
182
+ 0.00 1873.28 0.00 9 0.00 0.00 Module#const_set
183
+ 0.00 1873.28 0.00 1 0.00 0.00 Gem::Version#initialize
184
+ 0.00 1873.28 0.00 3 0.00 0.00 OptionParser::List#initialize
185
+ 0.00 1873.28 0.00 3 0.00 0.00 Module#method_undefined
186
+ 0.00 1873.28 0.00 1 0.00 0.00 OptionParser#add_officious
187
+ 0.00 1873.28 0.00 3 0.00 0.00 Module#extend_object
188
+ 0.00 1873.28 0.00 3 0.00 0.00 Regexp#hash
189
+ 0.00 1873.28 0.00 1 0.00 0.00 String#sub!
190
+ 0.00 1873.28 0.00 1 0.00 780312.00 Renamer#rename_each_match
191
+ 0.00 1873.28 0.00 1 0.00 0.00 Kernel.method
192
+ 0.00 1873.28 0.00 1 0.00 10.00 Kernel.catch
193
+ 0.00 1873.28 0.00 12 0.00 0.00 OptionParser::Switch::NoArgument#incompatible_argument_styles
194
+ 0.00 1873.28 0.00 5 0.00 50.00 OptionParser#define_head
195
+ 0.00 1873.28 0.00 15 0.00 0.00 Module#attr_reader
196
+ 0.00 1873.28 0.00 5 0.00 0.00 Hash#merge
197
+ 0.00 1873.28 0.00 3 0.00 0.00 Kernel.extend
198
+ 0.00 1873.28 0.00 1 0.00 0.00 File#split
199
+ 0.00 1873.28 0.00 2 0.00 0.00 Module#attr
200
+ 0.00 1873.28 0.00 1 0.00 0.00 Module#alias_method
201
+ 0.00 1873.28 0.00 2 0.00 0.00 Regexp#initialize
202
+ 0.00 1873.28 0.00 105 0.00 0.00 Hash#keys
203
+ 0.00 1873.28 0.00 1 0.00 0.00 Gem::Version#correct?
204
+ 0.00 1873.28 0.00 4 0.00 0.00 Gem::Specification#attribute_alias_singular
205
+ 0.00 1873.28 0.00 10 0.00 0.00 Hash#initialize
206
+ 0.00 1873.28 0.00 1 0.00 0.00 Provider::File::Base#initialize
207
+ 0.00 1873.28 0.00 1 0.00 0.00 Gem::Version::Requirement#default
208
+ 0.00 1873.28 0.00 7 0.00 1.43 Gem::Specification#required_attribute
209
+ 0.00 1873.28 0.00 3 0.00 0.00 Module#extended
210
+ 0.00 1873.28 0.00 2 0.00 0.00 MatchData#[]
211
+ 0.00 1873.28 0.00 71 0.00 0.00 Symbol#to_s
212
+ 0.00 1873.28 0.00 1 0.00 0.00 Gem.find_home
213
+ 0.00 1873.28 0.00 1 0.00 0.00 CollectResults#initialize
214
+ 0.00 1873.28 0.00 14 0.00 0.00 Regexp#to_s
215
+ 0.00 1873.28 0.00 1 0.00 0.00 IO#print
216
+ 0.00 1873.28 0.00 1 0.00 0.00 Time#gm
217
+ 0.00 1873.28 0.00 1 0.00 0.00 Array#[]=
218
+ 0.00 1873.28 0.00 196 0.00 0.00 Kernel.singleton_method_added
219
+ 0.00 1873.28 0.00 1 0.00 0.00 Gem::Version::Requirement#parse
220
+ 0.00 1873.28 0.00 3 0.00 0.00 ENV.[]
221
+ 0.00 1873.28 0.00 10 0.00 0.00 Gem::Specification#overwrite_accessor
222
+ 0.00 1873.28 0.00 5 0.00 0.00 Module#attr_writer
223
+ 0.00 1873.28 0.00 13 0.00 1.54 OptionParser#accept
224
+ 0.00 1873.28 0.00 1 0.00 10.00 OptionParser#permute!
225
+ 0.00 1873.28 0.00 2 0.00 0.00 Regexp#match
226
+
@@ -1,4 +1,4 @@
1
- require 'test/test_helper'
1
+ require '../lib/test/test_helper'
2
2
 
3
3
  class TestBasics < BaseTest
4
4
  def test_list
@@ -315,11 +315,19 @@ end
315
315
  class TestCount < BaseTest
316
316
  def test_count
317
317
  count = 0
318
- r :preview, :rename_count, 3, :search => /()./ do |h|
318
+ r :preview, :rename_count, 3, :search => (/()./), :count_start => 1 do |h|
319
319
  count += 1
320
320
  assert_equal "#{count}#{h[:oldfn]}", h[:newfn]
321
321
  end
322
322
  end
323
+
324
+ def test_count_string
325
+ count = "b.9 "
326
+ r :preview, :rename_count, 3, :search => (/()./), :count_start => count do |h|
327
+ assert_equal "#{count}#{h[:oldfn]}", h[:newfn]
328
+ count.succ!
329
+ end
330
+ end
323
331
  end
324
332
 
325
333
 
metadata CHANGED
@@ -3,9 +3,9 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: rfs
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.1"
7
- date: 2005-08-30 00:00:00 -04:00
8
- summary: A utility that allows you to use regular expressions to manage large sets of files or folders.
6
+ version: "0.2"
7
+ date: 2005-09-03 00:00:00 -04:00
8
+ summary: A utility that allows you to use regular expressions to rename large sets of files or folders. Fxruby and cmd-line interfaces.
9
9
  require_paths:
10
10
  - lib
11
11
  email: darrick@innatesoftware.com
@@ -33,7 +33,9 @@ files:
33
33
  - "./description.txt"
34
34
  - "./lib"
35
35
  - "./MIT-LICENSE"
36
+ - "./profile.txt"
36
37
  - "./Rakefile.rb"
38
+ - "./README"
37
39
  - "./tests"
38
40
  - "./bin/RenameFileSet.rbw"
39
41
  - "./bin/rfs.rb"
@@ -46,7 +48,6 @@ files:
46
48
  - "./lib/options.rb"
47
49
  - "./lib/providers.rb"
48
50
  - "./lib/regexp.rb"
49
- - "./lib/RenameFileSet.bak.rb"
50
51
  - "./lib/renamer.rb"
51
52
  - "./lib/rename_functions.rb"
52
53
  - "./lib/results.rb"
@@ -88,12 +89,13 @@ files:
88
89
  - "./lib/innate/test/files/reloadtarget2.rb"
89
90
  - "./lib/test/test_helper.rb"
90
91
  - "./tests/dir"
91
- - "./tests/test_helper.rb"
92
92
  - "./tests/test_rename_functions.rb"
93
93
  - "./tests/dir/file1"
94
94
  - "./tests/dir/file2"
95
95
  - "./tests/dir/file3"
96
- test_files: []
96
+ test_files:
97
+ - "./tests/test_rename_functions.rb"
98
+ - "./lib/innate/test/all_tests.rb"
97
99
  rdoc_options: []
98
100
  extra_rdoc_files: []
99
101
  executables:
@@ -1,372 +0,0 @@
1
- require 'rubygems'
2
- require_gem 'fxruby'
3
- require 'innate/mkdirs'
4
- require 'innate/roman'
5
- require 'innate/titlecase'
6
-
7
- include Fox
8
-
9
- #TO DO:
10
- # change pattern to combobox with some useful patterns
11
- # \d.+(\d+).+
12
- # (\d+[AB])
13
-
14
- #user interface
15
- class RenameWindow < FXMainWindow
16
- def initialize(app)
17
- super(app, 'Rename File Set', nil, nil, DECOR_ALL, 20, 100, 1000, 500)
18
-
19
- FXHorizontalFrame.new(self, LAYOUT_FILL | PACK_UNIFORM_WIDTH) { |mainFrame|
20
- # navigation on left
21
- @dirList = FXDirList.new mainFrame, nil, 0, LAYOUT_FILL | FRAME_NORMAL
22
- # controls in centre
23
- FXVerticalFrame.new(mainFrame, LAYOUT_FILL | PACK_UNIFORM_WIDTH) { |controls|
24
- FXLabel.new controls, 'Search (replace first capture):', nil, LAYOUT_FILL_X | JUSTIFY_LEFT
25
- @search = FXTextField.new controls, 0, nil, LAYOUT_FILL
26
-
27
- FXGroupBox.new(controls, 'Replacement Method', LAYOUT_FILL | FRAME_GROOVE | JUSTIFY_LEFT ) { |group|
28
- @selReplace = FXRadioButton.new(group, 'Replace with:') { |r|
29
- connectRadio r
30
- }
31
- @replace = FXTextField.new group, 30, nil, PACK_UNIFORM_WIDTH
32
- @selAdd = FXRadioButton.new(group, 'Add to captured digits: ', nil, LAYOUT_FILL_Y) { |r|
33
- connectRadio r
34
- }
35
- @add = FXSpinner.new(group, 10, nil, FRAME_NORMAL) { |s| s.range = (-500..500)}
36
- @selRoman = FXRadioButton.new(group, 'Convert to Roman Numerals', nil, LAYOUT_FILL_X ) { |r|
37
- connectRadio r
38
- }
39
- @selTapeNumbers = FXRadioButton.new(group, 'Convert Tape Numbers', nil, LAYOUT_FILL_X) { |r|
40
- connectRadio r
41
- }
42
- @selFill = FXRadioButton.new(group, 'Replace capture with last match capture:', nil, LAYOUT_FILL_X) { |r|
43
- connectRadio r
44
- }
45
- @fill = FXTextField.new group, 30, nil, PACK_UNIFORM_WIDTH
46
- @selFile = FXRadioButton.new(group, 'Replace from files.txt', nil, LAYOUT_FILL_X) { |r|
47
- connectRadio r
48
- }
49
- @selCount = FXRadioButton.new(group, 'Count', nil, LAYOUT_FILL_X) { |r|
50
- connectRadio r
51
- }
52
- @selMarkChange = FXRadioButton.new(group, 'Mark Changes', nil, LAYOUT_FILL_X) { |r|
53
- connectRadio r
54
- }
55
- @selFromFullName = FXRadioButton.new(group, 'Replace with capture in full name:', nil, LAYOUT_FILL_X) { |r|
56
- connectRadio r
57
- }
58
- @fullNamePattern = FXTextField.new group, 30, nil, PACK_UNIFORM_WIDTH
59
- @selMoveUp = FXRadioButton.new(group, 'Move to Parent Directory', nil, LAYOUT_FILL_X) { |r|
60
- connectRadio r
61
- }
62
- @selCapitalize = FXRadioButton.new(group, 'Capitalize', nil, LAYOUT_FILL_X) { |r|
63
- connectRadio r
64
- }
65
- }
66
-
67
- FXButton.new(controls, '&List Files', nil, nil, 0, LAYOUT_FILL_X | BUTTON_NORMAL) { |b|
68
- b.connect SEL_COMMAND, method(:onListClick)
69
- }
70
- FXButton.new(controls, '&Preview', nil, nil, 0, LAYOUT_FILL_X | FRAME_RAISED | FRAME_THICK) { |b|
71
- b.connect SEL_COMMAND, method(:onPreviewClick)
72
- }
73
- FXButton.new(controls, '&Apply', nil, nil, 0, LAYOUT_FILL_X | FRAME_RAISED | FRAME_THICK) { |b|
74
- b.connect SEL_COMMAND, method(:onReplaceClick)
75
- }
76
- }
77
- # results on right
78
- @results = FXText.new mainFrame, nil, 0, LAYOUT_FILL | FRAME_NORMAL | TEXT_READONLY
79
- }
80
-
81
- @selReplace.checkState = TRUE
82
- onRadioUpdate @selReplace, nil, nil
83
- end
84
-
85
- def connectRadio(r)
86
- r.connect SEL_COMMAND, method(:onRadioUpdate)
87
- end
88
-
89
- def onPreviewClick(sender, sel, ptr)
90
- start false
91
- end
92
-
93
- def onReplaceClick(sender, sel, ptr)
94
- start true
95
- end
96
-
97
- def onListClick(sender, sel, ptr)
98
- start false, true
99
- @dirList.setFocus
100
- end
101
-
102
- def onRadioUpdate(sender, sel, ptr)
103
- sender.parent.each_child do |c|
104
- if FXRadioButton === c and c != sender
105
- c.checkState = FALSE
106
- end
107
- end
108
- @selReplace.checked? ? @replace.enable : @replace.disable
109
- @selAdd.checked? ? @add.enable : @add.disable
110
- @selFill.checked? ? @fill.enable : @fill.disable
111
- @selFromFullName.checked? ? @fullNamePattern.enable : @fullNamePattern.disable
112
- end
113
- end
114
-
115
- #the real work of it
116
- class RenameWindow
117
-
118
- def start(commit, listonly = false)
119
- @results.text = ''
120
- traverseList @dirList, commit, listonly
121
- end
122
-
123
- def traverseList(list, commit, listonly)
124
- list.each do |i|
125
- if i.selected?
126
- doReplace @dirList.itemPathname(i), commit, listonly
127
- end
128
- traverseList(i, commit, listonly) if i.expanded?
129
- end
130
- end
131
-
132
- def subst(src, matchdata, repl)
133
- return src unless matchdata and matchdata[1]
134
- t = repl
135
- c = 1
136
- matchdata.to_a[c..-1].each do |m|
137
- t.gsub!("%#{c}", m ? m : '')
138
- c += 1
139
- end
140
- src[0, matchdata.offset(1)[0]] + (t == nil ? '' : t) + src[matchdata.offset(1)[1], src.length]
141
- end
142
-
143
- def result(t)
144
- @results.text += t.to_s + "\n"
145
- end
146
-
147
- def rename(path, oldfn, newfn, commit)
148
- unless oldfn == newfn
149
- t = newfn
150
- if commit
151
- begin
152
- fn = File.join(path, newfn)
153
- mkdirs File.split(fn).first
154
- File.rename(File.join(path, oldfn), fn)
155
- rescue
156
- t += ' - Error'
157
- else
158
- t += ' - OK'
159
- end
160
- end
161
- result t
162
- end
163
- end
164
-
165
- def rename_each_match(re, path, commit)
166
- return unless Regexp === re
167
- dir = Dir.new(path) rescue return
168
- dir.each do |file|
169
- next if /^\.+$/ =~ file
170
- md = re.match(file)
171
- if md
172
- newfn = yield(file, md)
173
- rename path, file, newfn, commit if newfn
174
- end
175
- end
176
- end
177
-
178
- def new_regexp(pattern)
179
- begin
180
- Regexp.new(pattern)
181
- rescue
182
- result "Error parsing regular expression:\n#{$!}\n------------"
183
- nil
184
- end
185
- end
186
-
187
- def doReplace(path, commit, listonly)
188
- re = new_regexp @search.text
189
-
190
- if listonly
191
- Dir.new(path).each do |file, md|
192
- result "#{(File.directory?(File.join(path, file)) ? '\\' : '')}#{file}"
193
- end
194
- return
195
- end
196
-
197
- if @selReplace.checked?
198
- rename_each_match(re, path, commit) do |file, md|
199
- if md.length >= 2
200
- subst(file, md, @replace.text)
201
- else
202
- file.sub(re, @replace.text)
203
- end
204
- end
205
- elsif @selFile.checked?
206
- fn = File.join(path, 'files.txt')
207
- return unless File.exist? fn
208
- File.open(fn) do |f|
209
- rename_each_match(re, path, commit) do |file, md|
210
- if md.length >= 2
211
- t = f.readline.chomp rescue '--no data--'
212
- subst(file, md, t)
213
- end
214
- end
215
- end
216
- elsif @selAdd.checked?
217
- rename_each_match(re, path, commit) do |file, md|
218
- if md.length >= 2
219
- int = md[1].to_i rescue int = md[1] rescue int = 0
220
- subst(file, md, (int + @add.value).to_s)
221
- end
222
- end
223
- elsif @selCount.checked?
224
- count = 0
225
- rename_each_match(re, path, commit) do |file, md|
226
- if md.length >= 2
227
- count += 1
228
- subst(file, md, count.to_s)
229
- end
230
- end
231
- elsif @selFromFullName.checked?
232
- r = new_regexp(@fullNamePattern.text)
233
- if r
234
- rename_each_match(re, path, commit) do |file, md|
235
- fn = path + '\\' + file
236
- m = r.match(fn)
237
- if m
238
- if m.length >= 2
239
- replace = m[1]
240
- else
241
- replace = m
242
- end
243
- if md.length >= 2
244
- subst(file, md, replace)
245
- else
246
- file.sub(re, replace)
247
- end
248
- else
249
- result "no match: #{fn}"
250
- nil
251
- end
252
- end
253
- end
254
- elsif @selMoveUp.checked?
255
- p = File.split(path).first
256
- unless p == '\\'
257
- rename_each_match(re, path, commit) do |file, md|
258
- d = File.join(p, file)
259
- File.rename File.join(path, file), d if commit
260
- result d
261
- nil
262
- end
263
- else
264
- nil
265
- end
266
- elsif @selRoman.checked?
267
- rename_each_match(re, path, commit) do |file, md|
268
- if md.length >= 2
269
- t = md[1].to_i rescue t = md[1] rescue t = 0
270
- subst(file, md, t.roman) if Integer === t
271
- end
272
- end
273
- elsif @selMarkChange.checked?
274
- prev = ''
275
- rename_each_match(re, path, commit) do |file, md|
276
- if md.length >= 2
277
- unless prev == md[1]
278
- prev = md[1]
279
- subst(file, /()\..+?$/.match(file), '-change-')
280
- end
281
- end
282
- end
283
- elsif @selTapeNumbers.checked?
284
- rename_each_match(re, path, commit) do |file, md|
285
- if md.length >= 2
286
- t = md[1]
287
- n = t.to_i * 2 + (/a/i =~ t[-1,1] ? -1 : 0)
288
- n += 2 if n < 1
289
- subst(file, md, n.to_s)
290
- end
291
- end
292
- elsif @selCapitalize.checked?
293
- always_lower = %w{ vol
294
- mp3 wav txt avi nfo pdf rar zip rb ape sfk mpg rm qt doc }
295
- lower = %w{ as of in the and at or a to is on by for with an } + always_lower
296
- split_chars = /[ �\-().]/
297
- upper = /^[MDCLXVIW]+$/
298
- rename_each_match(re, path, commit) do |file, md|
299
- o = md[1]
300
- prev = ''
301
- if md.length >= 2
302
- res = o.split(split_chars).inject([]) do |r, w|
303
- if r.empty? or prev =~ /^([\d.]+|.*[;.!_]|)$/
304
- if always_lower.include?(w.downcase)
305
- r << w.downcase
306
- elsif w =~ upper
307
- r << w.upcase
308
- else
309
- r << w.capitalize
310
- end
311
- else
312
- if lower.include?(w.downcase)
313
- r << w.downcase
314
- elsif w =~ upper
315
- r << w.upcase
316
- else
317
- r << w.capitalize
318
- end
319
- end
320
- prev = w
321
- r
322
- end
323
- r = res.inject('') do |s, w|
324
- unless w.empty?
325
- until o[s.length, 1] =~ Regexp.new(Regexp.escape(w[0,1]), 'i') do
326
- s << o[s.length, 1]
327
- end
328
- s << w
329
- end
330
- s
331
- end
332
- subst(file, md, r)
333
- end
334
- end
335
- elsif @selFill.checked?
336
- begin
337
- fillRE = Regexp.new(@fill.text)
338
- rescue
339
- result "Error parsing regular expression:\n#{$!}\n------------"
340
- return
341
- end
342
- fillText = ''
343
- dir = Dir.new(path) rescue return
344
- dir.each do |file|
345
- next if /^\.+$/ =~ file
346
- md = re.match(file)
347
- if md or fillText
348
- if md and md.length >= 2 and md[1].length > 0 and not (fillRE and fillRE.match(file))
349
- fillText = md[1]
350
- result "source: #{file}"
351
- else
352
- md = fillRE.match(file)
353
- if md and md.length >= 2
354
- t = subst(file, md, fillText)
355
- rename path, file, t, commit
356
- else
357
- result "* No capture to fill * #{file}"
358
- end
359
- end
360
- end
361
- end
362
- end
363
- end
364
- end
365
-
366
- if $0 == __FILE__
367
- theApp = FXApp.new
368
- theMainWindow = RenameWindow.new theApp
369
- theApp.create
370
- theMainWindow.show
371
- theApp.run
372
- end