tools 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,91 @@
1
+ module HashRecursiveBlank
2
+ def rblank
3
+ r = {}
4
+ each_pair do |key, val|
5
+ r[key] = val.rblank if val.is_a?(Hash)
6
+ end
7
+ return r.keep_if { |key, val| val.is_a?(Hash) }
8
+ end
9
+
10
+ def rblank!
11
+ each_pair do |key, val|
12
+ self[key] = val.rblank! if val.is_a?(Hash)
13
+ end
14
+ return keep_if { |key, val| val.is_a?(Hash) }
15
+ end
16
+ end
17
+
18
+ module HashRecursiveMerge
19
+ def rmerge(other_hash, concat_if_array = false)
20
+ r = {}
21
+ return merge(other_hash) do |key, oldval, newval|
22
+ if oldval.is_a?(Hash)
23
+ r[key] = oldval.rmerge(newval, concat_if_array)
24
+ elsif oldval.is_a?(Array) and newval.is_a?(Array)
25
+ r[key] = concat_if_array ? oldval + newval : newval
26
+ else
27
+ newval
28
+ end
29
+ end
30
+ end
31
+
32
+ def rmerge!(other_hash, concat_if_array = false)
33
+ return merge!(other_hash) do |key, oldval, newval|
34
+ if oldval.is_a?(Hash)
35
+ oldval.rmerge!(newval, concat_if_array)
36
+ elsif oldval.is_a?(Array) and newval.is_a?(Array)
37
+ concat_if_array ? oldval + newval : newval
38
+ else
39
+ newval
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ class Hash
46
+ include HashRecursiveMerge
47
+ include HashRecursiveBlank
48
+
49
+ def diff(other)
50
+ (self.keys + other.keys).uniq.inject({}) do |memo, key|
51
+ unless self[key] == other[key]
52
+ if self[key].kind_of?(Hash) && other[key].kind_of?(Hash)
53
+ memo[key] = self[key].diff(other[key])
54
+ else
55
+ memo[key] = [self[key], other[key]]
56
+ end
57
+ end
58
+ memo
59
+ end
60
+ end
61
+ end
62
+
63
+
64
+ class Hash
65
+
66
+ # ensures nested hash from keys, and sets final key to value
67
+ # keys: Array of Symbol|String
68
+ # value: any
69
+ def nested_set(keys, value)
70
+ raise "DEBUG: nested_set keys must be an Array" unless keys.is_a?(Array)
71
+
72
+ final_key = keys.pop
73
+ return unless valid_key?(final_key)
74
+ position = self
75
+ for key in keys
76
+ return unless valid_key?(key)
77
+ position[key] = {} unless position[key].is_a?(Hash)
78
+ position = position[key]
79
+ end
80
+ position[final_key] = value
81
+ end
82
+
83
+ private
84
+
85
+ # returns true if key is valid
86
+ def valid_key?(key)
87
+ return true if key.is_a?(Symbol) || key.is_a?(String)
88
+ raise "DEBUG: nested_set invalid key: #{key} (#{key.class})"
89
+ end
90
+
91
+ end
@@ -3,10 +3,8 @@ class ToolsLog
3
3
  include Singleton
4
4
 
5
5
  def initialize(options = {})
6
-
7
6
  end
8
7
 
9
-
10
8
  # Create a Log file in work area
11
9
  #
12
10
  # Sample
@@ -57,10 +55,8 @@ class ToolsLog
57
55
  when 'ucolor'
58
56
  color = nil
59
57
  logger_method = 'info'
60
- when 'exit'
61
- log_file = ToolsUtil.get_variable "#{logger_name}_log_file"
62
- ToolsDisplay.show "\tError in ToolsUtil. See details in '#{log_file}'", :light_yellow
63
- exit
58
+ else
59
+ return false
64
60
  end
65
61
  end
66
62
  unless color.nil?
