AbsoluteRenamer 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/AbsoluteRenamer.gemspec +26 -87
- data/Gemfile +4 -0
- data/README.rdoc +3 -1
- data/Rakefile +2 -59
- data/bin/absrenamer +4 -6
- data/conf/absrenamer/absrenamer.conf +1 -1
- data/lib/absolute_renamer.rb +99 -103
- data/lib/absolute_renamer/config.rb +43 -34
- data/lib/absolute_renamer/core-packages/core-case/module.rb +23 -24
- data/lib/absolute_renamer/core-packages/core-general/module.rb +106 -128
- data/lib/absolute_renamer/core-packages/core-general/parser.rb +75 -64
- data/lib/absolute_renamer/core-packages/core-interactive/parser.rb +15 -15
- data/lib/absolute_renamer/core-packages/core-interactive/plugin.rb +29 -29
- data/lib/absolute_renamer/core-packages/core-listing/parser.rb +6 -6
- data/lib/absolute_renamer/core-packages/core-listing/plugin.rb +10 -10
- data/lib/absolute_renamer/external.rb +48 -48
- data/lib/absolute_renamer/file_info.rb +87 -88
- data/lib/absolute_renamer/imodule.rb +69 -72
- data/lib/absolute_renamer/iparser.rb +4 -4
- data/lib/absolute_renamer/iplugin.rb +6 -6
- data/lib/absolute_renamer/libs/file.rb +8 -8
- data/lib/absolute_renamer/libs/hash.rb +23 -23
- data/lib/absolute_renamer/libs/string.rb +21 -21
- data/lib/absolute_renamer/parser.rb +57 -58
- data/lib/absolute_renamer/use_config.rb +6 -6
- data/lib/absolute_renamer/version.rb +3 -0
- data/lib/absolute_renamer/with_children.rb +15 -15
- data/test/config_test.rb +1 -8
- data/test/file_info_test.rb +23 -22
- data/test/imodule_test.rb +1 -8
- metadata +54 -18
- data/VERSION +0 -1
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'absolute_renamer/with_children'
|
2
2
|
|
3
3
|
module AbsoluteRenamer
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
# Parsers parent class.
|
5
|
+
# Parsers must inherit of it.
|
6
|
+
class IParser < AbsoluteRenamer::WithChildren
|
7
|
+
end
|
8
8
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'absolute_renamer/with_children'
|
2
2
|
|
3
3
|
module AbsoluteRenamer
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
4
|
+
# Plugins parent class.
|
5
|
+
# Plugins must inherit of it.
|
6
|
+
class IPlugin < AbsoluteRenamer::WithChildren
|
7
|
+
def self.symbol
|
8
|
+
name.intern
|
10
9
|
end
|
10
|
+
end
|
11
11
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
# Extension of existing File class.
|
2
2
|
class << File
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
3
|
+
# Returns the extension of a file.
|
4
|
+
# path: the path of the file
|
5
|
+
# dot: starting from the end, number of dots to count before cuting extension.
|
6
|
+
def extname(path, dot = 1)
|
7
|
+
pattern = (0...dot).inject('') { |pat,x| pat << '\.[^\.]+' } << '$'
|
8
|
+
ext = File.basename(path).match(pattern).to_s
|
9
|
+
ext.empty? ? "" : ext[1..ext.length]
|
10
|
+
end
|
11
11
|
end
|
@@ -1,28 +1,28 @@
|
|
1
1
|
# Extension of existing Hash class
|
2
2
|
class Hash
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
3
|
+
# Returns a new hash containing resulting of a recursive merge between two hashes
|
4
|
+
# new_hash: the hash to merge
|
5
|
+
# h1 = { :key1 => :val1, :key2 => { :key21 => :val21 } }
|
6
|
+
# h2 = { :key2 => { :key22 => :val22 }, :key3 => val3 }
|
7
|
+
# h1.deep_marge! h2 #=> { :key1 => val1, :key2 => { :key21 => :val21. :key22 => :val22 }, test3 => val3 }
|
8
|
+
# h1 #=> { :key1 => :val1, :key2 => { :key21 => :val21 } }
|
9
|
+
def deep_merge(new_hash)
|
10
|
+
self.clone.deep_merge! new_hash
|
11
|
+
end
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
13
|
+
# Adds the contents of new_hash to another hash recusively
|
14
|
+
# new_hash: the hash to merge
|
15
|
+
# h1 = { :key1 => :val1, :key2 => { :key21 => :val21 } }
|
16
|
+
# h2 = { :key2 => { :key22 => :val22 }, :key3 => val3 }
|
17
|
+
# h1.deep_marge! h2
|
18
|
+
# h1 #=> { :key1 => val1, :key2 => { :key21 => :val21. :key22 => :val22 }, test3 => val3 }
|
19
|
+
def deep_merge!(new_hash)
|
20
|
+
merge! new_hash do |key, v1, v2|
|
21
|
+
if v1.is_a? Hash
|
22
|
+
v1.deep_merge! v2
|
23
|
+
else
|
24
|
+
self[key] = v2
|
25
|
+
end
|
27
26
|
end
|
27
|
+
end
|
28
28
|
end
|
@@ -1,26 +1,26 @@
|
|
1
1
|
# Extension of existing String class
|
2
2
|
class String
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
3
|
+
# Returns a camelized version of a string
|
4
|
+
# word_separator: a regular expression used to separate words.
|
5
|
+
# str = "hello.THE world"
|
6
|
+
# str.camelize # => "Hello.The World"
|
7
|
+
# str # => "hello.THE world"
|
8
|
+
def camelize
|
9
|
+
self.clone.camelize!
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
-
self
|
12
|
+
# Camelizes a string and returns it.
|
13
|
+
# str = "Hello.THE World"
|
14
|
+
# str.camelize! # => "Hello.The World"
|
15
|
+
# str # => "Hello.The World"
|
16
|
+
def camelize!
|
17
|
+
word_separators = /[^a-zA-Z0-9']/
|
18
|
+
self.downcase!
|
19
|
+
self.each_char.each_with_index do |c,i|
|
20
|
+
if self[i-1].chr =~ word_separators or i.zero?
|
21
|
+
self[i] = c.upcase if c =~ /[a-z]/
|
22
|
+
end
|
25
23
|
end
|
24
|
+
self
|
25
|
+
end
|
26
26
|
end
|
@@ -4,70 +4,69 @@ require 'absolute_renamer/use_config'
|
|
4
4
|
require 'pp'
|
5
5
|
|
6
6
|
module AbsoluteRenamer
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
# Class in charge of the command line parsing.
|
8
|
+
class Parser
|
9
|
+
class << self
|
10
|
+
include AbsoluteRenamer::UseConfig
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
22
|
-
parser.parse!
|
23
|
-
rescue RuntimeError => ex
|
24
|
-
STDERR.puts(ex)
|
25
|
-
exit 1
|
26
|
-
end
|
27
|
-
conf[:files] = self.get_files(ARGV, 0) || []
|
28
|
-
conf[:options][:maxdepth] ||= 0
|
29
|
-
conf[:options][:interactive] ||= :never
|
30
|
-
conf[:options][:mode] ||= :rename
|
31
|
-
pp conf.get if conf[:debug]
|
32
|
-
end
|
12
|
+
# Calls all registred parsers.
|
13
|
+
# The parsers are written in configuration files.
|
14
|
+
# The core parsers are automaticaly added.
|
15
|
+
def parse_cmd_line
|
16
|
+
ARGV.options do |parser|
|
17
|
+
begin
|
18
|
+
list = AbsoluteRenamer::IParser.children
|
19
|
+
list.each do |class_name|
|
20
|
+
class_name.add_options(parser, conf[:options])
|
33
21
|
end
|
22
|
+
parser.parse!
|
23
|
+
rescue RuntimeError => ex
|
24
|
+
STDERR.puts(ex)
|
25
|
+
exit 1
|
26
|
+
end
|
27
|
+
conf[:files] = self.get_files(ARGV, 0) || []
|
28
|
+
pp conf.get if conf[:debug]
|
29
|
+
end
|
30
|
+
end
|
34
31
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
list.each do |entry|
|
49
|
-
return files unless File.exists?(entry)
|
50
|
-
is_dir = File.directory?(entry)
|
51
|
-
mod_dir = options[:dir]
|
52
|
-
depth_ok = (depth < options[:maxdepth] or options[:maxdepth].zero?)
|
53
|
-
mod_rec = (options[:rec] and depth_ok)
|
32
|
+
# Creates a list of all files and directories to rename.
|
33
|
+
# All options that have not been matched are considered as path.
|
34
|
+
# For directories, if the recursive otpion is set to true (conf[:rec] == true),
|
35
|
+
# files are searched in sub directories.
|
36
|
+
#
|
37
|
+
# list: a list of path to explore
|
38
|
+
# depth: maximum recursion depth
|
39
|
+
#
|
40
|
+
# Returns the files/directories list
|
41
|
+
def get_files(list, depth)
|
42
|
+
files = []
|
43
|
+
options = conf[:options]
|
54
44
|
|
55
|
-
|
56
|
-
|
57
|
-
|
45
|
+
list.each do |entry|
|
46
|
+
if File.exists?(entry)
|
47
|
+
is_dir = File.directory?(entry)
|
48
|
+
mod_dir = options[:dir]
|
49
|
+
depth_ok = (depth < options[:maxdepth] or options[:maxdepth].zero?)
|
50
|
+
mod_rec = (options[:rec] and depth_ok)
|
58
51
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
files
|
63
|
-
end
|
52
|
+
add_dir = (is_dir and mod_dir)
|
53
|
+
add_file = (!is_dir and !mod_dir)
|
54
|
+
add_sub = (is_dir and (mod_rec or (!mod_dir and depth < 1)))
|
64
55
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
files.delete_if { |file| file[0,1] == '.' }
|
69
|
-
files.collect! { |file| path + '/' + file }
|
70
|
-
end
|
56
|
+
files << FileInfo.new(entry) if (add_dir or add_file)
|
57
|
+
files += self.get_files(self.get_subentries(entry), depth + 1) if (add_sub)
|
58
|
+
end
|
71
59
|
end
|
60
|
+
|
61
|
+
files
|
62
|
+
end
|
63
|
+
|
64
|
+
# Returns files and directories contained in +path+.
|
65
|
+
def get_subentries(path)
|
66
|
+
files = Dir.entries(path)
|
67
|
+
files.delete_if { |file| file[0,1] == '.' }
|
68
|
+
files.collect! { |file| File.join(path, file) }
|
69
|
+
end
|
72
70
|
end
|
71
|
+
end
|
73
72
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'absolute_renamer/config'
|
2
2
|
|
3
3
|
module AbsoluteRenamer
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
4
|
+
# Module that provide configuration usage.
|
5
|
+
module UseConfig
|
6
|
+
# Returns the configuration class
|
7
|
+
def conf
|
8
|
+
@conf ||= AbsoluteRenamer::Config
|
10
9
|
end
|
10
|
+
end
|
11
11
|
end
|
@@ -1,22 +1,22 @@
|
|
1
1
|
require 'absolute_renamer/use_config'
|
2
2
|
|
3
3
|
module AbsoluteRenamer
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
# Class allowing childs listing.
|
5
|
+
class WithChildren
|
6
|
+
include AbsoluteRenamer::UseConfig
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
@children = []
|
9
|
+
class << self
|
10
|
+
attr_reader :children
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
12
|
+
# Inheritance callback.
|
13
|
+
# When a class inherit from a WithChildren class, it is added to
|
14
|
+
# the childs list of this class.
|
15
|
+
# This list is available as the +children+ attribute.
|
16
|
+
def inherited(by)
|
17
|
+
@children << by
|
18
|
+
by.instance_variable_set(:@children, [])
|
19
|
+
end
|
21
20
|
end
|
21
|
+
end
|
22
22
|
end
|
data/test/config_test.rb
CHANGED
@@ -2,16 +2,10 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class ConfigTest < Test::Unit::TestCase
|
4
4
|
context "The Config instance" do
|
5
|
-
|
5
|
+
|
6
6
|
should "exist" do
|
7
7
|
assert_not_nil AbsoluteRenamer::Config
|
8
8
|
end
|
9
|
-
|
10
|
-
should "raise ENOENT if config file not found" do
|
11
|
-
assert_raise(Errno::ENOENT) do
|
12
|
-
AbsoluteRenamer::Config.load('a file that must not be found')
|
13
|
-
end
|
14
|
-
end
|
15
9
|
|
16
10
|
should "not raise ENOENT if config file is found" do
|
17
11
|
assert_nothing_raised(Errno::ENOENT) do
|
@@ -38,7 +32,6 @@ class ConfigTest < Test::Unit::TestCase
|
|
38
32
|
AbsoluteRenamer::Config[:test_key] = :test_val
|
39
33
|
assert_equal(:test_val, AbsoluteRenamer::Config[:test_key])
|
40
34
|
end
|
41
|
-
|
42
35
|
end
|
43
36
|
|
44
37
|
end
|
data/test/file_info_test.rb
CHANGED
@@ -2,27 +2,28 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class FileInfoTest < Test::Unit::TestCase
|
4
4
|
context "A FileInfo instance" do
|
5
|
-
context "loaded with
|
5
|
+
context "loaded with /tmp/test-absrenamer.txt" do
|
6
6
|
|
7
7
|
setup do
|
8
|
-
|
9
|
-
|
8
|
+
AbsoluteRenamer::Config.load('conf/absrenamer/absrenamer.conf')
|
9
|
+
`touch /tmp/test-absrenamer.txt`
|
10
|
+
@fileinfo = AbsoluteRenamer::FileInfo.new('/tmp/test-absrenamer.txt')
|
10
11
|
end
|
11
12
|
|
12
|
-
should "have as name :
|
13
|
-
assert_equal('
|
13
|
+
should "have as name : test-absrenamer" do
|
14
|
+
assert_equal('test-absrenamer', @fileinfo.name)
|
14
15
|
end
|
15
16
|
|
16
|
-
should "have as dir_path :
|
17
|
-
assert_equal(
|
17
|
+
should "have as dir_path : /tmp" do
|
18
|
+
assert_equal('/tmp', @fileinfo.dir_path)
|
18
19
|
end
|
19
20
|
|
20
|
-
should "have as path :
|
21
|
-
assert_equal('
|
21
|
+
should "have as path : /tmp/test-absrenamer.txt" do
|
22
|
+
assert_equal('/tmp/test-absrenamer.txt', @fileinfo.path)
|
22
23
|
end
|
23
24
|
|
24
|
-
should "have as real_path :
|
25
|
-
assert_equal(
|
25
|
+
should "have as real_path : /tmp/test-absrenamer.txt" do
|
26
|
+
assert_equal('/tmp/test-absrenamer.txt', @fileinfo.real_path)
|
26
27
|
end
|
27
28
|
|
28
29
|
should "have as ext : txt" do
|
@@ -39,27 +40,27 @@ class FileInfoTest < Test::Unit::TestCase
|
|
39
40
|
|
40
41
|
end
|
41
42
|
|
42
|
-
context "loaded with
|
43
|
+
context "loaded with /usr/bin" do
|
43
44
|
|
44
45
|
setup do
|
45
|
-
|
46
|
-
@fileinfo = AbsoluteRenamer::FileInfo.new('
|
46
|
+
AbsoluteRenamer::Config.load('conf/absrenamer/absrenamer.conf')
|
47
|
+
@fileinfo = AbsoluteRenamer::FileInfo.new('/usr/bin')
|
47
48
|
end
|
48
49
|
|
49
|
-
should "have as name :
|
50
|
-
assert_equal('
|
50
|
+
should "have as name : bin" do
|
51
|
+
assert_equal('bin', @fileinfo.name)
|
51
52
|
end
|
52
53
|
|
53
|
-
should "have as dir_path :
|
54
|
-
assert_equal(
|
54
|
+
should "have as dir_path : /usr" do
|
55
|
+
assert_equal('/usr', @fileinfo.dir_path)
|
55
56
|
end
|
56
57
|
|
57
|
-
should "have as path :
|
58
|
-
assert_equal('
|
58
|
+
should "have as path : usr/bin" do
|
59
|
+
assert_equal('/usr/bin', @fileinfo.path)
|
59
60
|
end
|
60
61
|
|
61
|
-
should "have as real_path :
|
62
|
-
assert_equal(
|
62
|
+
should "have as real_path : /usr/bin" do
|
63
|
+
assert_equal('/usr/bin', @fileinfo.real_path)
|
63
64
|
end
|
64
65
|
|
65
66
|
should "have as ext : nil" do
|