AbsoluteRenamer 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,35 +1,35 @@
1
1
  module AbsoluteRenamer
2
- class InteractivePlugin < AbsoluteRenamer::IPlugin
3
- def before_batch_renaming
4
- if conf[:options][:interactive] == :once
5
- conf[:files].each do |file|
6
- file.display_change
7
- end
8
- print "Do you want to rename this files ? [y/N] "
9
- begin
10
- resp = STDIN.readline.chomp.downcase
11
- rescue Exception => e
12
- puts "\nExiting renamer"
13
- exit(0)
14
- end
15
- return resp == "y"
16
- end
17
- true
2
+ class InteractivePlugin < AbsoluteRenamer::IPlugin
3
+ def before_batch_renaming
4
+ ask_for_confirmation(:once, 'Do you want to rename this files ?') do
5
+ conf[:files].each do |file|
6
+ file.display_change
18
7
  end
8
+ end
9
+ end
10
+
11
+ def before_file_renaming(params)
12
+ ask_for_confirmation(:always, 'Do you want to rename this file ?') do
13
+ params[:file].display_change
14
+ end
15
+ end
19
16
 
20
- def before_file_renaming(params)
21
- if conf[:options][:interactive] == :always
22
- params[:file].display_change
23
- print "Do you want to rename this file ? [y/N] "
24
- begin
25
- resp = STDIN.readline.chomp.downcase
26
- rescue Exception => e
27
- puts "\nExiting renamer"
28
- exit(0)
29
- end
30
- return resp == "y"
31
- end
32
- true
17
+ def ask_for_confirmation(interactivity, message, waited_answer = 'y')
18
+ if conf[:options][:interactive] == interactivity
19
+ yield
20
+
21
+ print "#{message} [y/N] "
22
+
23
+ begin
24
+ resp = STDIN.readline.chomp.downcase
25
+ rescue
26
+ puts "\nExiting renamer"
27
+ exit(0)
33
28
  end
29
+
30
+ return resp == waited_answer
31
+ end
32
+ true
34
33
  end
34
+ end
35
35
  end
@@ -1,9 +1,9 @@
1
1
  module AbsoluteRenamer
2
- class ListingParser < AbsoluteRenamer::IParser
3
- def self.add_options(parser, options)
4
- parser.on('-l', '--list', "Only display how files will be renamed") do
5
- options[:listing] = true
6
- end
7
- end
2
+ class ListingParser < AbsoluteRenamer::IParser
3
+ def self.add_options(parser, options)
4
+ parser.on('-l', '--list', "Only display how files will be renamed") do
5
+ options[:listing] = true
6
+ end
8
7
  end
8
+ end
9
9
  end
@@ -1,14 +1,14 @@
1
1
  module AbsoluteRenamer
2
- class ListingPlugin < AbsoluteRenamer::IPlugin
3
- def before_batch_renaming
4
- listing = conf[:options][:listing] || false
5
- if listing
6
- conf[:files].each do |file|
7
- file.display_change
8
- end
9
- return false
10
- end
11
- true
2
+ class ListingPlugin < AbsoluteRenamer::IPlugin
3
+ def before_batch_renaming
4
+ listing = conf[:options][:listing] || false
5
+ if listing
6
+ conf[:files].each do |file|
7
+ file.display_change
12
8
  end
9
+ return false
10
+ end
11
+ true
13
12
  end
13
+ end
14
14
  end
@@ -3,58 +3,58 @@ require 'absolute_renamer/iplugin'
3
3
  require 'absolute_renamer/use_config'
4
4
 
5
5
  begin
6
- require 'rubygems'
6
+ require 'rubygems'
7
7
  rescue LoadError
8
8
  end
9
9
 
10
10
  module AbsoluteRenamer
