Class: TreeRb::DirTreeWalker

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_rb/input_plugins/file_system/directory_walker.rb

Overview

Visit a file system directory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dirname) ⇒ DirTreeWalker #initialize(dirname, options) ⇒ DirTreeWalker

Build a tree walker. Visit a director starting from dirname if dirname is missing, must be supplied when invoking run

Examples:

Print the contents of tmp directory

DirTreeWalker.new("/tmp") do
  on_visit_leaf_node { |pathname| puts pathname }
end.run

Overloads:

  • #initialize(dirname) ⇒ DirTreeWalker

    Parameters:

    • dirname (String)

      the root of the tree (top level directory)

  • #initialize(dirname, options) ⇒ DirTreeWalker

    Parameters:

    • options (Hash)

    Options Hash (options):

    • :ignore (String, Regex, Array<String,Regexp>)

      list of ignore pattern

    • :ignore_dir (String)
    • :ignore_file (String)
    • :match (String)

Yields:

  • (a, b, c)

    TreeNodeVisitor

Yield Parameters:

  • argument (optional, types, ...)

    name description

Yield Returns:

  • (optional, types, ...)

    description



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 33

def initialize(dirname = nil, options = nil)
  #
  # arg detection
  #
  if dirname and dirname.respond_to?(:key?)
    options = dirname
    dirname = nil
  end

  if dirname
    @dirname = dirname
    unless File.directory?(dirname)
      raise ArgumentError.new "#{dirname} is not a directory!"
    end
  end

  @visitor              = nil

  #
  # pattern
  #
  @ignore_dir_patterns  = []
  @ignore_file_patterns = []
  @match_file_patterns  = []

  if options and options[:ignore]
    unless options[:ignore].respond_to?(:at)
      options[:ignore] = [options[:ignore]]
    end
    options[:ignore].each { |p| ignore(p) }
  end

  if options and options[:ignore_dir]
    unless options[:ignore_dir].respond_to?(:at)
      options[:ignore_dir] = [options[:ignore_dir]]
    end
    options[:ignore_dir].each { |p| ignore_dir(p) }
  end

  if options and options[:ignore_file]
    unless options[:ignore_file].respond_to?(:at)
      options[:ignore_file] = [options[:ignore_file]]
    end
    options[:ignore_file].each { |p| ignore_file(p) }
  end

  if options and options[:match]
    unless options[:match].respond_to?(:at)
      options[:match] = [options[:match]]
    end
    options[:match].each { |p| match(p) }
  end

  #
  # options
  #
  @visit_file = true

  @max_level = nil
  if options and options[:max_level]
    @max_level = options[:max_level]
  end
end

Instance Attribute Details

#visit_fileObject

it is true to visit file



154
155
156
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 154

def visit_file
  @visit_file
end

Instance Method Details

#ignore(pattern) ⇒ Object

Ignore a node (leaf/Tree) matching pattern

Parameters:

  • pattern (RegEx)


104
105
106
107
108
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 104

def ignore(pattern)
  @ignore_dir_patterns << pattern
  @ignore_file_patterns << pattern
  self
end

#ignore_dir(pattern) ⇒ Object

Ignore a directory (Tree) matching pattern

Parameters:

  • pattern (RegEx)


114
115
116
117
118
119
120
121
122
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 114

def ignore_dir(pattern)
  # it transforms all in regexp
  if pattern.kind_of? Regexp
    @ignore_dir_patterns << pattern
  else
    @ignore_dir_patterns << /#{pattern}/
  end
  self
end

#ignore_dir?(dirname) ⇒ boolean

Test directory ignore pattern

Parameters:

  • dirname (String)

    directory name

Returns:

  • (boolean)

    if dirname match almost one pattern



164
165
166
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 164

def ignore_dir?(dirname)
  _include?(@ignore_dir_patterns, File.basename(dirname)) != nil
end

#ignore_file(pattern) ⇒ Object

Ignore a file (Leaf) matching pattern

Parameters:

  • pattern (RegEx)


128
129
130
131
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 128

def ignore_file(pattern)
  @ignore_file_patterns << pattern
  self
end

#ignore_file?(filename) ⇒ boolean

Test file ignore pattern

Parameters:

  • filename (String)

Returns:

  • (boolean)

    if filename match almost one pattern



174
175
176
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 174

def ignore_file?(filename)
  _include?(@ignore_file_patterns, File.basename(filename)) != nil
end

#match(pattern) ⇒ Object

Just the opposite of ignore directory/file matching pattern will be visited

Parameters:

  • pattern (RegEx)


139
140
141
142
143
144
145
146
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 139

def match(pattern)
  if pattern.kind_of? Regexp
    @match_file_patterns << pattern
  else
    @match_file_patterns << /#{pattern}/
  end
  self
end

#match?(filename) ⇒ boolean

Test match pattern

Parameters:

  • filename (String)

Returns:

  • (boolean)

    if filename match almost one pattern



184
185
186
187
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 184

def match?(filename)
  return true if @match_file_patterns.empty?
  _include?(@match_file_patterns, File.basename(filename)) != nil
end

#runTreeNodeVisitor #run(dirname) { ... } ⇒ TreeNodeVisitor #run(tree_node_visitor) { ... } ⇒ TreeNodeVisitor #run(dirname, tree_node_visitor) { ... } ⇒ TreeNodeVisitor

Run the visitor through the directory tree

Examples:

Print the contents of tmp directory

w = DirTreeWalker.new
w.run("/tmp") do
  on_visit_leaf_node { |pathname| puts pathname }
end.run

Overloads:

Returns:



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 215

def run(dirname = nil, tree_node_visitor = nil, &block)

  #
  # args detection
  #
  if dirname and dirname.respond_to?(:enter_node)
    tree_node_visitor = dirname
    dirname           = nil
  end

  #
  # check dirname
  #
  if @dirname.nil? and dirname.nil?
    raise 'missing starting directory'
  end
  @dirname = dirname if dirname

  #
  # check visitor
  #
  if tree_node_visitor and block
    raise 'cannot use block and parameter together'
  end

  if tree_node_visitor
    @visitor = tree_node_visitor
  end

  if block
    @visitor = TreeNodeVisitor.new(&block)
  end

  unless @visitor
    raise 'missing visitor'
  end

  #
  # finally starts to process
  #
  process_directory(File.expand_path(@dirname))
  @visitor
end