zabcon 0.0.332 → 0.0.350

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,8 +18,8 @@
18
18
 
19
19
  ##########################################
20
20
  # Subversion information
21
- # $Id: argument_processor.rb 330 2011-09-27 06:16:40Z nelsonab $
22
- # $Revision: 330 $
21
+ # $Id: argument_processor.rb 345 2011-11-23 04:28:53Z nelsonab $
22
+ # $Revision: 345 $
23
23
  ##########################################
24
24
 
25
25
  require 'zbxapi/zdebug'
@@ -202,7 +202,7 @@ module ArgumentProcessor
202
202
  # In :num_args is passed with a value, and error will be returned if more than that many args are passed
203
203
 
204
204
  def default_processor(args,arg_info,flags={})
205
- args=args.strip #remove preceding and trailing whitespace
205
+ # args=args.strip #remove preceding and trailing whitespace
206
206
  #valid_args=arg_info[:valid_args]
207
207
  #required_args=arg_info[:required_args]
208
208
  invalid_args=[]
@@ -216,6 +216,7 @@ module ArgumentProcessor
216
216
  elsif flags[:string_params]
217
217
  args=args
218
218
  else
219
+
219
220
  args=params_to_hash(args)
220
221
 
221
222
  if !arg_info[:valid_args].empty?
data/libs/command_tree.rb CHANGED
@@ -19,8 +19,8 @@
19
19
  #--
20
20
  ##########################################
21
21
  # Subversion information
22
- # $Id: command_tree.rb 330 2011-09-27 06:16:40Z nelsonab $
23
- # $Revision: 330 $
22
+ # $Id: command_tree.rb 347 2011-11-23 06:59:47Z nelsonab $
23
+ # $Revision: 347 $
24
24
  ##########################################
25
25
  #++
26
26
 
@@ -29,7 +29,7 @@ require 'libs/lexer'
29
29
  require 'zbxapi/zdebug'
30
30
  require 'libs/zabcon_exceptions'
31
31
  require 'libs/zabcon_globals'
32
- require 'libs/argument_processor'
32
+ #require 'libs/argument_processor'
33
33
 
34
34
  class ZabconExecuteBase
35
35
  include ZDebug
@@ -114,13 +114,14 @@ class ZabconExecuteContainer
114
114
 
115
115
  attr_reader :show_params, :results, :options
116
116
 
117
- def initialize(usr_str)
118
- @initial_string=usr_str
117
+ def initialize(tokens)
118
+ # @initial_string=usr_str
119
+ @initial_tokens=tokens
119
120
  @commands=[]
120
121
  @printing=true
121
122
  commandlist=CommandList.instance
122
123
 
123
- tokens=Tokenizer.new(usr_str)
124
+ # tokens=ExpressionTokenizer.new(usr_str)
124
125
 
125
126
  #split_str=usr_str.split2(:split_char=>'=|\s', :include_split=>true)
126
127
  #
@@ -217,11 +218,12 @@ class Command
217
218
  attr_reader :required_args, :valid_args
218
219
  attr_reader :help_tag, :path
219
220
 
220
- include ArgumentProcessor
221
+ # include ArgumentProcessor
222
+ include ZDebug
221
223
 
222
224
  #Class containing processed arguments to be passed to the command
223
225
  class Arguments
224
- class ParameterError < Exception
226
+ class ParameterError < ZError
225
227
  end
226
228
 
227
229
  attr_accessor :cmd_params
@@ -257,7 +259,7 @@ class Command
257
259
  class LoginRequired < Exception
258
260
  end
259
261
 
260
- class ParameterError < Exception
262
+ class ParameterError < ZError
261
263
  end
262
264
 
263
265
  class ArgumentError < Exception
@@ -276,7 +278,15 @@ class Command
276
278
  @result_type=nil
277
279
 
278
280
  #TODO Can the argument processor stuff be cleaned up?
279
- @argument_processor=method(:default_processor)
281
+ # @argument_processor=method(:default_processor)
282
+ #The argument processor is nil by default.
283
+ #The method call_arg_processor will call the tokenizer and the
284
+ #argument processor if it is not nil.
285
+ #Otherwise a default method will be called which will check the
286
+ #current parameters list for validity.
287
+ @argument_processor=nil
288
+ @tokenizer=ExpressionTokenizerHash
289
+
280
290
  @help_tag=nil
281
291
  @depreciated=nil
282
292
  end
@@ -352,6 +362,10 @@ class Command
352
362
  @help_tag=sym
353
363
  end
354
364
 
365
+ def set_tokenizer(tokenizer)
366
+ @tokenizer=tokenizer
367
+ end
368
+
355
369
  #--
356
370
  #TODO Complete type casting section and add error checking
357
371
  #++
@@ -359,32 +373,60 @@ class Command
359
373
  # :login_required - command requires a valid login
360
374
  # :print_output - the output of the command will be passed to the print processor
361
375
  # :array_params - Only process the parameters as an array
362
- def set_flag(flag)
376
+ def set_flag(flag,val=nil)
363
377
  case flag.class.to_s
