Class: AbsoluteRenamer::Parser

Inherits:
Object
  • Object
show all
Extended by:
UseConfig
Defined in:
lib/absolute_renamer/parser.rb

Overview

Class in charge of the command line parsing.

Class Method Summary collapse

Methods included from UseConfig

conf

Class Method Details

.get_files(list, depth) ⇒ Object

Creates a list of all files and directories to rename. All options that have not been matched are considered as path. For directories, if the recursive otpion is set to true (conf == true), files are searched in sub directories.

list: a list of path to explore depth: maximum recursion depth

Returns the files/directories list



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/absolute_renamer/parser.rb', line 62

def get_files(list, depth)
    files = []
    options = conf[:options]

    list.each do |entry|
        return files unless File.exists?(entry)
        is_dir =   File.directory?(entry)
        mod_dir =  options[:dir]
        depth_ok = (depth < options[:maxdepth] or options[:maxdepth].zero?)
        mod_rec =  (options[:rec] and depth_ok)

        add_dir =  (is_dir and mod_dir)
        add_file = (!is_dir and !mod_dir)
        add_sub =  (is_dir and (mod_rec or (!mod_dir and depth < 1)))

        files << FileInfo.new(entry) if (add_dir or add_file)
        files += self.get_files(self.get_subentries(entry), depth + 1) if (add_sub)
    end
    files
end

.get_subentries(path) ⇒ Object

Returns files and directories contained in path.



84
85
86
87
88
# File 'lib/absolute_renamer/parser.rb', line 84

def get_subentries(path)
    files = Dir.entries(path)
    files.delete_if { |file| file[0,1] == '.' }
    files.collect! { |file| path + '/' + file }
end

.parse_cmd_lineObject

Calls all registred parsers. The parsers are written in configuration files. The core parsers are automaticaly added.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/absolute_renamer/parser.rb', line 15

def parse_cmd_line
    parsers_dir = conf[:path][:parsers]
    parsers = conf[:parsers]

    core_parsers = ['general']

    parsers += core_parsers.map! { |core_parser| File.join('core', core_parser) }

    parsers.each do |parser|
        parser_file = File.join(parsers_dir, parser, 'parser.rb')
        begin
            if require parser_file
                puts "Loaded: #{parser_file}" if conf[:debug]
            end
        rescue LoadError => e
            STDERR.puts(e)
        end
    end

    ARGV.options do |parser|
        begin
            list = AbsoluteRenamer::IParser.children
            list.each do |class_name|
                class_name.add_options(parser, conf[:options])
            end
            parser.parse!
        rescue RuntimeError => ex
            STDERR.puts(ex)
            exit 1
        end
        conf[:files] = self.get_files(ARGV, 0) || []
        conf[:options][:maxdepth] ||= 0
        conf[:options][:interactive] ||= :never
        conf[:options][:mode] ||= :rename
        pp conf.get if conf[:debug]
    end
end