Class: Fluent::Config::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/config/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basepath, iterator, fname, i = 0) ⇒ Parser

Returns a new instance of Parser.



30
31
32
33
34
35
# File 'lib/fluent/config/parser.rb', line 30

def initialize(basepath, iterator, fname, i = 0)
  @basepath = basepath
  @iterator = iterator
  @i = i
  @fname = fname
end

Class Method Details

.parse(io, fname, basepath = Dir.pwd) ⇒ Object



25
26
27
28
# File 'lib/fluent/config/parser.rb', line 25

def self.parse(io, fname, basepath = Dir.pwd)
  attrs, elems = Parser.new(basepath, io.each_line, fname).parse!(true)
  Element.new('ROOT', '', attrs, elems)
end

Instance Method Details

#parse!(allow_include, elem_name = nil, attrs = {}, elems = []) ⇒ Object



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
# File 'lib/fluent/config/parser.rb', line 37

def parse!(allow_include, elem_name = nil, attrs = {}, elems = [])
  while line = @iterator.next
    line.force_encoding('UTF-8')
    @i += 1
    line.lstrip!
    line.gsub!(/\s*(?:\#.*)?$/,'')
    if line.empty?
      next
    elsif m = /^\<include\s*(.*)\s*\/\>$/.match(line)
      value = m[1].strip
      process_include(attrs, elems, value, allow_include)
    elsif m = /^\<([a-zA-Z0-9_]+)\s*(.+?)?\>$/.match(line)
      e_name = m[1]
      e_arg = m[2] || ""
      e_attrs, e_elems = parse!(false, e_name)
      elems << Element.new(e_name, e_arg, e_attrs, e_elems)
    elsif line == "</#{elem_name}>"
      break
    elsif m = /^([a-zA-Z0-9_]+)\s*(.*)$/.match(line)
      key = m[1]
      value = m[2]
      if allow_include && key == 'include'
        process_include(attrs, elems, value)
      else
        attrs[key] = value
      end
      next
    else
      raise ConfigParseError, "parse error at #{@fname} line #{@i}"
    end
  end

  return attrs, elems
rescue StopIteration
  return attrs, elems
end

#process_include(attrs, elems, uri, allow_include = true) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fluent/config/parser.rb', line 74

def process_include(attrs, elems, uri, allow_include = true)
  u = URI.parse(uri)
  if u.scheme == 'file' || u.path == uri  # file path
    path = u.path
    if path[0] != ?/
      pattern = File.expand_path("#{@basepath}/#{path}")
    else
      pattern = path
    end

    Dir.glob(pattern).sort.each { |entry|
      basepath = File.dirname(entry)
      fname = File.basename(entry)
      File.open(entry) { |f|
        Parser.new(basepath, f.each_line, fname).parse!(allow_include, nil, attrs, elems)
      }
    }

  else
    basepath = '/'
    fname = path
    parser_proc = ->(f) {
      Parser.new(basepath, f.each_line, fname).parse!(allow_include, nil, attrs, elems)
    }

    case u.scheme
    when 'http', 'https', 'ftp'
      # URI#open can be able to handle URIs for http, https and ftp.
      require 'open-uri'
      u.open(&parser_proc)
    else
      # TODO: This case should be handled in the previous if condition. Glob is not applied to some Windows path formats.
      # 'c:/path/to/file' will be passed as URI, 'uri' and 'u.path' will be:
      #   - uri is 'c:/path/to/file'
      #   - u.path is '/path/to/file' and u.scheme is 'c'
      # Therefore, the condition of the if statement above is not met and it is handled here.
      File.open(uri, &parser_proc)
    end
  end

rescue SystemCallError => e
  raise ConfigParseError, "include error at #{@fname} line #{@i}: #{e.to_s}"
end