nagios_config 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -47,8 +47,8 @@ module NagiosConfig
47
47
 
48
48
  def format_Define(define)
49
49
  buffer << "define "
50
- name_width = define.variables.map(&:name).map(&:value).map(&:length).max || 0
51
- value_width = define.variables.map(&:val).map(&:value).map(&:length).max || 0
50
+ name_width = define.variables.map {|e| e.name}.map {|e| e.value}.map {|e| e.length}.max || 0
51
+ value_width = define.variables.map {|e| e.val}.map {|e| e.value}.map {|e| e.length}.max || 0
52
52
  variable_width = define_indent + define_name_width + name_width + value_width
53
53
 
54
54
  self.define_name_width += name_width
@@ -0,0 +1,72 @@
1
+ module NagiosConfig
2
+ class Main
3
+ attr_accessor :variables, :objects
4
+
5
+ def initialize(variables={}, objects=[])
6
+ self.variables = Hash[variables.map {|k,v| [k.to_sym,v]}]
7
+ self.objects = objects
8
+ end
9
+
10
+ def self.from_node(node)
11
+ instance = new
12
+ object_store = instance.objects
13
+ node.nodes.each do |node|
14
+ if node.is_a?(NagiosConfig::Variable)
15
+ name = node.name.value
16
+ value = node.val.value
17
+ case instance[name]
18
+ when nil
19
+ instance[name] = value
20
+ when Array
21
+ instance[name].push(value)
22
+ else
23
+ instance[name] = [instance[name], value]
24
+ end
25
+ elsif node.is_a?(NagiosConfig::Define)
26
+ NagiosConfig::Object.from_node(node, object_store)
27
+ end
28
+ end
29
+ instance
30
+ end
31
+
32
+ def self.parse(io, include_comments=false)
33
+ from_node(NagiosConfig::Parser.new(!include_comments).parse(io))
34
+ end
35
+
36
+ def [](name)
37
+ variables[name.to_sym]
38
+ end
39
+
40
+ def []=(name, value)
41
+ variables[name.to_sym] = value
42
+ end
43
+
44
+ def objects(of_type=nil)
45
+ return @objects unless of_type
46
+ of_type = of_type.to_sym
47
+ objects.select {|obj| obj.type == of_type}
48
+ end
49
+
50
+ def ==(other)
51
+ other.is_a?(self.class) && other.variables == variables &&
52
+ other.objects == objects
53
+ end
54
+
55
+ def inspect
56
+ "#<#{self.class.name}:#{object_id} @variables=#{variables.inspect}, " <<
57
+ "@objects=#{objects.class.name}:#{objects.object_id}" <<
58
+ "(#{objects.length} items)>"
59
+ end
60
+
61
+ def method_missing(name, *args)
62
+ if name.to_s !~ /=$/ && args.empty?
63
+ self[name.to_sym]
64
+ elsif name.to_s =~ /=$/ && args.length == 1
65
+ self[name.to_s.chomp("=").to_sym] = args.first
66
+ else
67
+ super
68
+ end
69
+ end
70
+
71
+ end
72
+ end
@@ -5,46 +5,50 @@ require 'events'
5
5
 
6
6
  module NagiosConfig
7
7
  class Parser
8
- attr_accessor :scanner, :state, :in_define, :value_buffer
8
+ attr_accessor :scanner, :state
9
9
  include Events::Emitter
10
10
 
11
- def initialize
11
+ NEWLINE = "\n".freeze
12
+ EQUALS = "=".freeze
13
+ OPEN_BRACE = "{".freeze
14
+
15
+ def initialize(ignore_comments=false)
12
16
  @state = :body
13
- self.scanner = StringScanner.new("")
14
- self.value_buffer = ""
17
+ @scanner = StringScanner.new("")
18
+ @value_buffer = ""
19
+ @ignore_comments = ignore_comments
15
20
  end
16
21
 
17
22
  def parse(io)
18
23
  root = NagiosConfig::Config.new
19
- current_define = nil
24
+ current = root
20
25
  current_variable = nil
21
26
  on(:comment) do |comment|
22
- (current_define || root).add_node(NagiosConfig::Comment.new(comment))
23
- end
27
+ current.add_node(NagiosConfig::Comment.new(comment))
28
+ end unless @ignore_comments
24
29
  on(:whitespace) do |whitespace|
25
- (current_define || root).add_node(NagiosConfig::Whitespace.new(whitespace))
26
- end
30
+ current.add_node(NagiosConfig::Whitespace.new(whitespace))
31
+ end unless @ignore_comments
27
32
  on(:trailing_comment) do |comment|
28
- (current_define || root).nodes.last.add_node(NagiosConfig::TrailingComment.new(comment))
29
- end
33
+ current.nodes.last.add_node(NagiosConfig::TrailingComment.new(comment))
34
+ end unless @ignore_comments
30
35
  on(:name) do |name|
31
36
  current_variable = NagiosConfig::Variable.new
32
37
  current_variable.add_node(NagiosConfig::Name.new(name))