@@ -2,6 +2,9 @@ require 'singleton'
2
2
  class ToolsNet
3
3
  include Singleton
4
4
 
5
+ def initialize(options = {})
6
+ end
7
+
5
8
  def self.ping? host
6
9
  return Net::Ping::External.new(host || '0.0.0.1000', timeout=1).ping?
7
10
  end
@@ -12,7 +15,10 @@ class ToolsNet
12
15
  # return ip if IPAddress.valid?(ip)
13
16
  # rescue Exception => e
14
17
  # end
15
- ip = `ifconfig | grep -E '10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | awk '{print $2}'`
18
+ ip = (`ifconfig | grep -E '10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | awk '{print $2}'`).split("\n").first
19
+ unless valid_ip? ip
20
+ ip = (`ifconfig | grep -E '192\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | awk '{print $2}'`).split("\n").first
21
+ end
16
22
  ip = ip.split("\n").first if ip.include? "\n"
17
23
  return ip if IPAddress.valid?(ip)
18
24
  end
@@ -202,17 +208,19 @@ class ToolsNet
202
208
  result[:type] = 'mask'
203
209
  result[:status] = true
204
210
  rescue Exception => e
205
- ip = resolv_dns that
206
- if IPAddress::valid_ipv4? ip
207
- result[:status] = true
208
- result[:type] = 'ip'
209
- result[:oct1] = ip.split('.')[0]
210
- result[:oct2] = ip.split('.')[1]
211
- result[:oct3] = ip.split('.')[2]
212
- result[:oct4] = ip.split('.')[3]
213
- else
214
- result[:msg] = e.to_s
215
- end
211
+ result[:type] = ''
212
+ result[:status] = false
213
+ # ip = resolv_dns that
214
+ # if IPAddress::valid_ipv4? ip
215
+ # result[:status] = true
216
+ # result[:type] = 'ip'
217
+ # result[:oct1] = ip.split('.')[0]
218
+ # result[:oct2] = ip.split('.')[1]
219
+ # result[:oct3] = ip.split('.')[2]
220
+ # result[:oct4] = ip.split('.')[3]
221
+ # else
222
+ # result[:msg] = e.to_s
223
+ # end
216
224
  end
217
225
  end
218
226
  return result
@@ -0,0 +1,42 @@
1
+ class Object
2
+ # Self test Boolean class.
3
+ #
4
+ # @return boolean
5
+ def boolean?
6
+ self.is_a?(TrueClass) || self.is_a?(FalseClass)
7
+ end
8
+
9
+ # Self test Trueclass.
10
+ #
11
+ # @return boolean
12
+ def true?
13
+ self.is_a?(TrueClass)
14
+ end
15
+ # Self test Falseclass.
16
+ #
17
+ # @return boolean
18
+ def false?
19
+ self.is_a?(FalseClass)
20
+ end
21
+
22
+ # Self test Symbol class.
23
+ #
24
+ # @return boolean
25
+ def symbol?
26
+ self.is_a?(Symbol)
27
+ end
28
+ # Self test String class.
29
+ #
30
+ # @return boolean
31
+ def string?
32
+ self.is_a?(String)
33
+ end
34
+
35
+ # Self test nil Object class.
36
+ #
37
+ # @return boolean
38
+ # def nil?
39
+ # return '' if self == nil
40
+ # end
41
+
42
+ end
@@ -41,7 +41,7 @@ class ToolsPrompt
41
41
 
42
42
  def self.enum_select *args
43
43
  prompt = TTY::Prompt.new
44
- result = prompt.multi_select *args
44
+ result = prompt.enum_select *args
45
45
  return result
46
46
  end
47
47
 