11
- # Class in charge of loading external modules.
12
- class External
13
- class << self
14
- include AbsoluteRenamer::UseConfig
15
-
16
- def load_gems
17
- if defined?(Gem)
18
- @gems = {}
19
-
20
- find_gems
21
- find_gems_from_conf
22
- exclude_gems
23
-
24
- @gems.each do |gem_name, gem_infos|
25
- puts "Loading gem #{gem_name} (#{gem_infos[:version]}) : #{gem_infos[:lib]}" if conf[:debug]
26
- gem gem_name, gem_infos[:version]
27
- require gem_infos[:lib]
28
- end
29
- end
30
- end
31
-
32
- def find_gems
33
- installed_gems = Gem.source_index.find_name(/.*AbsoluteRenamer-.*/).map(&:name).uniq || []
34
- installed_gems.each do |gem_name|
35
- @gems[gem_name] = { :lib => gem_name, :version => '>= 0' }
36
- end
37
- end
38
-
39
- def find_gems_from_conf
40
- if conf[:gems]
41
- conf[:gems].each do |gem_name, gem_infos|
42
- @gems[gem_name] = gem_infos ||= {}
43
- @gems[gem_name][:lib] ||= gem_name
44
- @gems[gem_name][:version] ||= ">= 0"
45
- end
46
- end
47
- end
48
-
49
- def exclude_gems
50
- if @conf[:exclude]
51
- @gems.reject! { |gem_name, gem_infos| @conf[:exclude].include? gem_name }
52
- end
53
- end
54
-
55
- def load_core
56
- require 'absolute_renamer/core-packages/core-packages'
57
- end
11
+ # Class in charge of loading external modules.
12
+ class External
13
+ class << self
14
+ include AbsoluteRenamer::UseConfig
15
+
16
+ def load_gems
17
+ if defined?(Gem)
18
+ @gems = {}
19
+
20
+ find_gems
21
+ find_gems_from_conf
22
+ exclude_gems
23
+
24
+ @gems.each do |gem_name, gem_infos|
25
+ puts "Loading gem #{gem_name} (#{gem_infos[:version]}) : #{gem_infos[:lib]}" if conf[:debug]
26
+ gem gem_name, gem_infos[:version]
27
+ require gem_infos[:lib]
28
+ end
58
29
  end
30
+ end
31
+
32
+ def find_gems
33
+ installed_gems = Gem.source_index.find_name(/.*AbsoluteRenamer-.*/).map(&:name).uniq || []
34
+ installed_gems.each do |gem_name|
35
+ @gems[gem_name] = { :lib => gem_name, :version => '>= 0' }
36
+ end
37
+ end
38
+
39
+ def find_gems_from_conf
40
+ if conf[:gems]
41
+ conf[:gems].each do |gem_name, gem_infos|
42
+ @gems[gem_name] = gem_infos ||= {}
43
+ @gems[gem_name][:lib] ||= gem_name
44
+ @gems[gem_name][:version] ||= ">= 0"
45
+ end
46
+ end
47
+ end
48
+
49
+ def exclude_gems
50
+ if @conf[:exclude]
51
+ @gems.reject! { |gem_name, gem_infos| @conf[:exclude].include? gem_name }
52
+ end
53
+ end
54
+
55
+ def load_core
56
+ require 'absolute_renamer/core-packages/core-packages'
57
+ end
59
58
  end
59
+ end
60
60
  end
@@ -4,105 +4,104 @@ require 'absolute_renamer/use_config'
4
4
  require 'absolute_renamer/libs/file'
5
5
 
6
6
  module AbsoluteRenamer
7
- # Class that represents each file to be renamed.
8
- # It contains all informations about a file and, in the end,
9
- # processes to its renaming.
10
- class FileInfo
11
- include AbsoluteRenamer::UseConfig
7
+ # Class that represents each file to be renamed.
8
+ # It contains all informations about a file and, in the end,
9
+ # processes to its renaming.
10
+ class FileInfo
11
+ include AbsoluteRenamer::UseConfig
12
12
 
13
- attr_accessor :name, :new_name,
14
- :path, :real_path,
15
- :ext, :dir, :dir_path,
16
- :level
13
+ attr_accessor :name, :new_name,
14
+ :path, :real_path,
15
+ :ext, :dir, :dir_path,
16
+ :level
17
17
 
