zabcon 0.0.350 → 0.0.356

Sign up to get free protection for your applications and to get access to all the features.
@@ -70,7 +70,8 @@ class ZabconExecuteCommand < ZabconExecuteBase
70
70
  @proc=cmd_obj.command_obj.method(:execute)
71
71
  @command_obj=cmd_obj.command_obj
72
72
  begin
73
- arg_result=@command_obj.call_arg_processor(cmd_obj.parameters)
73
+ arg_result=@command_obj.call_tokenizer(cmd_obj.parameters)
74
+ arg_result=@command_obj.call_arg_processor(arg_result)
74
75
  #TODO Fix showing help messages
75
76
  #rescue ParameterError => e
76
77
  # e.help_func=cmd_obj.command_obj.help_method
@@ -80,6 +81,7 @@ class ZabconExecuteCommand < ZabconExecuteBase
80
81
  @show_params=arg_result.show_params
81
82
  @printing=cmd_obj.command_obj.print?
82
83
  @command_name=cmd_obj.command_obj.command_name
84
+
83
85
  # @help=cmd_obj.help
84
86
  # @options=options
85
87
 
@@ -115,47 +117,11 @@ class ZabconExecuteContainer
115
117
  attr_reader :show_params, :results, :options
116
118
 
117
119
  def initialize(tokens)
118
- # @initial_string=usr_str
119
120
  @initial_tokens=tokens
120
121
  @commands=[]
121
122
  @printing=true
122
123
  commandlist=CommandList.instance
123
124
 
124
- # tokens=ExpressionTokenizer.new(usr_str)
125
-
126
- #split_str=usr_str.split2(:split_char=>'=|\s', :include_split=>true)
127
- #
128
- #unravel=false #remove any extra space before the first =
129
- #split_str2=split_str.map {|item|
130
- # if unravel
131
- # item
132
- # elsif item=="="
133
- # unravel=true
134
- # item
135
- # elsif item.empty?
136
- # nil
137
- # elsif item.scan(/\s/).empty?
138
- # item
139
- # else
140
- # nil
141
- # end
142
- #}.delete_if {|i| i.nil?}
143
- #
144
- #if !split_str2[1].nil? && split_str2[1]=='='
145
- # split_str=split_str2 #use the trimmed version
146
- # var_name=split_str[0].strip
147
- # raise ParseError.new("Variable names cannot contain spaces or invalid characters \"#{var_name}\"",:retry=>true) if !var_name.scan(/[^\w]/).empty?
148
- #
149
- # debug(5,:var=>var_name,:msg=>"Creating Variable assignment")
150
- # add(ZabconExecuteVariable.new(var_name))
151
- #
152
- # usr_str=split_str[2..split_str.length-1].join.strip
153
- # debug(5,:var=>usr_str,:msg=>"Continuging to parse with")
154
- #end
155
- #
156
- #debug(6,:var=>split_str,:msg=>"Split Str")
157
- #debug(6,:var=>split_str2,:msg=>"Split Str2")
158
- #debug(6,:var=>usr_str,:msg=>"User Str")
159
125
  pos=tokens.walk(0)
160
126
  if (positions=tokens.assignment?(pos,:return_pos=>true))
161
127
  var_name = tokens[positions[0]].value
@@ -164,11 +130,18 @@ class ZabconExecuteContainer
164
130
  tokens=tokens.drop(positions[2]+1)
165
131
  end
166
132
 
167
- cmd_str=tokens.map{|i| i.value}.join
133
+ cmd_str=tokens.map{|i|
134
+ if i.kind==:variable
135
+ name=/^\$(.*)/.match(i.value)[1]
136
+ GlobalVars.instance[name] || env[name]
137
+ else
138
+ i.value
139
+ end
140
+ }.join
141
+
168
142
  debug(5,:msg=>"Command String",:var=>cmd_str)
169
143
  cmd=commandlist.find_and_parse(cmd_str)
170
144
  add(ZabconExecuteCommand.new(cmd))
171
-
172
145
  end
173
146
 
174
147
  def print?
@@ -253,6 +226,11 @@ class Command
253
226
  end