364
378
  when "Symbol"
365
- flag={flag=>true}
379
+ flag=val.nil? ? {flag=>true} : {flag=>val}
366
380
  end
367
381
 
368
382
  @flags.merge!(flag)
369
383
  end
370
384
 
371
- def call_arg_processor(parameters)
372
- def set_show_args(args)
373
- @arguments.show_params=args
374
- end
385
+ def check_parameters(parameters)
386
+ return if !parameters.is_a?(Hash)
375
387
 
376
- @arguments=Arguments.new("",@flags)
377
- result=@argument_processor.call(parameters,{:valid_args=>@valid_args,:required_args=>@required_args},@flags)
378
- return result if result.is_a?(Arguments)
379
- if !result.is_a?(String) && !result.is_a?(Hash) && !result.is_a?(Array)
380
- raise ("Arugment processor for \"#{command_name}\" returned invalid parameters: class: #{result.class}, #{result}")
381
- else
382
- @arguments.cmd_params=result
383
- arguments=@arguments
384
- return arguments
388
+ if !@valid_args.empty?
389
+ args_keys=parameters.keys
390
+
391
+ invalid_args=args_keys-@valid_args if @valid_args
392
+ raise ParameterError.new("Invalid parameters: "+invalid_args.join(", "),
393
+ :retry=>true) if !invalid_args.empty?
394
+
395
+ required_args=@required_args.reject{|i| i.class==Array }
396
+ required_or_args=@required_args.reject{|i| i.class!=Array }
397
+
398
+ missing_args=[]
399
+ missing_args=required_args-args_keys
400
+
401
+ required_or_args.delete_if do |i|
402
+ count=i.length
403
+ missing_args<<i if (i-args_keys).count==count
404
+ end
405
+
406
+ if !missing_args.empty?
407
+ msg=missing_args.map do |i|
408
+ if i.class==Array
409
+ "(#{i.join(" | ")})"
410
+ else
411
+ i
412
+ end
413
+ end.join(", ")
414
+ raise ParameterError.new("Missing required arguments: #{msg}",:retry=>true)
415
+ end
385
416
  end
386
417
  end
387
418
 
419
+ def call_arg_processor(parameters)
420
+ debug(6,:msg=>"parameters",:var=>"\"#{parameters.to_s}\"")
421
+ debug(7,:msg=>"Using tokenizer", :var=>@tokenizer.to_s)
422
+ tokenized_parameters=@tokenizer.new(parameters).parse
423
+ debug(7,:msg=>"Tokenized Parameters",:var=>tokenized_parameters)
424
+ check_parameters(tokenized_parameters)
425
+ @arguments=Arguments.new(tokenized_parameters, @flags)
426
+ debug(6,:var=>@arguments)
427
+ @arguments
428
+ end
429
+
388
430
  #Sets the symbold describing the result type. May be used by the print processor
389
431
  # as a hint for printing the output.
390
432
  def result_type(type)
@@ -502,7 +544,8 @@ class CommandList
502
544
 
503
545
  def find_and_parse(str)
504
546
 
505
- tokens=Tokenizer.new(str)
547
+ tokens=CommandTokenizer.new(str)
548
+
506
549
  token_items=tokens.length
507
550
 
508
551
  cur_node=@cmd_tree
@@ -532,7 +575,7 @@ class CommandList
532
575
 
533
576
  debug(6,:msg=>"Tokens", :var=>tokens)
534
577
  debug(6,:msg=>"Pos", :var=>pos)
535
- debug(6,:msg=>"Parsed", :var=>tokens.parse)
578
+ # debug(6,:msg=>"Parsed", :var=>tokens.parse)
536
579
  params = tokens.drop(pos+1).join
537
580
 
538
581
  debug(6,:msg=>"Parameters", :var=>params)
data/libs/lexer.rb CHANGED
@@ -19,8 +19,8 @@
19
19
  #--
20
20
  ##########################################
21
21
  # Subversion information
22
- # $Id: lexer.rb 330 2011-09-27 06:16:40Z nelsonab $
23
- # $Revision: 330 $
22
+ # $Id: lexer.rb 345 2011-11-23 04:28:53Z nelsonab $
23
+ # $Revision: 345 $
24
24
  ##########################################
25
25
  #++
26
26
 
@@ -28,8 +28,9 @@
28
28
  #The origional source for his work can be found here:
29
29
  # https://github.com/michaelbaldry/lexr
30
30
 
31
- require 'zbxapi/exceptions'
32
- require "zbxapi/zdebug"
31
+ require 'libs/zabcon_exceptions'
32
+ #require 'zbxapi/exceptions'
33
+ #require "zbxapi/zdebug"
33
34
 
34
35
  #This is a wrapper class for creating a generalized lexer.
35
36
  class Lexr