18
- # Initializes a FileInfo.
19
- # path: the relative or absolute path of the file.
20
- def initialize(path)
21
- @path = path
22
- @real_path = File.expand_path(@path)
23
- @dir_path = File.dirname(@real_path)
24
- @dir = File.directory?(@real_path)
25
- @name = File.basename(@real_path)
26
- unless @dir
27
- # TODO utiliser une conf :dot
28
- @ext = File.extname(@name)
29
- @name.gsub!(Regexp.new('.' << @ext << '$'), '') unless @ext.empty?
30
- @level = 0
31
- else
32
- @level = @real_path.split('/').size
33
- end
34
- end
18
+ # Initializes a FileInfo.
19
+ # path: the relative or absolute path of the file.
20
+ def initialize(path)
21
+ @path = path
22
+ @real_path = File.expand_path(@path)
23
+ @dir_path = File.dirname(@real_path)
24
+ @dir = File.directory?(@real_path)
25
+ @name = File.basename(@real_path)
26
+ unless @dir
27
+ @ext = File.extname(@name, conf[:options][:dots])
28
+ @name.gsub!(Regexp.new('.' << @ext << '$'), '') unless @ext.empty?
29
+ @level = 0
30
+ else
31
+ @level = @real_path.split('/').size
32
+ end
33
+ end
35
34
 
36
- # Returns a description of a FileInfo
37
- # some_fileinfo.inspect # => "File: hello_world pdf"
38
- def inspect
39
- "File: #{@name} #{@ext}"
40
- end
35
+ # Returns a description of a FileInfo
36
+ # some_fileinfo.inspect # => "File: hello_world pdf"
37
+ def inspect
38
+ "File: #{@name} #{@ext}"
39
+ end
41
40
 
42
- # Displays the action that will be done on the file.
43
- # some_fileinfo.display_change # => "rename a_file.txt --> A_File.TXT"
44
- def display_change
45
- puts "#{color conf[:options][:mode]} #{@real_path.sub(Dir.pwd+'/', '')} #{color '-->'} #{new_path.sub(Dir.pwd+'/', '')}"
46
- end
41
+ # Displays the action that will be done on the file.
42
+ # some_fileinfo.display_change # => "rename a_file.txt --> A_File.TXT"
43
+ def display_change
44
+ puts "#{color conf[:options][:mode]} #{@real_path.sub(Dir.pwd+'/', '')} #{color '-->'} #{new_path.sub(Dir.pwd+'/', '')}"
45
+ end
47
46
 
48
- # Returns a text colorized in red.
49
- # color('hello') #=> "\e[31mhello\e[0m"
50
- def color(text)
51
- "\e[31m#{text}\e[0m"
52
- end
47
+ # Returns a text colorized in red.
48
+ # color('hello') #=> "\e[31mhello\e[0m"
49
+ def color(text)
50
+ "\e[31m#{text}\e[0m"
51
+ end
53
52
 
54
- # Returns the new path of the file.
55
- def new_path
56
- if conf[:options][:dest].nil? or conf[:options][:dest].empty?
57
- File.join(@dir_path, @new_name)
58
- else
59
- File.join(conf[:options][:dest], @new_name)
60
- end
61
- end
53
+ # Returns the new path of the file.
54
+ def new_path
55
+ if conf[:options][:dest].nil? or conf[:options][:dest].empty?
56
+ File.join(@dir_path, @new_name)
57
+ else
58
+ File.join(conf[:options][:dest], @new_name)
59
+ end
60
+ end
62
61
 
63
- # Renames the file.
64
- def rename
65
- display_change
66
- File.rename(@real_path, new_path)
67
- end
62
+ # Renames the file.
63
+ def rename
64
+ display_change
65
+ File.rename(@real_path, new_path)
66
+ end
68
67
 
