Class: Fluent::Auditify::ParsletUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/auditify/parsletutil.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ParsletUtil



6
7
8
# File 'lib/fluent/auditify/parsletutil.rb', line 6

def initialize(options={})
  reset_style
end

Instance Method Details

#collect_file_handlers(object) ⇒ Object



30
31
32
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
# File 'lib/fluent/auditify/parsletutil.rb', line 30

def collect_file_handlers(object)
  handlers = {}
  object.each do |directive|
    if directive[:empty_line]
      key = handler_key(directive, object)
      unless key and handlers.key?(key)
        if key
          handlers[key] = File.open(key, 'w+')
        end
      end
    elsif directive[:source] or directive[:match] or
         directive[:system] or directive[:filter]
      key = handler_key(directive, object)
      unless key and handlers.key?(key)
        if key
          handlers[key] = File.open(key, 'w+')
        end
      end
      directive[:body].each do |body|
        key = handler_key(body, directive)
        unless key and handlers.key?(key)
          if key
            handlers[key] = File.open(key, 'w+')
          end
        end
      end
    end
  end
  handlers
end

#export(object, options = {}) ⇒ Object



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fluent/auditify/parsletutil.rb', line 61

def export(object, options={})
  # setup rewrite file handles
  @handlers = collect_file_handlers(object)
  @include_flushed = {}
  object.each do |directive|
    key = handler_key(directive, object)
    if directive[:system]
      export_line(key, directive[:system].to_s)
      export_body(directive)
      export_line(key, '</system>')
    elsif directive[:source]
      export_line(key, directive[:source].to_s)
      export_body(directive)
      export_line(key, '</source>')
      if directive[:__PATTERN__] and directive[:__PARENT__]
        key = File.join(directive[:__BASE__], directive[:__PARENT__])
        unless @include_flushed[key]
          export_line(key, "@include #{directive[:__PATTERN__]}")
          @include_flushed[key] = true
        end
      end
    elsif directive[:filter]
      export_line(key, directive[:filter].to_s)
      export_body(directive)
      export_line(key, '</filter>')
      if directive[:__PATTERN__] and directive[:__PARENT__]
        key = File.join(directive[:__BASE__], directive[:__PARENT__])
        unless @include_flushed[key]
          export_line(key, "@include #{directive[:__PATTERN__]}")
          @include_flushed[key] = true
        end
      end
    elsif directive[:match]
      if directive[:pattern]
        export_line(key, "#{directive[:match].to_s} #{directive[:pattern]}>")
      else
        export_line(key, "#{directive[:match].to_s}>")
      end
      export_body(directive)
      export_line(key, '</match>')
    elsif directive[:empty_line]
      export_line(key, '')
    end
  end
  @handlers.each do |path, io|
    io.flush
    io.fsync
    io.close
  end
  @handlers = []
end

#export_at_include(object, parent) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/fluent/auditify/parsletutil.rb', line 130

def export_at_include(object, parent)
  pattern = object[:__PATTERN__]
  if pattern
    unless @include_flushed[pattern]
      key = handler_key(parent, parent)
      export_line(key, "@include #{pattern}")
      @include_flushed[pattern] = true
    end
  end
end

#export_body(directive) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/fluent/auditify/parsletutil.rb', line 148

def export_body(directive)
  @indent_level += 1
  directive[:body].each do |child|
    if child[:section]
      export_section(child, directive)
    elsif child[:empty_line]
      key = handler_key(child, directive)
      export_line(key, "")
    elsif child[:value]
      key = handler_key(child, directive)
      export_line(key, "#{child[:name].to_s} #{child[:value].to_s}")
      export_at_include(child, directive)
    elsif child[:name]
      key = handler_key(child, directive)
      export_line(key, child[:name].to_s)
      export_at_include(child, directive)
    end
  end
  @indent_level -= 1
end

#export_line(key, message) ⇒ Object



141
142
143
144
145
146
# File 'lib/fluent/auditify/parsletutil.rb', line 141

def export_line(key, message)
  io = @handlers[key]
  if io
    io.puts("#{' ' * @align * @indent_level}#{message}")
  end
end

#export_section(section, directive) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/fluent/auditify/parsletutil.rb', line 113

def export_section(section, directive)
  key = handler_key(section, directive)
  export_line(key, "<#{section[:section][:name].to_s}>")
  @indent_level += 1
  section[:body].each do |kv|
    key = handler_key(kv, directive)
    if kv[:value]
      export_line(key, "#{kv[:name].to_s} #{kv[:value].to_s}")
    else
      export_line(key, "#{kv[:name].to_s}")
    end
  end
  @indent_level -= 1
  export_line(key, "</#{section[:name].to_s}>")
  export_at_include(section, directive)
end

#handler_key(object, parent = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fluent/auditify/parsletutil.rb', line 16

def handler_key(object, parent = nil)
  if object[:__BASE__] and object[:__PATH__]
    # directive
    File.join(object[:__BASE__], object[:__PATH__])
  elsif parent and parent[:__BASE__] and object[:__PATH__]
    # section, body
    File.join(parent[:__BASE__], object[:__PATH__])
  elsif parent and parent[:__BASE__] and parent[:__PATH__]
    File.join(parent[:__BASE__], parent[:__PATH__])
  else
    nil
  end
end

#reset_styleObject



10
11
12
13
14
# File 'lib/fluent/auditify/parsletutil.rb', line 10

def reset_style
  @indent_level = 0
  @align = 2
  @content = StringIO.new
end

#to_s(object, options = {}) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/fluent/auditify/parsletutil.rb', line 169

def to_s(object, options={})
  object.each do |directive|
    if directive[:source]
      @content.puts "#{' ' * @align * @indent_level}#{directive[:source].to_s}"
      stringify_body(directive)
      @content.puts "</source>"
    elsif directive[:match]
      @content.puts "#{' ' * @align * @indent_level}#{directive[:match].to_s}>"
      stringify_body(directive)
      @content.puts "</match>"
    elsif directive[:system]
      @content.puts "#{' ' * @align * @indent_level}#{directive[:system].to_s}"
      stringify_body(directive)
      @content.puts "</system>"
    elsif directive[:empty_line]
      @content.puts
    else
    end
  rescue => e
    p e
  end
  @content.string
end