33
- (current_define || root).add_node(current_variable)
38
+ current.add_node(current_variable)
34
39
  end
35
40
  on(:value) do |value|
36
41
  current_variable.add_node(NagiosConfig::Value.new(value))
37
- current_variable = nil
38
42
  end
39
43
  on(:begin_define) do
40
- current_define = NagiosConfig::Define.new
41
- root.add_node(current_define)
44
+ current = NagiosConfig::Define.new
45
+ root.add_node(current)
42
46
  end
43
47
  on(:type) do |type|
44
- current_define.add_node(NagiosConfig::Type.new(type))
48
+ current.add_node(NagiosConfig::Type.new(type))
45
49
  end
46
50
  on(:finish_define) do
47
- current_define = nil
51
+ current = root
48
52
  end
49
53
  stream_parse(io)
50
54
  root
@@ -58,10 +62,8 @@ module NagiosConfig
58
62
  end
59
63
 
60
64
  def <<(string)
61
- scanner.string.replace(scanner.rest)
62
- scanner.reset
63
- scanner << string
64
- self.state = send(state)
65
+ @scanner << string
66
+ @state = send(@state)
65
67
  end
66
68
 
67
69
  private
@@ -71,59 +73,61 @@ module NagiosConfig
71
73
  alias start body
72
74
 
73
75
  def empty_line
74
- whitespace = scanner.scan(/[ \t]*\n/)
76
+ whitespace = @scanner.scan(/[ \t]*\n/)
75
77
  if whitespace
76
- emit(:whitespace, whitespace)
77
- in_define ? definition_body : body
78
- elsif in_define && whitespace = scanner.scan(/[ \t]*(?=;)/) && trailing_comment
79
- emit(:whitespace, whitespace)
78
+ emit(:whitespace, whitespace) unless @ignore_comments
79
+ @in_define ? definition_body : body
80
+ elsif @in_define && whitespace = @scanner.scan(/[ \t]*(?=;)/) && trailing_comment
81
+ emit(:whitespace, whitespace) unless @ignore_comments
80
82
  definition_body
81
83
  end
82
84
  end
83
85
 
84
86
  def leading_whitespace
85
- if scanner.scan(/[ \t]+[^\s]/)
87
+ if @scanner.skip(/[ \t]+[^\s]/)
86
88
  raise ParseError.new("leading whitespace not allowed")
87
89
  end
88
90
  end
89
91
 
90
92
  def comment