69
- # Copies the file.
70
- def copy
71
- display_change
72
- if @dir
73
- if @real_path != conf[:options][:dest]
74
- FileUtils.cp_r(@real_path, conf[:options][:dest])
75
- else
76
- puts "#{real_path} ignored"
77
- end
78
- else
79
- File.copy(@real_path, new_path, false)
80
- end
68
+ # Copies the file.
69
+ def copy
70
+ display_change
71
+ if @dir
72
+ if @real_path != conf[:options][:dest]
73
+ FileUtils.cp_r(@real_path, conf[:options][:dest])
74
+ else
75
+ puts "#{real_path} ignored"
81
76
  end
77
+ else
78
+ File.copy(@real_path, new_path, false)
79
+ end
80
+ end
82
81
 
83
- # Moves a file. Moving to the same directories is just like renaming.
84
- def move
85
- display_change
86
- File.move(@real_path, new_path, false)
87
- end
82
+ # Moves a file. Moving to the same directories is just like renaming.
83
+ def move
84
+ display_change
85
+ File.move(@real_path, new_path, false)
86
+ end
88
87
 
89
- # Creates a symbolic link to the file.
90
- def link
91
- display_change
92
- begin
93
- File.symlink(@real_path, new_path)
94
- rescue NotImplemented
95
- # TODO trouver mieux
96
- puts "Error: cannot create symlinks"
97
- end
98
- end
88
+ # Creates a symbolic link to the file.
89
+ def link
90
+ display_change
91
+ begin
92
+ File.symlink(@real_path, new_path)
93
+ rescue NotImplemented
94
+ puts "Error: cannot create symlinks"
95
+ end
96
+ end
99
97
 
100
- def self.compare_level(a, b)
101
- if (a.level == b.level)
102
- return (a.name <= b.name) ? -1 : 1
103
- else
104
- return (a.level < b.level) ? 1 : -1
105
- end
106
- end
98
+ # Overriding the comparison operator to sort paths
99
+ # based on their depth.
100
+ def <=>(file_info)
101
+ if (self.level == file_info.level)
102
+ return (file_info.name <=> self.name)
103
+ end
104
+ file_info.level <=> self.level
107
105
  end
106
+ end
108
107
  end
@@ -1,86 +1,83 @@
1
1
  require 'absolute_renamer/with_children'
2
2
 
3
3
  module AbsoluteRenamer
4
- # Modules parent class.
5
- # Modules must inherit of it.
6
- class IModule < AbsoluteRenamer::WithChildren
7
- def initialize
8
- @filters = []
9
- end
4
+ # Modules parent class.
5
+ # Modules must inherit of it.
6
+ class IModule < AbsoluteRenamer::WithChildren
7
+ def initialize
8
+ @filters = []
9
+ end
10
10
 
11
- # Returns the classname symbol
12
- def self.symbol
13
- name.intern
14
- end
11
+ # Returns the classname symbol
12
+ def self.symbol
13
+ name.intern
14
+ end
15
15
 
16
- # Returns a format pattern generated from pattern_string that
17
- # can be used to match strings like [*test] in the filename format
18
- # pattern('test') #=> '(\[(.)?test\])'
19
- def pattern(pattern_string)
20
- Regexp.new "(\\[(.)?#{pattern_string}\\])"
21
- end
16
+ def self.process(file, name_format, ext_format)
17
+ @mods ||= {}
22
18
 
23
- # Returns a value modified using a modifier defined in the Case module
24
- # modifiy('value', '&') #=> 'VALUE'
25
- # modifiy('value', '*') #=> 'Value'
26
- def modify(val, modifier)
27
- if CaseModule.actions.include?(modifier)
28
- mod = CaseModule.method(CaseModule.actions[modifier])
29
- val = mod.call(val)
30
- end
31
- val
32
- end
19
+ self.children.each do |mod|
20
+ mod_sym = mod.symbol
21
+ @mods[mod_sym] ||= mod.new
22
+ name_format = @mods[mod_sym].process(file, name_format)
23
+ ext_format = @mods[mod_sym].process(file, ext_format, :ext) unless file.dir
24
+ end
33
25
 