@@ -0,0 +1,83 @@
1
+ class String
2
+
3
+ # Justity relative left or right position filled with a espefific char in String.
4
+ #
5
+ # sample:
6
+ #
7
+ # "TESTE".fix(10,'xy') # => xxxxxTESTE
8
+ # "TESTE".fix(-10,'xy') # => TESTExxxxx
9
+ #
10
+ # @param size to justify.
11
+ # @param pattern pattern do justify
12
+ # @return formated string
13
+ def fix(size, pattern=' ')
14
+ if size >= 0
15
+ self[0...size].rjust(size, pattern)
16
+ else
17
+ diff = size.abs - self.size
18
+ self + ''.fix(diff,pattern)
19
+ end
20
+ end
21
+
22
+ # Encrypt a string using a key.
23
+ # sample
24
+ # msg = "teste do encrypt".light_blue
25
+ # passwd = 'tools999'
26
+ # encrypted = msg.encrypt passwd
27
+ # puts (encrypted.decrypt passwd)
28
+ # @return encrypt string
29
+ def encrypt(key)
30
+ Encrypt.dump self, key
31
+ end
32
+
33
+ # Decrypt a string using a key.
34
+ # sample
35
+ # msg = "teste do encrypt".light_blue
36
+ # passwd = 'tools999'
37
+ # encrypted = msg.encrypt passwd
38
+ # puts (encrypted.decrypt passwd)
39
+ # @return decrypt string
40
+ def decrypt(key)
41
+ Encrypt.load self, key
42
+ end
43
+
44
+ # Self test numeric String class.
45
+ #
46
+ # @return boolean
47
+ def numeric?
48
+ Float(self) != nil rescue false
49
+ end
50
+
51
+ # Self test digits String class.
52
+ #
53
+ # @return boolean
54
+ def num?
55
+ !!match(/^[[:digit:]]+$/)
56
+ end
57
+
58
+ # Self test alphanum String class.
59
+ #
60
+ # @return boolean
61
+ def alnum?
62
+ !!match(/^[[:alnum:]]+$/)
63
+ end
64
+
65
+ # Self test alpha String class.
66
+ #
67
+ # @return boolean
68
+ def alpha?
69
+ !!match(/^[[:alpha:]]+$/)
70
+ end
71
+
72
+ def help?
73
+ if self.eql? '?' or
74
+ self.eql? '-h' or
75
+ self.eql? '--help' or
76
+ self.eql? 'help'
77
+ return true
78
+ else
79
+ return false
80
+ end
81
+ end
82
+
83
+ end
@@ -2,20 +2,23 @@ require 'singleton'
2
2
  class ToolsUtil
3
3
  include Singleton
4
4
 
5
- def initialize(options = {})
6
- I18n.load_path = Dir[Tools.files + '/pt-BR.yml']
7
- I18n.locale = 'pt-BR'.to_sym
8
- ToolsDisplay.instance
9
- ToolsFiles.instance
10
- ToolsConfig.instance
11
- ToolsLog.instance
12
- ToolsPrompt.instance
13
- unless File.exists? Tools.home+'/.tools'
14
- FileUtils.mkdir_p(Tools.home+'/.tools')
15
- end
16
- tools_logfile = Tools.home+'/.tools/tools.log'
17
- ToolsLog.create_log_file 'tools', tools_logfile
18
- end
5
+ # def initialize(options = {})
6
+ # # I18n.load_path = Dir[Tools.files + '/pt-BR.yml']
7
+ # # I18n.locale = 'pt-BR'.to_sym
8
+ # ToolsCache.instance
9
+ # ToolsConfig.instance
10
+ # ToolsConsole.instance
11
+ # ToolsDisplay.instance
12
+ # ToolsFiles.instance
13
+ # ToolsLog.instance
14
+ # ToolsNet.instance
15
+ # ToolsPrompt.instance
16
+ # unless File.exists? Tools.home+'/.tools'
17
+ # FileUtils.mkdir_p(Tools.home+'/.tools')
18
+ # end
19
+ # tools_logfile = Tools.home+'/.tools/tools.log'
20
+ # ToolsLog.create_log_file 'tools', tools_logfile
21
+ # end
19
22
 
20
23
  # Synbolize all keys in hash.
21
24
  #
@@ -37,18 +40,6 @@ class ToolsUtil
37
40
  end
38
41
 
39
42
 
40
- # Capture string from a prompt.
41
- #
42
- # @param prompt
43
- # @param hidden option
44
- # @return String
45
- def self.ask_prompt(prompt = '', hidden = true)
46
- if hidden
47
- a = ask("#{prompt}") { |q| q.echo = "*"; } #q.case = :downcase }
48
- else
49
- a = ask("#{prompt}") #{ |q| q.case = :downcase }
50
- end
51
- end
52
43
 
53
44
  # Test a valid json string.
54
45
  #
@@ -125,16 +116,6 @@ class ToolsUtil
125
116
  return self.instance_variable_get("@#{variable}")
126
117
  end
127
118
 
128
- # Check a existent class variable.
129
- #
130
- # @param variable variable name to retrive
131
- # @return boolean
132
- def self.instance_variable? variable
133
- # ap self.instance_variables.include? variable.to_sym
134
- # exit
135
- return self.instance_variable_get(":@#{variable}")
136
- end
137
-
138
119
  # Get all existent class variablea.
139
120
  #
140
121
  # @return variables
@@ -163,378 +144,4 @@ class ToolsUtil
163
144
  end
164
145
  end
165
146
 
166
-
167
-
168
-
169
- ##### sem minitest
170
-
171
- def self.purge_files path, select, time #Cmdapi.configuration.home+'/.cmdapi/backup', '*', 14*24*60*60
172
- to_clean = Dir.glob(File.join(path, select)).select { |a|
173
- Time.now - File.ctime(a) > time }
174
- to_clean.each do |file_to_delete|
175
- File.delete(file_to_delete)
176
- end
177
- end
178
-
179
- end
180
-
181
- module HashRecursiveBlank
182
- def rblank
183
- r = {}
184
- each_pair do |key, val|
185
- r[key] = val.rblank if val.is_a?(Hash)
186
- end
187
- return r.keep_if { |key, val| val.is_a?(Hash) }
188
- end
189
-
190
- def rblank!
191
- each_pair do |key, val|
192
- self[key] = val.rblank! if val.is_a?(Hash)
193
- end
194
- return keep_if { |key, val| val.is_a?(Hash) }
195
- end
196
- end
197
-
198
- module HashRecursiveMerge
199
- def rmerge(other_hash, concat_if_array = false)
200
- r = {}
201
- return merge(other_hash) do |key, oldval, newval|
202
- if oldval.is_a?(Hash)
203
- r[key] = oldval.rmerge(newval, concat_if_array)
204
- elsif oldval.is_a?(Array) and newval.is_a?(Array)
205
- r[key] = concat_if_array ? oldval + newval : newval
206
- else
207
- newval
208
- end
209
- end
210
- end
211
-
212
- def rmerge!(other_hash, concat_if_array = false)
213
- return merge!(other_hash) do |key, oldval, newval|
214
- if oldval.is_a?(Hash)
215
- oldval.rmerge!(newval, concat_if_array)
216
- elsif oldval.is_a?(Array) and newval.is_a?(Array)
217
- concat_if_array ? oldval + newval : newval
218
- else
219
- newval
220
- end
221
- end
222
- end
223
- end
224
-
225
- class Hash
226
- include HashRecursiveMerge
227
- include HashRecursiveBlank
228
-
229
- def diff(other)
230
- (self.keys + other.keys).uniq.inject({}) do |memo, key|
231
- unless self[key] == other[key]
232
- if self[key].kind_of?(Hash) && other[key].kind_of?(Hash)
233
- memo[key] = self[key].diff(other[key])
234
- else
235
- memo[key] = [self[key], other[key]]
236
- end
237
- end
238
- memo
239
- end
240
- end
241
- end
242
-
243
-
244
- class Hash
245
-
246
- # ensures nested hash from keys, and sets final key to value
247
- # keys: Array of Symbol|String
248
- # value: any
249
- def nested_set(keys, value)
250
- raise "DEBUG: nested_set keys must be an Array" unless keys.is_a?(Array)
251
-
252
- final_key = keys.pop
253
- return unless valid_key?(final_key)
254
- position = self
255
- for key in keys
256
- return unless valid_key?(key)
257
- position[key] = {} unless position[key].is_a?(Hash)
258
- position = position[key]
259
- end
260
- position[final_key] = value
261
- end
262
-
263
- private
264
-
265
- # returns true if key is valid
266
- def valid_key?(key)
267
- return true if key.is_a?(Symbol) || key.is_a?(String)
268
- raise "DEBUG: nested_set invalid key: #{key} (#{key.class})"
269
- end
270
-
271
- end
272
-
273
-
274
- class String
275
-
276
- # Self test nil String class.
277
- #
278
- # @return boolean
279
- def nil?
280
- return '' if self == nil
281
- end
282
-
283
-
284
- # Justity relative left or right position filled with a espefific char in String.
285
- #
286
- # sample:
287
- #
288
- # "TESTE".fix(10,'xy') # => xxxxxTESTE
289
- # "TESTE".fix(-10,'xy') # => TESTExxxxx
290
- #
291
- # @param size to justify.
292
- # @param pattern pattern do justify
293
- # @return formated string
294
- def fix(size, pattern=' ')
295
- if size >= 0
296
- self[0...size].rjust(size, pattern)
297
- else
298
- diff = size.abs - self.size
299
- self + ''.fix(diff,pattern)
300
- end
301
- end
302
-
303
- # Encrypt a string using a key.
304
- # sample
305
- # msg = "teste do encrypt".light_blue
306
- # passwd = 'tools999'
307
- # encrypted = msg.encrypt passwd
308
- # puts (encrypted.decrypt passwd)
309
- # @return encrypt string
310
- def encrypt(key)
311
- Encrypt.dump self, key
312
- end
313
-
314
- # Decrypt a string using a key.
315
- # sample
316
- # msg = "teste do encrypt".light_blue
317
- # passwd = 'tools999'
318
- # encrypted = msg.encrypt passwd
319
- # puts (encrypted.decrypt passwd)
320
- # @return decrypt string
321
- def decrypt(key)
322
- Encrypt.load self, key
323
- end
324
-
325
- # Self test numeric String class.
326
- #
327
- # @return boolean
328
- def numeric?
329
- Float(aux) != nil rescue false
330
- end
331
-
332
- # Self test digits String class.
333
- #
334
- # @return boolean
335
- def num?
336
- !!match(/^[[:digit:]]+$/)
337
- end
338
-
339
- # Self test alphanum String class.
340
- #
341
- # @return boolean
342
- def alnum?
343
- !!match(/^[[:alnum:]]+$/)
344
- end
345
-
346
- # Self test alpha String class.
347
- #
348
- # @return boolean
349
- def alpha?
350
- !!match(/^[[:alpha:]]+$/)
351
- end
352
-
353
- def help?
354
- if self.eql? '?' or
355
- self.eql? '-h' or
356
- self.eql? '--help' or
357
- self.eql? 'help'
358
- return true
359
- else
360
- return false
361
- end
362
- end
363
-
364
- end
365
-
366
-
367
- class Object
368
- # Self test Boolean class.
369
- #
370
- # @return boolean
371
- def boolean?
372
- self.is_a?(TrueClass) || self.is_a?(FalseClass)
373
- end
374
-
375
- # Self test Trueclass.
376
- #
377
- # @return boolean
378
- def true?
379
- self.is_a?(TrueClass)
380
- end
381
- # Self test Falseclass.
382
- #
383
- # @return boolean
384
- def false?
385
- self.is_a?(FalseClass)
386
- end
387
-
388
- # Self test Symbol class.
389
- #
390
- # @return boolean
391
- def symbol?
392
- self.is_a?(Symbol)
393
- end
394
- # Self test String class.
395
- #
396
- # @return boolean
397
- def string?
398
- self.is_a?(String)
399
- end
400
-
401
- # Self test nil Object class.
402
- #
403
- # @return boolean
404
- # def nil?
405
- # return '' if self == nil
406
- # end
407
-
408
- end
409
-
410
- class Array
411
-
412
- # Self pop first element.
413
- # @return first element
414
- def extract_first
415
- first = self[0]
416
- self.delete_at(0)
417
- return first
418
- end
419
-
420
- # Self extract symbol.
421
- # @param symbol to retrive
422
- # @return boolean
423
- def extract_symbol symbol
424
- status = false
425
- if self.include? symbol
426
- status = true
427
- self.delete(symbol)
428
- end
429
- return status
430
- end
431
-
432
- # Self extract color.
433
- # @return boolean
434
- def extract_color
435
- colors = String.colors
436
- color = :default
437
- self.each do |argument|
438
- if argument.symbol?
439
- if colors.include? argument
440
- color = argument
441
- self.delete(argument)
442
- return color
443
- end
444
- else
445
- if argument.string?
446
- if argument.start_with? ':'
447
- color_candidate = argument.gsub(':','').to_sym
448
- color = color_candidate if colors.include? color_candidate
449
- self.delete(argument)
450
- return color
451
- end
452
- end
453
- end
454
- end
455
- return color
456
- end
457
-
458
-
459
-
460
-
461
- # Self extract option.
462
- # @param option to extract.
463
- # @param single boolean to repeat
464
- # @return variable boolean, arguments - extract_option
465
- #
466
- # Sample
467
- # args = [xxx -x -vvv -c -vcv -v2 -vvvvv -s :json :yellow :red]
468
- # args.extract_option '-x' => true
469
- # args.extract_option '-f' => false
470
- # args.extract_option '-v', false => 8
471
- # args.extract_symbol :json => true
472
- # args.extract_symbol :yaml => false
473
- # args.extract_color => :yellow
474
- # args.extract_color => red
475
- # args => [-c -vcv -v2 -s ]
476
- #
477
- def extract_option option, single = true
478
- if single
479
- result = false
480
- if self.include? option
481
- index = self.index(option)
482
- self.delete_at(index)
483
- result = true
484
- end
485
- else
486
- result = 0
487
- self.each do |argument|
488
- if argument.start_with? option
489
- #puts "'#{option}' '#{argument}' #{argument.start_with? option} \n"
490
- search_char = option.sub('-','')
491
- aux_string = argument.sub('-','')
492
- count = argument.count(search_char)
493
- if (count == aux_string.size)
494
- result = result + count
495
- #self.delete(argument)
496
- end
497
- end
498
-
499
- end
500
- end
501
- return result
502
- end
503
-
504
- def extract_option_value option, args={}
505
-
506
- if args[:multiple].nil?
507
- args[:multiple] = false
508
- end
509
-
510
- if args[:separator].nil?
511
- args[:separator] = '='
512
- end
513
-
514
- result = false
515
- if args[:multiple]
516
- multiple_value = []
517
- while self.include? option
518
- index = self.index(option)
519
- self.delete_at(index)
520
- result = true
521
- value = self.at(index)
522
- multiple_value << self.at(index).split(args[:separator]).first
523
- multiple_value << self.at(index).split(args[:separator]).last
524
- self.delete_at(index)
525
- end
526
- return [result, multiple_value]
527
- else
528
- value = nil
529
- while self.include? option
530
- index = self.index(option)
531
- self.delete_at(index)
532
- result = true
533
- value = self.at(index)
534
- self.delete_at(index)
535
- end
536
- return [result, value]
537
- end
538
- end
539
-
540
147
  end