91
- comment = scanner.scan(/[ \t]*#.*(\n)/)
93
+ comment = @scanner.scan(/[ \t]*#[^\n]*\n/)
92
94
  if comment
93
- comment.lstrip!
94
- comment.slice!(0)
95
- comment.chomp!("\n")
96
- emit(:comment, comment)
97
- in_define ? definition_body : body
98
- elsif scanner.check(/#/)
95
+ if !@ignore_comments
96
+ comment.lstrip!
97
+ comment.slice!(0)
98
+ comment.chomp!(NEWLINE)
99
+ emit(:comment, comment)
100
+ end
101
+ @in_define ? definition_body : body
102
+ elsif @scanner.check(/#/)
99
103
  :comment
100
104
  end
101
105
  end
102
106
 
103
107
  def name
104
- name = scanner.scan(/[^\s=]+=/)
108
+ name = @scanner.scan(/[^\s=]+=/)
105
109
  if name
106
- name.chomp!("=")
110
+ name.chomp!(EQUALS)
107
111
  emit(:name, name)
108
112
  value
109
- elsif scanner.scan(/.+\n/)
113
+ elsif @scanner.skip(/[^\n]+\n/)
110
114
  raise ParseError.new("expected variable definition")
111
- elsif scanner.check(/[^\s]+/) && !scanner.check(/d(e(f(i(n(e?))?)?)?)?\Z/)
115
+ elsif @scanner.check(/[^\s]+/) && !@scanner.check(/d(e(f(i(n(e?))?)?)?)?\Z/)
112
116
  :name
113
117
  end
114
118
  end
115
119
 
116
120
  def value
117
- value = scanner.scan(/.*\n/)
121
+ value = @scanner.scan(/[^\n]*\n/)
118
122
  if value
119
- value = value_buffer + value
120
- value.chomp!("\n")
123
+ value = @value_buffer + value
124
+ value.chomp!(NEWLINE)
121
125
  raise ParseError.new("value expected") if value.empty?
122
126
  emit(:value, value)
123
- self.value_buffer = ""
127
+ @value_buffer = ""
124
128
  body
125
- elsif value = scanner.scan(/.+/)
126
- value_buffer << value
129
+ elsif value = @scanner.scan(/.+/)
130
+ @value_buffer << value
127
131
  :value
128
132
  else
129
133
  :value
@@ -131,22 +135,22 @@ module NagiosConfig
131
135
  end
132
136
 
133
137
  def definition
134
- if scanner.skip(/define[ \t]/)
138
+ if @scanner.skip(/define[ \t]/)
135
139
  emit(:begin_define)
136
- self.in_define = true
140
+ @in_define = true
137
141
  type
138
142
  end
139
143
  end
140
144
 
141
145
  def type
142
- type = scanner.scan(/[^;{]+[ \t]*\{[ \t]*\n/)
146
+ type = @scanner.scan(/[^;{]+[ \t]*\{[ \t]*\n/)
143
147
  if type
144
148
  type.strip!
145
- type.chomp!("{")
149
+ type.chomp!(OPEN_BRACE)
146
150
  type.strip!
147
151
  emit(:type, type)
148
152
  definition_body
149
- elsif scanner.check(/([^;{]+[ \t]*(\{[ \t]*)?)?\Z/)
153
+ elsif @scanner.check(/([^;{]+[ \t]*(\{[ \t]*)?)?\Z/)
150
154
  :type
151
155
  else
152
156
  raise ParseError.new("type expected")
@@ -158,29 +162,29 @@ module NagiosConfig
158
162
  end
159
163
 
160
164
  def definition_name
161
- name = scanner.scan(/[ \t]*[^\s;#]+[ \t]+/)
165
+ name = @scanner.scan(/[ \t]*[^\s;#]+[ \t]+/)
162
166
  if name
163
167
  name.strip!
164
168
  emit(:name, name)
165
169
  definition_value
166
- elsif scanner.check(/[ \t]*[^\s#;}]+\Z/)
170
+ elsif @scanner.check(/[ \t]*[^\s#;}]+\Z/)
167
171
  :definition_name
168
- elsif scanner.scan(/[ \t]*[^\s#;]+\n/)
172
+ elsif @scanner.skip(/[ \t]*[^\s#;]+\n/)
169
173
  raise ParseError.new("value expected")
170
174
  end
171
175
  end
172
176
 
173
177
  def definition_value
174
- value = scanner.scan(/[^\n;#]*(?=(\n|;))/)
178
+ value = @scanner.scan(/[^\n;#]*(?=(\n|;))/)
175
179
  if value
176
- value = value_buffer + value
180
+ value = @value_buffer + value
177
181
  value.strip!
178
182
  raise ParseError.new("value expected") if value.empty?
179
183
  emit(:value, value)
180
- self.value_buffer = ""
184
+ @value_buffer = ""
181
185
  after_value
182
- elsif value = scanner.scan(/[^\n;#]+/)
183
- value_buffer << value
186
+ elsif value = @scanner.scan(/[^\n;#]+/)
187
+ @value_buffer << value
184
188
  :definition_value
185
189
  else
186
190
  :definition_value
@@ -188,7 +192,7 @@ module NagiosConfig
188
192
  end
189
193
 
190
194
  def after_value
191
- if scanner.skip(/[ \t]*\n/) || trailing_comment
195
+ if @scanner.skip(/[ \t]*\n/) || trailing_comment
192
196
  definition_body
193
197
  else
194
198
  :after_value
@@ -196,18 +200,18 @@ module NagiosConfig
196
200
  end
197
201
 
198
202
  def finish_definition
199
- if scanner.scan(/[ \t]*\}[ \t]*\n/)
203
+ if @scanner.skip(/[ \t]*\}[ \t]*\n/)
200
204
  emit(:finish_define)
201
- self.in_define = false
205
+ @in_define = false
202
206
  body
203
207
  end
204
208
  end
205
209
 
206
210
  def trailing_comment
207
- trailing_comment = scanner.scan(/[ \t]*;.*\n/)
208
- if trailing_comment
211
+ trailing_comment = @scanner.scan(/[ \t]*;[^\n]*\n/)
212
+ if trailing_comment && !@ignore_comments
209
213
  trailing_comment.strip!
210
- trailing_comment.chomp!("\n")
214
+ trailing_comment.chomp!(NEWLINE)
211
215
  trailing_comment.slice!(0)
212
216
  emit(:trailing_comment, trailing_comment)
213
217
  end
data/lib/nagios_config.rb CHANGED
@@ -4,4 +4,5 @@ require File.dirname(__FILE__) + "/nagios_config/ast"
4
4
  require File.dirname(__FILE__) + "/nagios_config/parser"
5
5
  require File.dirname(__FILE__) + "/nagios_config/formater"
6
6
  require File.dirname(__FILE__) + "/nagios_config/builder"
7
- require File.dirname(__FILE__) + "/nagios_config/object"
7
+ require File.dirname(__FILE__) + "/nagios_config/object"
8
+ require File.dirname(__FILE__) + "/nagios_config/main"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nagios_config
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matthew Sadler
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-31 00:00:00 +00:00
18
+ date: 2011-03-31 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -47,6 +47,7 @@ files:
47
47
  - lib/nagios_config/builder.rb
48
48
  - lib/nagios_config/errors.rb
49
49
  - lib/nagios_config/formater.rb
50
+ - lib/nagios_config/main.rb
50
51
  - lib/nagios_config/node.rb
51
52
  - lib/nagios_config/object.rb
52
53
  - lib/nagios_config/parser.rb