@@ -292,6 +293,7 @@ ExpressionLexer = Lexr.setup {
292
293
  matches /\\/ => :escape
293
294
  matches /\$[\w]+/ => :variable
294
295
  matches /"([^"\\]*(\\.[^"\\]*)*)"/ => :quote
296
+ matches /"([^'\\]*(\\.[^'\\]*)*)"/ => :quote
295
297
  matches "(" => :l_paren, :increment=>:paren
296
298
  matches ")" => :r_paren, :decrement=>:paren
297
299
  matches "{" => :l_curly, :increment=>:curly
@@ -305,13 +307,45 @@ ExpressionLexer = Lexr.setup {
305
307
  matches "=" => :equals
306
308
  matches "\"" => :umatched_quote, :raises=> "Unmatched quote"
307
309
  matches /#.*$/ => :comment
308
- default /[^\s^\\^"^\(^\)^\{^\}^\[^\]^,^=]+/ => :word
310
+ default /[^\s^\\^"^'^\(^\)^\{^\}^\[^\]^,^=]+/ => :word
309
311
  }
310
312
 
313
+ CommandLexer = Lexr.setup {
314
+ matches /\\/ => :escape
315
+ matches /\$[\w]+/ => :variable
316
+ matches /"([^"\\]*(\\.[^"\\]*)*)"/ => :quote
317
+ matches /"([^'\\]*(\\.[^'\\]*)*)"/ => :quote
318
+ matches /\s+/ => :whitespace
319
+ matches /[-+]?\d*\.\d+/ => :number, :convert_with => lambda { |v| Float(v) }
320
+ matches /[-+]?\d+/ => :number, :convert_with => lambda { |v| Integer(v) }
321
+ matches "=" => :equals
322
+ matches "\"" => :umatched_quote, :raises=> "Unmatched quote"
323
+ matches /#.*$/ => :comment
324
+ default /[^\s^=^\$^"^']+/ => :word
325
+ }
326
+
327
+ SimpleLexer = Lexr.setup{
328
+ matches /"([^"\\]*(\\.[^"\\]*)*)"/ => :quote
329
+ matches /"([^'\\]*(\\.[^'\\]*)*)"/ => :quote
330
+ matches /\s+/ => :whitespace
331
+ matches "\"" => :umatched_quote, :raises=> "Unmatched quote"
332
+ matches /#.*$/ => :comment
333
+ default /[^\s^"^']+/ => :word
334
+ }
335
+
336
+ #Base Class for all Tokenizers
337
+ #Inherits from Array
311
338
  class Tokenizer < Array
312
339
  include ZDebug
313
340
 
314
- class InvalidCharacter <ZError
341
+ class NoLexer < ZabconError
342
+ def initialize(message=nil, params={})
343
+ super(message,params)
344
+ @message=message || "No Lexer Passed"
345
+ end
346
+ end
347
+
348
+ class InvalidCharacter <ZabconError
315
349
  attr_accessor :invalid_char, :invalid_str, :position
316
350
 
317
351
  def initialize(message=nil, params={})
@@ -328,9 +362,52 @@ class Tokenizer < Array
328
362
  puts preamble+@invalid_str
329
363
  puts pointer
330
364
  end
365
+ end
366
+
367
+ attr_accessor :parsed
368
+ attr :items
369
+
370
+ #Base class version of initialize
371
+ #Will raise an exception if a Lexer is not passed in.
372
+ #Takes a string str and and hash arguments and creates a Lexical token reference of str
373
+ #It will also parse the lexical tokens into an array of items.
374
+ #:keep_escape determines weather or not to keep all escape backslashes, default false
375
+ def initialize(str,args={})
376
+ super()
377
+ raise NoLexer.new if args[:lexer].nil?
378
+ debug(8,:msg=>"Initial String",:var=>str.inspect)
379
+ replace(str.lexer_parse(args[:lexer])) #replace self with array from lexer_parse
380
+ debug(8,:msg=>"Tokens",:var=>self)
381
+ debug(8,:msg=>"Tokens(Length)",:var=>length)
382
+ @available_tokens=args[:lexer].available_tokens
383
+ end
331
384
 
385
+ def parse(args={})
386
+ raise BaseClassError.new
332
387
  end
333
388
 
389
+ end
390
+
391
+ class BasicExpressionTokenizer < Tokenizer
392
+ def initialize(str,args={})
393
+ args[:lexer]||=ExpressionLexer
394
+ super(str,args)
395
+ #@parsed=parse(args)
396
+ #@items=@parsed.clone
397
+ end
398
+
399
+ def parse
400
+ map {|i| #SimpleTokenizer inherits from Array
401
+ next if i.kind==:end
402
+ i.value
403
+ }.compact
404
+
405
+ end
406
+
407
+ end
408
+
409
+ class ExpressionTokenizer < Tokenizer
410
+
334
411
  class UnexpectedClose < InvalidCharacter
335
412
  def initialize(message=nil, params={})
336
413
  super(message,params)
@@ -373,14 +450,10 @@ class Tokenizer < Array
373
450
  #It will also parse the lexical tokens into an array of items.
374
451
  #:keep_escape determines weather or not to keep all escape backslashes, default false
375
452
  def initialize(str,args={})
376
- super()
377
- debug(8,:msg=>"Initial String",:var=>str.inspect)
378
- replace(str.lexer_parse(ExpressionLexer))
379
- debug(8,:msg=>"Tokens",:var=>self)
380
- debug(8,:msg=>"Tokens(Length)",:var=>length)
381
- @available_tokens=ExpressionLexer.available_tokens
382
- @parsed=parse(args)
383
- @items=@parsed.clone
453
+ args[:lexer]||=ExpressionLexer
454
+ super(str,args)
455
+ #@parsed=parse(args)
456
+ #@items=@parsed.clone
384
457
  end
385
458
 
386
459
  def parse(args={})
@@ -870,6 +943,65 @@ class Tokenizer < Array
870
943
  end
871
944
  end
872
945
 
946
+ class ExpressionTokenizerHash < ExpressionTokenizer
947
+
948
+ @default_val = true
949
+
950
+ class InvalidItem <ZabconError
951
+ attr_accessor :invalid_item
952
+
953
+ def initialize(message=nil, params={})
954
+ super(message,params)
955
+ @message=message || "Invalid Token"
956
+ @invalid_item=params[:invalid_item] || nil
957
+ end
958
+
959
+ def show_message
960
+ puts "#{@message} \"#{@invalid_item}\""
961
+ end
962
+ end
963
+
964
+
965
+ def parse(args={})
966
+ parsed=super(args)
967
+ ret_hash={}
968
+ parsed.each do |item|
969
+ if item.is_a?(Hash)
970
+ val=item
971
+ elsif item.is_a?(Numeric) || item.is_a?(String)
972
+ val={item.to_s=>@default_val}
973
+ else
974
+ raise InvaliItem.new("Invalid token for hash key",:invalid_item=>item.to_s)
975
+ end
976
+ ret_hash.merge!(val)
977
+ end
978
+ ret_hash
979
+ end
980
+
981
+ end
982
+
983
+ class CommandTokenizer < ExpressionTokenizer
984
+ def initialize(str,args={})
985
+ args[:lexer]||=CommandLexer
986
+
987
+ super(str,args)
988
+ end
989
+ end
990
+
991
+ class SimpleTokenizer < BasicExpressionTokenizer
992
+ def initialize(str,args={})
993
+ args[:lexer]||=SimpleLexer
994
+
995
+ super(str,args)
996
+ end
997
+ end
998
+
999
+ class SimpleTokenizerString < SimpleTokenizer
1000
+ def parse
1001
+ super.join
1002
+ end
1003
+ end
1004
+
873
1005
 
874
1006
 
875
1007
  #p test_str="\"test\"=test1,2.0,3, 4 \"quote test\" value = { a = { b = [ c = { d = [1,a,g=f,3,4] }, e=5,6,7,8] } }"
@@ -881,6 +1013,6 @@ end
881
1013
  #p test_str="a=b \\(a=c\\)"
882
1014
  #p test_str="\\)"
883
1015
 
884
- #p tokens=Tokenizer.new(test_str)
1016
+ #p tokens=ExpressionTokenizer.new(test_str)
885
1017
  #p result=tokens.parse
886
1018
 
data/libs/revision.rb CHANGED
@@ -1 +1 @@
1
- REVISION=332
1
+ REVISION=350
@@ -17,8 +17,8 @@
17
17
 
18
18
  ##########################################
19
19
  # Subversion information
20
- # $Id: zabcon_commands.rb 332 2011-09-30 07:24:38Z nelsonab $
21
- # $Revision: 332 $
20
+ # $Id: zabcon_commands.rb 350 2011-11-23 09:46:46Z nelsonab $
21
+ # $Revision: 350 $
22
22
  ##########################################
23
23
 
24
24
  require "zbxapi/zdebug"
@@ -47,8 +47,9 @@ ZabconCommand.add_command "help" do
47
47
  end
48
48
  end
49
49
  end
50
- set_flag :array_params
50
+ # set_flag :array_params
51
51
  set_help_tag :help
52
+ set_tokenizer SimpleTokenizer
52
53
  end
53
54
 
54
55
  ZabconCommand.add_command "help commands" do
@@ -69,7 +70,8 @@ ZabconCommand.add_command "login" do
69
70
  server.login
70
71
  end
71
72
  set_help_tag :help
72
- set_flag :array_params
73
+ # set_flag :array_params
74
+ set_tokenizer SimpleTokenizer
73
75
  end
74
76
 
75
77
  ZabconCommand.add_command "logout" do
@@ -120,8 +122,9 @@ ZabconCommand.add_command "set debug" do
120
122
  env["debug"]=params[0]
121
123
  end
122
124
  depreciated "set env debug=N"
123
- set_flag :array_params
125
+ # set_flag :array_params
124
126
  set_help_tag :set_debug
127
+ set_tokenizer SimpleTokenizer
125
128
  end
126
129
 
127
130
  ZabconCommand.add_command "set lines" do
@@ -129,8 +132,9 @@ ZabconCommand.add_command "set lines" do
129
132
  env["lines"]=params[0]
130
133
  end
131
134
  depreciated "set env lines=N"
132
- set_flag :array_params
135
+ # set_flag :array_params
133
136
  set_help_tag :set_lines
137
+ set_tokenizer SimpleTokenizer
134
138
  end
135
139
 
136
140
  ZabconCommand.add_command "set pause" do
@@ -149,8 +153,9 @@ ZabconCommand.add_command "set pause" do
149
153
  end
150
154
  env["lines"]=24 if env["lines"]==0
151
155
  end
152
- set_flag :array_params
156
+ # set_flag :array_params
153
157
  set_help_tag :set_pause
158
+ set_tokenizer SimpleTokenizer
154
159
  end
155
160
 
156
161
  ZabconCommand.add_command "show var" do
@@ -174,7 +179,8 @@ ZabconCommand.add_command "show var" do
174
179
  end
175
180
  end
176
181
  set_help_tag :show_var
177
- set_flag :array_params
182
+ # set_flag :array_params
183
+ set_tokenizer SimpleTokenizer
178
184
  end
179
185
 
180
186
  ZabconCommand.add_command "show env" do
@@ -198,7 +204,8 @@ ZabconCommand.add_command "show env" do
198
204
  end
199
205
  end
200
206
  set_help_tag :show_env
201
- set_flag :array_params
207
+ # set_flag :array_params
208
+ set_tokenizer SimpleTokenizer
202
209
  end
203
210
 
204
211
  ZabconCommand.add_command "set var" do
@@ -226,39 +233,62 @@ ZabconCommand.add_command "unset var" do
226
233
  }
227
234
  end
228
235
  end
229
- set_flag :array_params
236
+ # set_flag :array_params
230
237
  set_help_tag :unset_var
238
+ set_tokenizer SimpleTokenizer
231
239
  end
232
240
 
233
- ZabconCommand.add_command "raw api" do
241
+ ZabconCommand.add_command "show revisions" do
234
242
  set_method do |params|
235
- api_func=params[:method]
236
- params=params[:params]
237
- server.connection.raw_api(api_func, params)
243
+ revision_path=File.join(ZABCON_PATH,"revision_information")
244
+ raise ZabconError.new("Revision file not found!",:retry=>true) if !File.file?(revision_path)
245
+ revision_file=File.new(revision_path)
246
+ while !revision_file.eof
247
+ puts revision_file.gets
248
+ end
249
+ revision_file.close
238
250
  end
251
+ set_help_tag :show_revision
252
+ end
239
253
 
240
- arg_processor do |params,args,flags|
241
- params=Tokenizer.new(params).parse
242
- parameter_error "Command \"raw api\" requires parameters" if params.empty?
254
+ ZabconCommand.add_command "raw api" do
255
+ set_method do |params|
243
256
  api_func=params[0]
244
257
  params.delete_at(0)
245
- params2={}
246
- params.each do |i|
247
- params2.merge!(i)
258
+ args={}
259
+ if !params.empty?
260
+ args=ExpressionTokenizerHash.new(params.join(" ")).parse
248
261
  end
249
- {:method=>api_func, :params=>params2}
250
- end
262
+ #api_func=params[:method]
263
+ #params=params[:params]
264
+ server.connection.raw_api(api_func, args)
265
+ end
266
+
267
+ #arg_processor do |params,args,flags|
268
+ # debug(6,:var=>params)
269
+ # params=ExpressionTokenizer.new(params).parse
270
+ # parameter_error "Command \"raw api\" requires parameters" if params.empty?
271
+ # api_func=params[0]
272
+ # params.delete_at(0)
273
+ # params2={}
274
+ # params.each do |i|
275
+ # params2.merge!(i)
276
+ # end
277
+ # {:method=>api_func, :params=>params2}
278
+ #end
251
279
  set_flag :login_required
252
280
  set_flag :print_output
253
281
  set_help_tag :raw_api
254
282
  result_type :raw_api
283
+ set_tokenizer CommandTokenizer
284
+ # set_tokenizer SimpleTokenizer
255
285
  end
256
286
 
257
287
  ZabconCommand.add_command "raw json" do
258
288
  set_method do |params|
259
289
  begin
260
290
  result=server.connection.do_request(params)
261
- return result["result"]
291
+ retval=result["result"]
262
292
  rescue ZbxAPI_GeneralError => e
263
293
  puts "An error was received from the Zabbix server"
264
294
  if e.message.class==Hash
@@ -267,15 +297,20 @@ ZabconCommand.add_command "raw json" do
267
297
  puts "Error data: #{e.message["data"]}"
268
298
  end
269
299
  puts "Original text:"
270
- puts parameters
300
+ puts params
271
301
  puts
272
- return nil
302
+ retval=nil
273
303
  end
304
+ retval
274
305
  end
306
+ #arg_processor do |params,args,flags|
307
+ # params
308
+ #end
275
309
  set_flag :login_required
276
310
  set_flag :print_output
277
311
  set_help_tag :raw_api
278
312
  result_type :raw_api
313
+ set_tokenizer SimpleTokenizerString
279
314
  end
280
315
 
281
316
  ###############################################################################
@@ -519,7 +554,7 @@ ZabconCommand.add_command "update user" do
519
554
  valid_parameters.each {|key| p_keys.delete(key)}
520
555
  if !p_keys.empty? then
521
556
  puts "Invalid items"
522
- p p_keys
557
+ debug(8,p_keys)
523
558
  return false
524
559
  elsif parameters["userid"].nil?
525
560
  puts "Missing required userid statement."
data/libs/zabcon_core.rb CHANGED
@@ -20,11 +20,11 @@
20
20
 
21
21
  ##########################################
22
22
  # Subversion information
23
- # $Id: zabcon_core.rb 330 2011-09-27 06:16:40Z nelsonab $
24
- # $Revision: 330 $
23
+ # $Id: zabcon_core.rb 350 2011-11-23 09:46:46Z nelsonab $
24
+ # $Revision: 350 $
25
25
  ##########################################
26
26
 
27
- require 'libs/utility_items'
27
+ #require 'libs/utility_items'
28
28
  require 'libs/revision'
29
29
  require 'parseconfig'
30
30
  require 'ostruct'
@@ -39,14 +39,13 @@ require 'libs/command_tree'
39
39
  require 'libs/command_help'
40
40
  require 'libs/zabcon_globals'
41
41
  require 'libs/zabcon_commands'
42
-
43
-
42
+ require 'libs/lexer'
43
+ require 'pp'
44
44
 
45
45
  class ZabconCore
46
46
 
47
47
  include ZDebug
48
48
 
49
-
50
49
  def initialize()
51
50
  # This must be set first or the debug module will throw an error
52
51
  set_debug_level(env["debug"])
@@ -56,12 +55,10 @@ class ZabconCore
56
55
 
57
56
  @printer=OutputPrinter.new
58
57
  debug(5,:msg=>"Setting up help")
59
- # @cmd_help=CommandHelp.new("english") # Setup help functions, determine default language to use
60
58
  CommandHelp.setup("english")
61
59
 
62
60
  #TODO Remove reference to ArgumentProcessor when new command objects in use
63
61
  debug(5,:msg=>"Setting up ArgumentProcessor")
64
- # @arg_processor=ArgumentProcessor.new # Need to instantiate for debug routines
65
62
 
66
63
  if !env["server"].nil? and !env["username"].nil? and !env["password"].nil? then
67
64
  puts "Found valid login credentials, attempting login" if env["echo"]
@@ -93,9 +90,10 @@ class ZabconCore
93
90
  end
94
91
 
95
92
  ###############################################################################
93
+ # Configure the history command.
96
94
  ###############################################################################
97
95
  zabconcore=self
98
- if @input.respond_to?(:history)
96
+ if @input.respond_to?(:history) #does our input object have a history method?
99
97
  ZabconCommand.add_command "history" do
100
98
  set_method do zabconcore.show_history end
101
99
  set_help_tag :history
@@ -111,57 +109,80 @@ class ZabconCore
111
109
 
112
110
  debug(5,:msg=>"Setting up custom commands")
113
111
 
114
- if !env["custom_commands"].nil?
115
- filename=nil
116
- cmd_file=env["custom_commands"]
117
- filename=File.exist?(cmd_file) && cmd_file
118
- cmd_file=File::expand_path("~/#{env["custom_commands"]}")
119
- filename=File.exist?(cmd_file) && cmd_file if filename.class!=String
120
- if filename.class==String
121
- puts "Loading custom commands from #{filename}" if env["echo"]
122
- begin
123
- load filename
124
- rescue Exception=> e
125
- puts "There was an error loading your custom commands"
126
- p e
112
+
113
+ if !(custom_commands=env["custom_commands"]).nil?
114
+ if custom_commands.is_a?(String)
115
+ load_command_path(custom_commands)
116
+ elsif custom_commands.is_a?(Hash)
117
+ base_path=custom_commands["base_path"]
118
+ custom_commands.delete("base_path") if base_path
119
+ show_load=custom_commands["show_load"]
120
+ if show_load
121
+ show_load=show_load.downcase
122
+ custom_commands.delete("show_load")
123
+ if !show_load.is_a?(String) ||
124
+ !(show_load=="total" || show_load=="all")
125
+ warn "Invalid value for show_load: #{custom_commands}"
126
+ warn "Valid arguments are \"all\" and \"total\""
127
+ env["show_load"]=nil
128
+ else
129
+ env["show_load"]=show_load
130
+ end
131
+ else
132
+ env["show_load"]="all"
127
133
  end
134
+
135
+ env["load_count"]=0
136
+
137
+ custom_commands.each { |k,v|
138
+ load_command_path(v,base_path,show_load)
139
+ }
140
+
141
+ puts "#{env["load_count"]} Custom command files loaded" if show_load=="total"
142
+ else
143
+ warn "\"#{custom_commands.to_s}\" is an invalid parameter for custom_commands"
128
144
  end
129
145
  end
130
146
 
131
147
  debug(5,:msg=>"Setup complete")
132
148
  end
133
149
 
134
- # #TODO The following method may need to be removed when new command object in use
135
- # # Argument logged in is used to determine which set of commands to load. If loggedin is true then commands which
136
- # # require a valid login are loaded
137
- # def setupcommands(loggedin)
138
- # debug(5,loggedin,"Starting setupcommands (loggedin)")
139
- #
140
- #
141
- ## no_cmd=nil
142
- ## no_args=nil
143
- ## no_help=nil
144
- ## no_verify=nil
145
- #
146
- # login_required = lambda {
147
- # debug(6,"Lambda 'login_required'")
148
- # puts "Login required"
149
- # }
150
- #
151
- # # parameters for insert: insert_path, command, commandproc, arguments=[], helpproc=nil, verify_func=nil, options
152
- #
153
- # #Import commented out until fixed
154
- # #@commands.insert ["import"], self.method(:do_import),no_args,@cmd_help.method(:import),@arg_processor.default,:not_empty, :use_array_processor, :num_args=>"==1"
155
- #
156
- # @commands.insert ["add","app","id"], @server.method(:getappid),no_args,no_help,no_verify
157
- # @commands.insert ["add","link"], @server.method(:addlink),no_args,no_help,no_verify
158
- # @commands.insert ["add","link","trigger"], @server.method(:addlinktrigger),no_args,no_help,no_verify
159
- # @commands.insert ["add","sysmap"], @server.method(:addsysmap),no_args,no_help,no_verify
160
- # @commands.insert ["add","sysmap","element"], @server.method(:addelementtosysmap),no_args,no_help,no_verify
161
- # @commands.insert ["add","user","media"], @server.method(:addusermedia),no_args,@cmd_help.method(:add_user_media),no_verify
162
- #
163
- # @commands.insert ["get","host","group","id"], @server.method(:gethostgroupid), no_args, no_help, @arg_processor.method(:get_group_id)
164
- # @commands.insert ["get","seid"], @server.method(:getseid), no_args, no_help, @arg_processor.default_get
150
+ def load_command_path(path,base_path=nil,show_load=nil)
151
+ base_path||="~"
152
+ load_path=nil
153
+ path=File.expand_path(path,base_path)
154
+ if File.file?(path)
155
+ load_path=[path]
156
+ elsif File.directory?(path)
157
+ load_path=Dir.entries(path).map { |i|
158
+ if i =~ /^\..*/
159
+ nil
160
+ else
161
+ File.join(path,i)
162
+ end
163
+ }
164
+ load_path.compact!
165
+ else
166
+ load_path=Dir[path]
167
+ end
168
+
169
+ return if load_path.nil? || load_path.empty?
170
+
171
+ load_path.each { |f|
172
+ env["load_count"]+=1
173
+ puts "Loading custom commands from #{f}" if env["echo"] && env["show_load"]=="all"
174
+
175
+ begin
176
+ load f
177
+ rescue Exception=> e
178
+ warn "*** Error loading custom commands ***"
179
+ warn "#{e.class.to_s}: #{e}"
180
+ warn "Custom commands from: #{f} were not loaded."
181
+ warn ""
182
+ env["load_count"]-=1
183
+ end
184
+ }
185
+ end
165
186
 
166
187
  def start
167
188
  debug(5,:msg=>"Entering main zabcon start routine")
@@ -171,12 +192,15 @@ class ZabconCore
171
192
  begin
172
193
  catch(:exit) do
173
194
  while line=@input.get_line()
174
- line=line.strip_comments
195
+ #tokens=ExpressionTokenizer.new(line)
196
+ tokens=CommandTokenizer.new(line)
197
+ # line=line.strip_comments
198
+ tokens.delete_if {|item| item.kind==:comment}
175
199
  next if line.nil?
176
200
  next if line.strip.length==0 # don't bother parsing an empty line'
177
201
  debug(6, :var=>line, :msg=>"Input from user")
178
202
 
179
- commands=ZabconExecuteContainer.new(line)
203
+ commands=ZabconExecuteContainer.new(tokens)
180
204
  debug(8,:var=>commands,:msg=>"Commands tree")
181
205
 
182
206
  commands.execute
@@ -241,26 +265,6 @@ class ZabconCore
241
265
  @server.login? ? " #{debug_part}+> " : " #{debug_part}-> "
242
266
  end
243
267
 
244
- # def set_lines(input)
245
- # @printer.sheight=input.keys[0].to_i
246
- # end
247
-
248
- # def set_pause(input)
249
- # if input.nil? then
250
- # puts "set pause requires either Off or On"
251
- # return
252
- # end
253
- #
254
- # if input.keys[0].upcase=="OFF"
255
- # @printer.sheight=@printer.sheight.abs*(-1)
256
- # elsif input.keys[0].upcase=="ON"
257
- # @printer.sheight=@printer.sheight.abs
258
- # else
259
- # puts "set pause requires either Off or On"
260
- # end
261
- # @printer.sheight = 24 if @printer.sheight==0
262
- # end
263
-
264
268
  def set_debug(input)
265
269
  if input["prompt"].nil? then
266
270
  puts "This command is deprecated, please use \"set env debug=n\""
@@ -275,29 +279,6 @@ class ZabconCore
275
279
  set_facility_debug_level(:api,value)
276
280
  end
277
281
 
278
- # def set_var(input)
279
- # debug(6,input)
280
- # input.each {|key,val|
281
- # GlobalVars.instance[key]=val
282
- # puts "#{key} : #{val.inspect}"
283
- # }
284
- # end
285
-
286
- # def unset_var(input)
287
- # if input.empty?
288
- # puts "No variables given to unset"
289
- # else
290
- # input.each {|item|
291
- # if GlobalVars.instance[item].nil?
292
- # puts "#{item} *** Not Defined ***"
293
- # else
294
- # GlobalVars.instance.delete(item)
295
- # puts "#{item} Deleted"
296
- # end
297
- # }
298
- # end
299
- # end
300
-
301
282
  #
302
283
  # Import config from an XML file:
303
284
  #
@@ -18,8 +18,8 @@
18
18
 
19
19
  ##########################################
20
20
  # Subversion information
21
- # $Id: zabcon_exceptions.rb 325 2011-09-26 08:57:00Z nelsonab $
22
- # $Revision: 325 $
21
+ # $Id: zabcon_exceptions.rb 333 2011-10-12 16:58:11Z nelsonab $
22
+ # $Revision: 333 $
23
23
  ##########################################
24
24
 
25
25
  #------------------------------------------------------------------------------
@@ -43,3 +43,11 @@ end
43
43
 
44
44
  class ZabconError < ZError
45
45
  end
46
+
47
+ class BaseClassError < ZabconError
48
+ def initialize(message=nil, params={})
49
+ super(message,params)
50
+ @message=mesasge || "Base Class Method Called"
51
+ end
52
+ end
53
+
@@ -0,0 +1,9 @@
1
+ **** 11/23/11 nelsonab
2
+ * Added revision file and command to display revision information
3
+ "show revision"
4
+ * Added support for multiple custom command files.
5
+ See Sample command file for full information.
6
+ Sample can be found here:
7
+ http://trac.red-tux.net/browser/trunk/ruby/zabcon/zabcon.conf.default
8
+ * Fixed Raw JSON support
9
+ variable substitution currently not working.
data/zabcon.conf.default CHANGED
@@ -27,15 +27,40 @@ password=zabbix
27
27
  #output related environment variables
28
28
 
29
29
  # Are we displaying a well formatted table or csv? Zabcon will automatically
30
- # set this variable to true for intaractive use.
30
+ # set this variable to true for interactive use.
31
31
  # true = table false = csv
32
32
  #table_output=true
33
33
 
34
34
  # Print the table header?
35
35
  #table_header=true
36
36
 
37
- # The default seperator character for csv output.
37
+ # The default separator character for csv output.
38
38
  #table_separator=","
39
39
 
40
+ [custom_commands]
41
+ #This is a sub section which deals with Custom commands
42
+ #ONLY CUSTOM COMMAND INFORMATION IS ALLOWED IN THIS SECTION
43
+ #Custom commands can be explicitly called paths or globs may be given.
44
+ #Each entry however must have a unique name. There are two reserved names
45
+ #in this section "base_path" and "show_load".
46
+ #base_path denotes the base path to be used for relative paths, by default
47
+ #it is ~ (home directory of the current user)
48
+ #show_load has two values "show" and "all" and is case insensitive.
49
+ #"all" (default) will show each file being loaded if echo is enabled
50
+ #"total" will only show a total of how many files were loaded if echo is
51
+ #enabled.
52
+
53
+ #The following are some examples
54
+ #base_path=/usr/local/zabcon
55
+ #show_load=all
56
+
57
+ #dir1=custom_commands
58
+ #If custom_commands is a directory in /usr/local/zabcon all files i that
59
+ #directory, except (dot) files, will be loaded because the base search
60
+ #path is /usr/loca/zabcon
61
+
62
+ #dir2=~/zabcon_commands
63
+ #file1=~/my_zabcon_commands.rb
64
+ #file2=~/ruby/zabcon*.rb
40
65
  # File to store custom commands
41
66
  #custom_commands=sample_custom_commands
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: 647
4
+ hash: 675
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 332
10
- version: 0.0.332
9
+ - 350
10
+ version: 0.0.350
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-09-30 00:00:00 -04:00
18
+ date: 2011-11-23 00:00:00 -05:00
19
19
  default_executable: zabcon
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -74,6 +74,7 @@ files:
74
74
  - zabcon.rb
75
75
  - zabcon.conf.default
76
76
  - README
77
+ - revision_information
77
78
  - libs/argument_processor.rb
78
79
  - libs/revision.rb
79
80
  - libs/command_help.rb