34
- # Process a +file+ by searching for a known pattern in its name
35
- # and replacing it by the corresponding value.
36
- # The pattern is a regular expression obtained by concatening
37
- # the +@filters+ variable with "|".
38
- #
39
- # file: a FileInfo instance
40
- # format: the format string used to rename the file
41
- # type: the type of the renaming format (:name or :ext)
42
- def process(file, format, type = :name)
43
- return format if @filters.empty?
26
+ [name_format, ext_format]
27
+ end
44
28
 
45
- str = format
46
- result = []
47
- pattern = Regexp.union @filters
29
+ # Returns a format pattern generated from pattern_string that
30
+ # can be used to match strings like [*test] in the filename format
31
+ # pattern('test') #=> '(\[(.)?test\])'
32
+ def pattern(pattern_string)
33
+ Regexp.new "(\\[(.)?#{pattern_string}\\])"
34
+ end
48
35
 
49
- idx = str.index(pattern)
50
- while idx
51
- matched = pattern.match(str).to_a
52
- part = str.partition(matched[0])
53
- result.push(part[0])
54
- val = self.interpret(file, matched, type)
55
- result.push(val)
56
- str = part[2]
57
- idx = str.index(pattern)
58
- end
59
- result.push(str) unless str.empty?
60
- format.replace(result.join)
61
- format
62
- end
36
+ # Returns a value modified using a modifier defined in the Case module
37
+ # modifiy('value', '&') #=> 'VALUE'
38
+ # modifiy('value', '*') #=> 'Value'
39
+ def modify(val, modifier)
40
+ modification = CaseModule.actions[modifier]
41
+ val = CaseModule.send(modification, val) unless modification.nil?
42
+ val
43
+ end
44
+
45
+ # Process a +file+ by searching for a known pattern in its name
46
+ # and replacing it by the corresponding value.
47
+ # The pattern is a regular expression obtained by concatening
48
+ # the +@filters+ variable with "|".
49
+ #
50
+ # file: a FileInfo instance
51
+ # format: the format string used to rename the file
52
+ # type: the type of the renaming format (:name or :ext)
53
+ def process(file, format, type = :name)
54
+ return format if @filters.empty?
63
55
 
64
- # Interprets a matched pattern.
65
- # Searchs for the corresponding callback
66
- # in the current module and call it.
67
- #
68
- # file: a FileInfo instance
69
- # infos: the matched values depending of the pattern
70
- # type: the type of the renaming format (:name or :ext)
71
- def interpret(file, infos, type)
72
- modifier = infos[2]
73
- action = infos[3]
56
+ result = []
57
+ pattern = Regexp.union @filters
58
+ idx = format.index(pattern)
74
59
 
75
- return conf[:options][:default_string] unless self.respond_to?(action.intern)
60
+ while idx
61
+ matched = pattern.match(format).to_a
62
+ part = format.partition(matched[0])
63
+ result.push(part[0])
64
+ result.push self.interpret(file, matched, type)
65
+ format = part[2]
66
+ idx = format.index(pattern)
67
+ end
68
+ result.push(format)
69
+ format.replace(result.join)
70
+ end
76
71
 
77
- ap = self.method(action.intern)
78
- val = ap.call(file, infos, type)
79
- unless modifier.empty?
80
- mp = CaseModule.method(CaseModule.actions[modifier])
81
- val = mp.call(val)
82
- end
83
- val.empty? ? conf[:options][:default_string] : val
84
- end
72
+ # Interprets a matched pattern.
73
+ # Searchs for the corresponding callback
74
+ # in the current module and call it.
75
+ #
76
+ # file: a FileInfo instance
77
+ # infos: the matched values depending of the pattern
78
+ # type: the type of the renaming format (:name or :ext)
79
+ def interpret(file, infos, type)
80
+ # This method has to be overriden in every module
85
81
  end
82
+ end
86
83
  end