254
227
  end
255
228
 
229
+ #Command Error
230
+ #Raised whenever there is a problem with a command configuration
231
+ class CommandError < ZError
232
+ end
233
+
256
234
  class NonFatalError < Exception
257
235
  end
258
236
 
@@ -286,11 +264,29 @@ class Command
286
264
  #current parameters list for validity.
287
265
  @argument_processor=nil
288
266
  @tokenizer=ExpressionTokenizerHash
267
+ @tokenizer_method=nil
289
268
 
290
269
  @help_tag=nil
291
270
  @depreciated=nil
292
271
  end
293
272
 
273
+ def deprecate_function(new_func)
274
+ #probe the stack to find the deprecated function name
275
+ caller[0]=~/`(.*?)'/
276
+ function=$1
277
+
278
+ #probe the stack again to find the command definition
279
+ caller[1]=~/(.*):(\d+).*/
280
+ path=$1
281
+ line_num=$2
282
+
283
+ warn("Command definition Warning")
284
+ warn(" \"#{function}\" is depreciated and may be removed in future versions")
285
+ warn(" use \"#{new_func}\". Command: \"#{command_name}\", line number #{line_num}")
286
+ warn(" Path: #{path}")
287
+ warn(" Fixing the command definition will remove this warning.")
288
+ end
289
+
294
290
  def command_name
295
291
  @path.join(" ")
296
292
  end
@@ -350,11 +346,18 @@ class Command
350
346
  @depreciated=str
351
347
  end
352
348
 
353
- def arg_processor(&block)
354
- @argument_processor=block
349
+ def arg_processor(method=nil,&block)
350
+ raise CommandError.new("arg_processor cannot be passed a method and a block") if !method.nil? && !block.nil?
351
+
352
+ if !method.nil?
353
+ @argument_processor=method
354
+ else
355
+ @argument_processor=block
356
+ end
355
357
  end
356
358
 
357
359
  def set_arg_processor(method)
360
+ deprecate_function("arg_processor")
358
361
  @argument_processor=method
359
362
  end
360
363
 
@@ -363,9 +366,28 @@ class Command
363
366
  end
364
367
 
365
368
  def set_tokenizer(tokenizer)
369
+ deprecate_function("tokenizer")
366
370
  @tokenizer=tokenizer
367
371
  end
368
372
 
373
+ def tokenizer(tokenizer=nil,&tokenizer_method)
374
+ #Check to see that tokenizer is a descendant of Tokenizer
375
+ #Returns False or nil if for negative results
376
+ if !tokenizer.nil? && (tokenizer <= Tokenizer)
377
+ @tokenizer=tokenizer
378
+ else
379
+ p tokenizer
380
+ raise CommandError.new("Tokenizer must be a descendant of the Tokenizer Class")
381
+ end
382
+
383
+ if tokenizer_method
384
+ if tokenizer_method.arity!=1
385
+ CommandError.new("Tokenizer blocks require an arity of one")
386
+ end
387
+ @tokenizer_method=tokenizer_method
388
+ end
389
+ end
390
+
369
391
  #--
370
392
  #TODO Complete type casting section and add error checking
371
393
  #++
@@ -416,13 +438,19 @@ class Command
416
438
  end
417
439
  end
418
440
 
419
- def call_arg_processor(parameters)
441
+ def call_tokenizer(parameters)
420
442
  debug(6,:msg=>"parameters",:var=>"\"#{parameters.to_s}\"")
421
443
  debug(7,:msg=>"Using tokenizer", :var=>@tokenizer.to_s)
422
- tokenized_parameters=@tokenizer.new(parameters).parse
444
+ tokenized_parameters=@tokenizer.new(parameters)
445
+ tokenized_parameters=@tokenizer_method.call(tokenized_parameters) if @tokenizer_method
423
446
  debug(7,:msg=>"Tokenized Parameters",:var=>tokenized_parameters)
424
- check_parameters(tokenized_parameters)
425
- @arguments=Arguments.new(tokenized_parameters, @flags)
447
+ tokenized_parameters.parse
448
+ end
449
+
450
+ def call_arg_processor(parameters)
451
+ debug(6,:msg=>"parameters",:var=>"\"#{parameters.to_s}\"")
452
+ check_parameters(parameters)
453
+ @arguments=Arguments.new(parameters, @flags)
426
454
  debug(6,:var=>@arguments)
427
455
  @arguments
428
456
  end
@@ -32,6 +32,26 @@ require 'libs/zabcon_exceptions'
32
32
  #require 'zbxapi/exceptions'
33
33
  #require "zbxapi/zdebug"
34
34
 
35
+ class InvalidCharacterSuper <ZabconError
36
+ attr_accessor :invalid_char, :invalid_str, :position
37
+
38
+ def initialize(message=nil, params={})
39
+ super(message,params)
40
+ @message=message || "Invalid Character"
41
+ @position=params[:position] || nil
42
+ @invalid_char=params[:invalid_char] || nil
43
+ @invalid_str=params[:invalid_str] || raise(RuntimeError.new(":invalid_str required",:retry=>false))
44
+ end
45
+
46
+ def show_message
47
+ preamble="#{@message} \"#{@invalid_char}\" : "
48
+ pointer="^".rjust(@position+preamble.length+1)
49
+ puts preamble+@invalid_str
50
+ puts pointer
51
+ end
52
+ end
53
+
54
+
35
55
  #This is a wrapper class for creating a generalized lexer.
36
56
  class Lexr
37
57
 
@@ -92,7 +112,7 @@ class Lexr
92
112
  @res = rule.match(unprocessed_text)
93
113
  next unless @res
94
114
 
95
- raise Lexr::UnmatchableTextError.new(rule.raises, @position) if @res and rule.raises?
115
+ raise Lexr::UnmatchableTextError.new(rule.raises, :position=>@position,:invalid_str=>@text) if @res and rule.raises?
96
116
 
97
117
  @position += @res.characters_matched
98
118
  return self.next if rule.ignore?
@@ -103,7 +123,7 @@ class Lexr
103
123
  @position += @res.characters_matched
104
124
  return @current = @res.token
105
125
  end
106
- raise Lexr::UnmatchableTextError.new(unprocessed_text[0..0], @position)
126
+ raise Lexr::UnmatchableTextError.new(unprocessed_text[0..0], :position=>@position,:invalid_str=>@text)
107
127
  end
108
128
 
109
129
  def end?
@@ -121,10 +141,11 @@ class Lexr
121
141
  end
122
142
 
123
143
  class Token
124
- attr_reader :value, :kind
144
+ attr_reader :value, :kind, :opts
125
145
 
126
- def initialize(value, kind = nil)
127
- @value, @kind = value, kind
146
+ def initialize(value, kind = nil, opts={})
147
+ # @opts, @value, @kind = value, kind, opts
148
+ @value, @kind, @opts = value, kind, opts
128
149
  end
129
150
 
130
151
  def self.method_missing(sym, *args)
@@ -249,16 +270,24 @@ class Lexr
249
270
 
250
271
  end
251
272
 
252
- class UnmatchableTextError < StandardError
253
- attr_reader :character, :position
273
+ class UnmatchableTextError < InvalidCharacterSuper
254
274
 
255
- def initialize(character, position)
256
- @character, @position = character, position
275
+ def initialize(message=nil, params={})
276
+ params[:retry]||=true
277
+ params[:invalid_char]=params[:invalid_str][params[:position]]
278
+ super(message,params)
257
279
  end
258
280
 
259
281
  def message
260
- "Unexpected character '#{character}' at position #{position + 1}"
261
- end
282
+ "#{@message} '#{@invalid_char}' at position #{position + 1}"
283
+ end
284
+
285
+ def show_message
286
+ preamble="#{@message} : "
287
+ pointer="^".rjust(@position+preamble.length+1)
288
+ puts preamble+@invalid_str
289
+ puts pointer
290
+ end
262
291
 
263
292
  def inspect
264
293
  message
@@ -291,6 +320,7 @@ end
291
320
 
292
321
  ExpressionLexer = Lexr.setup {
293
322
  matches /\\/ => :escape
323
+ # matches /\\\"/ =>
294
324
  matches /\$[\w]+/ => :variable
295
325
  matches /"([^"\\]*(\\.[^"\\]*)*)"/ => :quote
296
326
  matches /"([^'\\]*(\\.[^'\\]*)*)"/ => :quote
@@ -302,6 +332,7 @@ ExpressionLexer = Lexr.setup {
302
332
  matches "]" => :r_square, :decrement=>:square
303
333
  matches "," => :comma
304
334
  matches /\s+/ => :whitespace
335
+ matches /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ => :ipv4
305
336
  matches /[-+]?\d*\.\d+/ => :number, :convert_with => lambda { |v| Float(v) }
306
337
  matches /[-+]?\d+/ => :number, :convert_with => lambda { |v| Integer(v) }
307
338
  matches "=" => :equals
@@ -316,6 +347,7 @@ CommandLexer = Lexr.setup {
316
347
  matches /"([^"\\]*(\\.[^"\\]*)*)"/ => :quote
317
348
  matches /"([^'\\]*(\\.[^'\\]*)*)"/ => :quote
318
349
  matches /\s+/ => :whitespace
350
+ matches /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ => :ipv4
319
351
  matches /[-+]?\d*\.\d+/ => :number, :convert_with => lambda { |v| Float(v) }
320
352
  matches /[-+]?\d+/ => :number, :convert_with => lambda { |v| Integer(v) }
321
353
  matches "=" => :equals
@@ -345,23 +377,7 @@ class Tokenizer < Array
345
377
  end
346
378
  end
347
379
 
348
- class InvalidCharacter <ZabconError
349
- attr_accessor :invalid_char, :invalid_str, :position
350
-
351
- def initialize(message=nil, params={})
352
- super(message,params)
353
- @message=message || "Invalid Character"
354
- @position=params[:position] || nil
355
- @invalid_char=params[:invalid_char] || nil
356
- @invalid_str=params[:invalid_str] || raise(RuntimeError.new(":invalid_str required",:retry=>false))
357
- end
358
-
359
- def show_message
360
- preamble="#{@message} \"#{@invalid_char}\" : "
361
- pointer="^".rjust(@position+preamble.length+1)
362
- puts preamble+@invalid_str
363
- puts pointer
364
- end
380
+ class InvalidCharacter <InvalidCharacterSuper
365
381
  end
366
382
 
367
383
  attr_accessor :parsed
@@ -380,12 +396,40 @@ class Tokenizer < Array
380
396
  debug(8,:msg=>"Tokens",:var=>self)
381
397
  debug(8,:msg=>"Tokens(Length)",:var=>length)
382
398
  @available_tokens=args[:lexer].available_tokens
399
+ @class_options||=[]
400
+
401
+ if @class_options.include?(:remove_whitespace)
402
+ delete_if do |i|
403
+ i.kind==:whitespace
404
+ end
405
+ end
406
+
383
407
  end
384
408
 
385
409
  def parse(args={})
386
410
  raise BaseClassError.new
387
411
  end
388
412
 
413
+ #Creates a factory to dynamicly generate a new descendant object with
414
+ #the options passed in, which are available via the variable @class_options
415
+ #*options : array of symbols
416
+ def self.options(*options)
417
+ if options.nil?
418
+ self
419
+ else
420
+ class_name=self.to_s+"_"+options.map {|i| i.to_s}.join
421
+ str =<<-EOF
422
+ class #{class_name} < self
423
+ def initialize(str,args={})
424
+ @class_options=#{options.inspect}
425
+ super(str,args)
426
+ end
427
+ end
428
+ EOF
429
+ eval(str)
430
+ eval(class_name)
431
+ end
432
+ end
389
433
  end
390
434
 
391
435
  class BasicExpressionTokenizer < Tokenizer
@@ -1 +1 @@
1
- REVISION=350
1
+ REVISION=356
@@ -49,7 +49,7 @@ ZabconCommand.add_command "help" do
49
49
  end
50
50
  # set_flag :array_params
51
51
  set_help_tag :help
52
- set_tokenizer SimpleTokenizer
52
+ tokenizer SimpleTokenizer
53
53
  end
54
54
 
55
55
  ZabconCommand.add_command "help commands" do
@@ -71,7 +71,12 @@ ZabconCommand.add_command "login" do
71
71
  end
72
72
  set_help_tag :help
73
73
  # set_flag :array_params
74
- set_tokenizer SimpleTokenizer
74
+ tokenizer(SimpleTokenizer.options(:remove_whitespace))
75
+ #arg_processor do |arg|
76
+ # puts "arg_processor method"
77
+ # p arg
78
+ # arg
79
+ #end
75
80
  end
76
81
 
77
82
  ZabconCommand.add_command "logout" do
@@ -124,7 +129,7 @@ ZabconCommand.add_command "set debug" do
124
129
  depreciated "set env debug=N"
125
130
  # set_flag :array_params
126
131
  set_help_tag :set_debug
127
- set_tokenizer SimpleTokenizer
132
+ tokenizer SimpleTokenizer
128
133
  end
129
134
 
130
135
  ZabconCommand.add_command "set lines" do
@@ -134,7 +139,7 @@ ZabconCommand.add_command "set lines" do
134
139
  depreciated "set env lines=N"
135
140
  # set_flag :array_params
136
141
  set_help_tag :set_lines
137
- set_tokenizer SimpleTokenizer
142
+ tokenizer SimpleTokenizer
138
143
  end
139
144
 
140
145
  ZabconCommand.add_command "set pause" do
@@ -155,7 +160,7 @@ ZabconCommand.add_command "set pause" do
155
160
  end
156
161
  # set_flag :array_params
157
162
  set_help_tag :set_pause
158
- set_tokenizer SimpleTokenizer
163
+ tokenizer SimpleTokenizer
159
164
  end
160
165
 
161
166
  ZabconCommand.add_command "show var" do
@@ -180,7 +185,7 @@ ZabconCommand.add_command "show var" do
180
185
  end
181
186
  set_help_tag :show_var
182
187
  # set_flag :array_params
183
- set_tokenizer SimpleTokenizer
188
+ tokenizer SimpleTokenizer
184
189
  end
185
190
 
186
191
  ZabconCommand.add_command "show env" do
@@ -205,7 +210,7 @@ ZabconCommand.add_command "show env" do
205
210
  end
206
211
  set_help_tag :show_env
207
212
  # set_flag :array_params
208
- set_tokenizer SimpleTokenizer
213
+ tokenizer SimpleTokenizer
209
214
  end
210
215
 
211
216
  ZabconCommand.add_command "set var" do
@@ -235,7 +240,7 @@ ZabconCommand.add_command "unset var" do
235
240
  end
236
241
  # set_flag :array_params
237
242
  set_help_tag :unset_var
238
- set_tokenizer SimpleTokenizer
243
+ tokenizer SimpleTokenizer
239
244
  end
240
245
 
241
246
  ZabconCommand.add_command "show revisions" do
@@ -280,8 +285,8 @@ ZabconCommand.add_command "raw api" do
280
285
  set_flag :print_output
281
286
  set_help_tag :raw_api
282
287
  result_type :raw_api
283
- set_tokenizer CommandTokenizer
284
- # set_tokenizer SimpleTokenizer
288
+ tokenizer CommandTokenizer
289
+ # tokenizer SimpleTokenizer
285
290
  end
286
291
 
287
292
  ZabconCommand.add_command "raw json" do
@@ -310,7 +315,7 @@ ZabconCommand.add_command "raw json" do
310
315
  set_flag :print_output
311
316
  set_help_tag :raw_api
312
317
  result_type :raw_api
313
- set_tokenizer SimpleTokenizerString
318
+ tokenizer SimpleTokenizerString
314
319
  end
315
320
 
316
321
  ###############################################################################
@@ -184,6 +184,7 @@ class ZabconCore
184
184
  }
185
185
  end
186
186
 
187
+
187
188
  def start
188
189
  debug(5,:msg=>"Entering main zabcon start routine")
189
190
  puts "Welcome to Zabcon. Build Number: #{REVISION}" if env["echo"]
@@ -194,10 +195,10 @@ class ZabconCore
194
195
  while line=@input.get_line()
195
196
  #tokens=ExpressionTokenizer.new(line)
196
197
  tokens=CommandTokenizer.new(line)
197
- # line=line.strip_comments
198
- tokens.delete_if {|item| item.kind==:comment}
199
- next if line.nil?
200
- next if line.strip.length==0 # don't bother parsing an empty line'
198
+
199
+ tokens.delete_if {|item| item.kind==:comment }
200
+ next if tokens.nil? || tokens.first.kind==:end ||
201
+ (tokens.first.kind==:whitespace && tokens[1].kind==:end)
201
202
  debug(6, :var=>line, :msg=>"Input from user")
202
203
 
203
204
  commands=ZabconExecuteContainer.new(tokens)
@@ -1,4 +1,9 @@
1
1
  **** 11/23/11 nelsonab
2
+ * Minor input parser modifications. Will not barf on inputs like the following:
3
+ > #comment
4
+ * Code cleanup (removed old comments etc)
5
+
6
+ **** 11/23/11 nelsonab 0.0.350
2
7
  * Added revision file and command to display revision information
3
8
  "show revision"
4
9
  * Added support for multiple custom command files.
@@ -6,4 +11,15 @@
6
11
  Sample can be found here:
7
12
  http://trac.red-tux.net/browser/trunk/ruby/zabcon/zabcon.conf.default
8
13
  * Fixed Raw JSON support
9
- variable substitution currently not working.
14
+ variable substitution currently not working.
15
+
16
+ **** 12/09/11 nelsonab
17
+ * Bumped zbxapi version dependency to 0.1.352
18
+
19
+ *** 12/18/11 nelsonab
20
+ * Fixed login command
21
+ * Added factory method "options" to Tokenizer class
22
+ * Added deprecate_function to Command class
23
+ Will show a warning message with new function name to use along with
24
+ command file path and line numbers/command name
25
+ * Fixed Ruby version check for 1.9.x
data/zabcon.rb CHANGED
@@ -42,7 +42,7 @@ DEPENDENCIES={
42
42
  "parseconfig"=>true,
43
43
  "json"=>true,
44
44
  "highline"=>true,
45
- "zbxapi"=>Gem::Version.new("0.1.294")
45
+ "zbxapi"=>Gem::Version.new("0.1.352")
46
46
  }
47
47
 
48
48
 
@@ -58,6 +58,8 @@ for i in 0..items-1 do
58
58
  puts
59
59
  exit(1)
60
60
  end
61
+ #If this step is higher than the required version break out of loop
62
+ break if ruby_rev[i].to_i>required_rev[i].to_i
61
63
  end
62
64
 
63
65
 
@@ -70,11 +72,14 @@ DEPENDENCIES.each_key {|dep|
70
72
  depsok=false
71
73
  break
72
74
  end
73
- gem=gem[0]
74
- if DEPENDENCIES[dep].class==Gem::Version && gem.version<DEPENDENCIES[dep]
75
- puts "#{dep} needs to be at least version #{DEPENDENCIES[dep]}, found #{gem.version}"
76
- depsok=false
77
- end
75
+ gem.sort!{ |a,b| a.version <=> b.version }
76
+ depsok=depsok && !gem.map { |g|
77
+ if DEPENDENCIES[dep].class==Gem::Version && g.version<DEPENDENCIES[dep]
78
+ "#{dep} needs to be at least version #{DEPENDENCIES[dep]}, found #{g.version}"
79
+ else
80
+
81
+ end
82
+ }.delete_if {|i| i==false}.empty?
78
83
  }
79
84
  if !depsok
80
85
  puts
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zabcon
3
3
  version: !ruby/object:Gem::Version
4
- hash: 675
4
+ hash: 727
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 350
10
- version: 0.0.350
9
+ - 356
10
+ version: 0.0.356
11
11
  platform: ruby
12
12
  authors:
13
13
  - A. Nelson
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: .
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-23 00:00:00 -05:00
18
+ date: 2011-12-18 00:00:00 -05:00
19
19
  default_executable: zabcon
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency