toolrack 0.22.1 → 0.23.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2bf5660073953ff0e581655963232f58762e39d46ccd1383885958da071d76e6
4
- data.tar.gz: 6f94d5fe9de1c054d386639e192a60a0d075c34daf46c477936a4fccb7c52a56
3
+ metadata.gz: 4ba701f80c017de64b440dfbd5388aef7ecf9e08200cf7fe4413a1e495ba5d27
4
+ data.tar.gz: 3d043a9e921cbec66a9c3fb760a10187cea1dcc0b25c990b66825b91eb051b43
5
5
  SHA512:
6
- metadata.gz: f218994963cbdf44dc154b02ebdf4fa011c0f4f2819ab96143d8e14e383aa8688cf775a3db67896110020157253a33ee419e56a6be2649e76c169813af3ce1c4
7
- data.tar.gz: 6acabb8fd0426fde3acaa42b3e1f55c27402480e6dad210ee21ff0f5a9426e07a377b23aeb005da851788a6bec7a1eb057253947d4db2dec76df3121d58c8896
6
+ metadata.gz: 56664034a546c17cfc0f58e6c4c220da5de164c4513d240ed2152c5cd1b3291f05ab1d662a6c1c1f8f53fa5a7a1e82fac105722cecc96f3bc773f58be04ec779
7
+ data.tar.gz: 439aaa664c77d76a334d6730bba5fa44a23aae15b0ca00fa8b3d41da93b06b5affc64fed2cd7acb53207ddb3cbe306cee2f5db92dc6ac9fd25d212ebdb0cd069
data/Gemfile.lock CHANGED
@@ -1,39 +1,25 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- toolrack (0.22.1)
4
+ toolrack (0.23.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- colorize (0.8.1)
10
- devops_assist (0.3.11)
11
- git_cli
12
- git_cli_prompt (~> 0.3.3)
13
- gvcs
14
- teLogger
15
- toolrack
16
- tty-prompt
9
+ colorize (1.1.0)
17
10
  diff-lcs (1.5.0)
18
- git_cli (0.11.2)
19
- gvcs
11
+ git_cli (0.13.6)
20
12
  ptools (~> 1.4.0)
21
- teLogger
22
- toolrack
23
- git_cli_prompt (0.3.4)
24
- teLogger
25
- toolrack
26
- tty-prompt
27
- gvcs (0.1.1)
28
- minitest (5.18.0)
13
+ teLogger (> 0.2)
14
+ toolrack (> 0.23)
15
+ minitest (5.20.0)
29
16
  pastel (0.8.0)
30
17
  tty-color (~> 0.5)
31
18
  ptools (1.4.3)
32
19
  rake (12.3.3)
33
- release-gem (0.1.12)
20
+ release-gem (0.1.30)
34
21
  colorize
35
22
  git_cli
36
- gvcs
37
23
  teLogger
38
24
  toolrack
39
25
  tty-command
@@ -42,15 +28,15 @@ GEM
42
28
  rspec-core (~> 3.12.0)
43
29
  rspec-expectations (~> 3.12.0)
44
30
  rspec-mocks (~> 3.12.0)
45
- rspec-core (3.12.1)
31
+ rspec-core (3.12.2)
46
32
  rspec-support (~> 3.12.0)
47
- rspec-expectations (3.12.2)
33
+ rspec-expectations (3.12.3)
48
34
  diff-lcs (>= 1.2.0, < 2.0)
49
35
  rspec-support (~> 3.12.0)
50
- rspec-mocks (3.12.5)
36
+ rspec-mocks (3.12.6)
51
37
  diff-lcs (>= 1.2.0, < 2.0)
52
38
  rspec-support (~> 3.12.0)
53
- rspec-support (3.12.0)
39
+ rspec-support (3.12.1)
54
40
  teLogger (0.2.2)
55
41
  tty-color (0.6.0)
56
42
  tty-command (0.10.1)
@@ -70,7 +56,6 @@ PLATFORMS
70
56
  x86_64-linux
71
57
 
72
58
  DEPENDENCIES
73
- devops_assist
74
59
  minitest (~> 5.0)
75
60
  rake (~> 12.0)
76
61
  release-gem
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rake/testtask"
3
3
 
4
- require 'devops_assist'
4
+ #require 'devops_assist'
5
5
 
6
6
  Rake::TestTask.new(:test) do |t|
7
7
  t.libs << "test"
@@ -0,0 +1,254 @@
1
+
2
+
3
+ module Antrapol
4
+ module ToolRack
5
+
6
+ module ArgUtils
7
+ include Antrapol::ToolRack::ConditionUtils
8
+
9
+ class ArgParsingException < StandardError; end
10
+ class RequiredFieldEmpty < ArgParsingException; end
11
+ class DuplicatedField < ArgParsingException; end
12
+ class InvalidKey < ArgParsingException; end
13
+
14
+ module ClassMethods
15
+ include Antrapol::ToolRack::ConditionUtils
16
+
17
+ def arg_spec(&block)
18
+ class_eval(&block)
19
+ end
20
+
21
+ def opt(key, desc, opts = {}, &block)
22
+ raise RequiredFieldEmpty, "Key field cannot be empty" if is_empty?(key)
23
+ raise DuplicatedField, "Given key '#{key}' already exist" if arg_options.keys.include?(key)
24
+ raise RequiredFieldEmpty, "Block is require" if not block
25
+
26
+ arg_options[key] = { key: key, desc: desc, arg_options: opts, callback: block }
27
+ required << key if opts[:required] == true
28
+ end
29
+
30
+ def opt_alias(existing, new)
31
+ raise InvalidKey, "Existing key '#{existing}'to map to new key '#{new}' not found" if not arg_options.keys.include?(existing)
32
+ aliases[new] = existing
33
+ end
34
+
35
+ def is_all_required_satisfy?(arr)
36
+ if arr.is_a?(Array)
37
+ res = true
38
+ required.each do |f|
39
+ res = arr.include?(f)
40
+ if not res
41
+ al = aliases.invert[f]
42
+ res = arr.include?(al)
43
+ raise RequiredFieldEmpty, "Required parameter '#{f}' is not given" if not res
44
+ end
45
+ end
46
+ res
47
+
48
+ else
49
+ raise ArgParsingException, "Given array to check for required fields is not an array. Got #{arr}"
50
+ if arr.is_a?(ArgParsingException)
51
+ STDERR.puts "Error throwing into the method was : #{arr.message}"
52
+ end
53
+
54
+ end
55
+ end
56
+
57
+ def callback(evt, opts = {}, &block)
58
+ raise ArgParsingException, "Event should not be nil" if evt.nil?
59
+ callbacks[evt] = { opts: opts, cb: block }
60
+ end
61
+
62
+ def required
63
+ if @_req.nil?
64
+ @_req = []
65
+ end
66
+ @_req
67
+ end
68
+
69
+ def arg_options
70
+ if @_arg_options.nil?
71
+ @_arg_options = {}
72
+ end
73
+ @_arg_options
74
+ end
75
+
76
+ def aliases
77
+ if @_aliases.nil?
78
+ @_aliases = {}
79
+ end
80
+ @_aliases
81
+ end
82
+
83
+ def callbacks
84
+ if @_cb.nil?
85
+ @_cb = {}
86
+ end
87
+ @_cb
88
+ end
89
+
90
+ #def value_separator=(val)
91
+ # logger.debug "Setting value separator #{val}"
92
+ # @_valSep = val
93
+ #end
94
+ def set_value_separator(val)
95
+ @_valSep = val
96
+ end
97
+
98
+ def value_separator
99
+ if @_valSep.nil?
100
+ @_valSep = " "
101
+ end
102
+ @_valSep
103
+ end
104
+
105
+ def logger
106
+ Antrapol::ToolRack.logger(:c_arg_utils)
107
+ end
108
+
109
+ end # module ClassMethods
110
+ def self.included(klass)
111
+ klass.extend(ClassMethods)
112
+ end
113
+
114
+ def parse_argv(argv, &block)
115
+ cb = self.class.callbacks[:pre_processing]
116
+ if not_empty?(cb) and cb[:cb]
117
+ #logger.debug "Calling pre-processing for class #{self}"
118
+ @parse_argv_block = block
119
+ # here will engage the DSL method of the included class
120
+ update_argv, val = instance_exec(argv, &cb[:cb])
121
+ #logger.debug "Preprocessing return update flag : #{update_argv} and list #{val} for class #{self}"
122
+ argv = val if update_argv
123
+ end
124
+
125
+ # split the key and value if given
126
+ # > app -n:john
127
+ if self.class.value_separator == " " and argv.length > 0
128
+ parse_argv_space(argv)
129
+ else
130
+ parse_argv_key_value_mixed(argv)
131
+ end
132
+
133
+ cbp = self.class.callbacks[:post_processing]
134
+ if not_empty?(cbp) and cbp[:cb]
135
+ #logger.debug "Post processing got #{argv}"
136
+ instance_exec(argv, &cbp[:cb])
137
+ end
138
+ end
139
+
140
+ private
141
+ def parse_argv_space(argv)
142
+ logger.debug "got argv (space) : #{argv}"
143
+ cls = self.class
144
+ clear = cls.is_all_required_satisfy?(argv)
145
+ logger.debug "All required fields are there? : #{clear}"
146
+ if clear
147
+
148
+ go = true
149
+ i = 0
150
+ while i < argv.length
151
+ a = argv[i]
152
+ logger.debug "Processing : #{a}"
153
+
154
+ key = a
155
+ conf = cls.arg_options[key]
156
+ if is_empty?(conf)
157
+ al = cls.aliases[key]
158
+ conf = cls.arg_options[al] if not_empty?(al)
159
+ end
160
+
161
+ if is_empty?(conf)
162
+ logger.warn "Given key to select ('#{key}') has not defined before"
163
+ # ignore if the given parameter not recognized
164
+ i += 1
165
+ next
166
+ end
167
+
168
+ if not conf[:callback].nil?
169
+ # expecting parameter
170
+ paramsCount = conf[:callback].arity
171
+ if paramsCount > 0
172
+ # look ahead token to load the parameters
173
+ val = []
174
+ (0...paramsCount).each do |ii|
175
+ val << argv[i+1+ii]
176
+ end
177
+ val.delete_if { |e| e.nil? }
178
+ logger.debug "Parameter for callback : #{val}"
179
+ raise RequiredFieldEmpty, "Key '#{a}' requires #{paramsCount} parameter(s) but got #{val.length}" if paramsCount != val.length
180
+ instance_exec(*val, &conf[:callback])
181
+ i += paramsCount+1
182
+ else
183
+ instance_eval(&conf[:callback])
184
+ i += 1
185
+ end
186
+
187
+ else
188
+ i += 1
189
+
190
+ end
191
+
192
+ end # argv.each
193
+
194
+ end # if all required satisfy
195
+
196
+ end
197
+
198
+ def parse_argv_key_value_mixed(argv)
199
+ logger.debug "got argv : #{argv}"
200
+ cls = self.class
201
+ if cls.is_all_required_satisfy?(argv)
202
+
203
+ argv.each do |a|
204
+ logger.debug "Processing : #{a}"
205
+
206
+ key = a
207
+ val = []
208
+ #p a
209
+ #p cls.value_separator
210
+ #p a =~ /#{cls.value_separator}/
211
+ if (a =~ /#{cls.value_separator}/) != nil
212
+ keys = a.split(cls.value_separator)
213
+ key = keys.first
214
+ val = keys[1..-1]
215
+ end
216
+
217
+ logger.debug "After separation : #{key}"
218
+
219
+ conf = cls.arg_options[key]
220
+ if is_empty?(conf)
221
+ al = cls.aliases[key]
222
+ conf = cls.arg_options[al] if not_empty?(al)
223
+ end
224
+
225
+ if is_empty?(conf)
226
+ logger.warn "Given key to select ('#{key}') has not defined before"
227
+ # ignore if the given parameter not recognized
228
+ next
229
+ end
230
+
231
+ if not conf[:callback].nil?
232
+ # expecting parameter
233
+ paramsCount = conf[:callback].arity
234
+ if paramsCount > 0
235
+ raise RequiredFieldEmpty, "Key '#{conf[:key]}' requires #{paramsCount} value(s) to be given but got #{val.length}." if val.length != paramsCount
236
+ instance_exec(*val, &conf[:callback])
237
+ else
238
+ instance_eval(&conf[:callback])
239
+ end
240
+ end
241
+
242
+ end # argv.each
243
+
244
+ end # if all required satisfy
245
+ end
246
+
247
+ def logger
248
+ Antrapol::ToolRack.logger(:arg_utils)
249
+ end
250
+
251
+ end
252
+
253
+ end
254
+ end
@@ -97,6 +97,37 @@ module Antrapol
97
97
  end
98
98
  end
99
99
 
100
+ def hex_to_bin(hex)
101
+ if not_empty?(hex)
102
+ # [abcd1234].pack('H*')
103
+ # turn hex to binary
104
+ case hex
105
+ when Array
106
+ hex.pack('H*')
107
+ else
108
+ hex.split.pack('H*')
109
+ end
110
+ else
111
+ hex
112
+ end
113
+ end
114
+
115
+ def bin_to_int(bin)
116
+ if not_empty?(bin)
117
+ bin.bytes.inject { |a,b| (a << 8) + b }
118
+ else
119
+ bin
120
+ end
121
+ end
122
+
123
+ def int_to_bin(intStr)
124
+ if not_empty?(intStr)
125
+ hex_to_bin(intStr.to_i.to_s(16))
126
+ else
127
+ intStr
128
+ end
129
+ end
130
+
100
131
  def string_to_bool(str)
101
132
  if not_empty?(str) and is_str_bool?(str)
102
133
  s = str.to_s.strip.downcase
@@ -1,6 +1,6 @@
1
1
  module Antrapol
2
2
  module ToolRack
3
- VERSION = "0.22.1"
3
+ VERSION = "0.23.3"
4
4
  end
5
5
  end
6
6
 
data/lib/toolrack.rb CHANGED
@@ -19,11 +19,45 @@ require_relative 'toolrack/version_utils'
19
19
  require_relative 'toolrack/file_utils'
20
20
  require_relative 'toolrack/block_params_utils'
21
21
  require_relative 'toolrack/terminal_utils'
22
+ require_relative 'toolrack/arg_utils'
23
+
22
24
 
23
25
  module Antrapol
24
26
  module ToolRack
27
+ include ConditionUtils
28
+
25
29
  class Error < StandardError; end
26
30
  # Your code goes here...
31
+ #
32
+ def self.logger(tag = nil, &block)
33
+ if @_logger.nil?
34
+ trLogout = ENV["TR_LOGOUT"]
35
+ if not_empty?(trLogout)
36
+ @_logger = TeLogger::Tlogger.new(trLogout, 5, 5*1024*1024)
37
+ else
38
+ @_logger = TeLogger::Tlogger.new('toolrack.log', 5, 5*1024*1024)
39
+ end
40
+ end
41
+
42
+ if block
43
+ if not_empty?(tag)
44
+ @_logger.with_tag(tag, &block)
45
+ else
46
+ @_logger.with_tag(@_logger.tag, &block)
47
+ end
48
+ else
49
+ if is_empty?(tag)
50
+ @_logger.tag = :tr
51
+ @_logger
52
+ else
53
+ # no block but tag is given? hmm
54
+ @_logger.tag = tag
55
+ @_logger
56
+ end
57
+ end
58
+
59
+ end
60
+
27
61
  end
28
62
  end
29
63
 
@@ -38,6 +72,7 @@ ToolRack = Antrapol::ToolRack
38
72
 
39
73
  ToolRack::DataConvUtils = Antrapol::ToolRack::DataConversionUtils
40
74
  TR::DataConvUtils = ToolRack::DataConvUtils
75
+ TR::DCUtils = TR::DataConvUtils
41
76
 
42
77
  ToolRack::CondUtils = ToolRack::ConditionUtils
43
78
  TR::CondUtils = ToolRack::ConditionUtils
@@ -68,3 +103,5 @@ TR::BlockParamsUtils = ToolRack::BlockParamsUtils
68
103
 
69
104
  TR::TerminalUtils = ToolRack::TerminalUtils
70
105
 
106
+ TR::ArgUtils = ToolRack::ArgUtils
107
+
data/toolrack.gemspec CHANGED
@@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
30
30
  #spec.add_dependency "base58"
31
31
 
32
32
  #spec.add_development_dependency "devops_helper" #, "~> 0.1.0"
33
- spec.add_development_dependency "devops_assist"
33
+ #spec.add_development_dependency "devops_assist"
34
34
  spec.add_development_dependency "rspec"
35
35
  spec.add_development_dependency "release-gem"
36
36
 
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toolrack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.22.1
4
+ version: 0.23.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-06 00:00:00.000000000 Z
11
+ date: 2023-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: devops_assist
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rspec
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -72,6 +58,7 @@ files:
72
58
  - bin/console
73
59
  - bin/setup
74
60
  - lib/toolrack.rb
61
+ - lib/toolrack/arg_utils.rb
75
62
  - lib/toolrack/block_params_utils.rb
76
63
  - lib/toolrack/cli_utils.rb
77
64
  - lib/toolrack/condition_utils.rb
@@ -98,7 +85,7 @@ licenses: []
98
85
  metadata:
99
86
  homepage_uri: https://github.com/chrisliaw/toolrack
100
87
  source_code_uri: https://github.com/chrisliaw/toolrack
101
- post_install_message:
88
+ post_install_message:
102
89
  rdoc_options: []
103
90
  require_paths:
104
91
  - lib
@@ -113,8 +100,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
100
  - !ruby/object:Gem::Version
114
101
  version: '0'
115
102
  requirements: []
116
- rubygems_version: 3.4.10
117
- signing_key:
103
+ rubygems_version: 3.4.6
104
+ signing_key:
118
105
  specification_version: 4
119
106
  summary: Collection of simple utilities but I find it increase clarity
120
107
  test_files: []