case_parser 1.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/case_parser.gemspec +52 -0
- data/lib/case_parser.rb +1 -0
- data/lib/case_parser/case_parser.rb +291 -0
- data/lib/case_parser/class_methods.rb +24 -0
- data/lib/case_parser/version/version.rb +12 -0
- data/test/testing_case_parser.rb +20 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4eb3ac4e3e0ab334bd4467876c3b3bcb7863b4ebac5c32628664b2ef21adcb89
|
4
|
+
data.tar.gz: 274280302860ccfe7f44f36da4c503ed5d2479091b017ea643d57f1fed27ff48
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4effea98ea2b44eae22d93898e5b9c5e268c813b70deb595fb65a493069602a59cf9260f8c6b346e298c8a6b9c221e651d7a9ac42b7fb1b7b36fefdeacd143a8
|
7
|
+
data.tar.gz: 3ed735cb4369bab8ef903c3808f0c93baf5b8f7bfcc1d2c6fdc6a777df21193229cc89d4698f10a71c3472c30a733d9c8c8c4062006baf4fad91a9659dd5e0b0
|
data/case_parser.gemspec
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# =========================================================================== #
|
2
|
+
# Gemspec for Project CaseParser.
|
3
|
+
# =========================================================================== #
|
4
|
+
require 'case_parser'
|
5
|
+
|
6
|
+
Gem::Specification.new { |s|
|
7
|
+
|
8
|
+
s.name = 'case_parser'
|
9
|
+
s.version = CaseParser::VERSION
|
10
|
+
s.date = Time.now.strftime('%Y-%m-%d')
|
11
|
+
|
12
|
+
s.summary = <<-EOF
|
13
|
+
|
14
|
+
It will try to parse a case/when menu from a given
|
15
|
+
.rb file and then give back an Array of all entries
|
16
|
+
found after the first occurance of when.
|
17
|
+
|
18
|
+
If you have specific suggestions to make this gem more
|
19
|
+
useful for others, please drop me an email at:
|
20
|
+
|
21
|
+
shevegen@gmail.com
|
22
|
+
|
23
|
+
Thank you.
|
24
|
+
|
25
|
+
EOF
|
26
|
+
|
27
|
+
s.description = <<-EOF
|
28
|
+
|
29
|
+
This library is called case_parser.
|
30
|
+
|
31
|
+
It will try to parse a case/when menu from a given
|
32
|
+
.rb file and then give back an Array of all entries
|
33
|
+
found after the first occurance of when.
|
34
|
+
|
35
|
+
EOF
|
36
|
+
|
37
|
+
s.extra_rdoc_files = %w()
|
38
|
+
|
39
|
+
s.authors = ['Robert A. Heiler']
|
40
|
+
s.email = 'shevegen@gmail.com'
|
41
|
+
s.files = Dir.glob('**/*')
|
42
|
+
s.license = 'GPL-2.0'
|
43
|
+
s.homepage = 'http://rubygems.org/gems/case_parser'
|
44
|
+
|
45
|
+
s.required_ruby_version = '>= '+RUBY_VERSION
|
46
|
+
s.required_rubygems_version = '>= '+Gem::VERSION
|
47
|
+
s.rubygems_version = '>= '+Gem::VERSION
|
48
|
+
|
49
|
+
s.add_dependency 'colours'
|
50
|
+
s.add_dependency 'opn'
|
51
|
+
|
52
|
+
}
|
data/lib/case_parser.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'case_parser/class_methods.rb'
|
@@ -0,0 +1,291 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
# === CaseParser
|
6
|
+
#
|
7
|
+
# This class will scan a file and extract all case entries.
|
8
|
+
#
|
9
|
+
# For now though, we will work only on the first case menu.
|
10
|
+
# =========================================================================== #
|
11
|
+
# require 'case_parser.rb'
|
12
|
+
# =========================================================================== #
|
13
|
+
require 'case_parser/version/version.rb'
|
14
|
+
|
15
|
+
class CaseParser # CaseParser.new
|
16
|
+
|
17
|
+
begin
|
18
|
+
require 'opn'
|
19
|
+
rescue LoadError; end
|
20
|
+
|
21
|
+
begin
|
22
|
+
require 'convert_global_env'
|
23
|
+
rescue LoadError; end
|
24
|
+
|
25
|
+
begin
|
26
|
+
require 'colours'
|
27
|
+
include Colours
|
28
|
+
rescue LoadError; end
|
29
|
+
|
30
|
+
# ========================================================================= #
|
31
|
+
# === USE_THIS_ENCODING
|
32
|
+
# ========================================================================= #
|
33
|
+
USE_THIS_ENCODING = 'UTF-8' # 'ISO-8859-1'
|
34
|
+
|
35
|
+
# ========================================================================= #
|
36
|
+
# Test it via compile.rb.
|
37
|
+
# ========================================================================= #
|
38
|
+
DEFAULT_FILE =
|
39
|
+
"#{ENV['RBT']}/compile.rb"
|
40
|
+
# DEFAULT_FILE = '/Depot/jjj/testcase.rb'
|
41
|
+
|
42
|
+
# ========================================================================== #
|
43
|
+
# === SHALL_WE_DEBUG
|
44
|
+
# ========================================================================== #
|
45
|
+
SHALL_WE_DEBUG = false
|
46
|
+
|
47
|
+
# ========================================================================== #
|
48
|
+
# === initialize
|
49
|
+
# ========================================================================== #
|
50
|
+
def initialize(
|
51
|
+
i = DEFAULT_FILE,
|
52
|
+
run_already = true
|
53
|
+
)
|
54
|
+
reset
|
55
|
+
case run_already
|
56
|
+
when :continue,
|
57
|
+
:do_not_exit
|
58
|
+
@may_we_exit_on_missing_file = false
|
59
|
+
run_already = true
|
60
|
+
end
|
61
|
+
run(i) if run_already
|
62
|
+
end
|
63
|
+
|
64
|
+
# ========================================================================== #
|
65
|
+
# === set_file
|
66
|
+
#
|
67
|
+
# Simply set the @file variable here.
|
68
|
+
# ========================================================================== #
|
69
|
+
def set_file(i)
|
70
|
+
i = DEFAULT_FILE if i.nil?
|
71
|
+
case i
|
72
|
+
# ======================================================================== #
|
73
|
+
# === :beautiful_menu
|
74
|
+
# ======================================================================== #
|
75
|
+
when :beautiful_menu
|
76
|
+
i = ConvertGlobalEnv[
|
77
|
+
'$RUBY_SRC/beautiful_url/lib/beautiful_url/toplevel_methods/menu.rb'
|
78
|
+
] # Hardcoded.
|
79
|
+
unless File.exist? i
|
80
|
+
opn; e 'Warning - no file exists at `'+sfile(i)+'`.'
|
81
|
+
end
|
82
|
+
when :dia_menu
|
83
|
+
i = ConvertGlobalEnv['$RUBY_SRC/diamond_shell/lib/diamond_shell/menu/menu.rb']
|
84
|
+
unless File.exist? i
|
85
|
+
opn; e 'Warning - no file exists at `'+sfile(i)+'`.'
|
86
|
+
end
|
87
|
+
end
|
88
|
+
i = i.to_s unless i.is_a? String
|
89
|
+
i = ConvertGlobalEnv.convert(i) if i.include? '$'
|
90
|
+
@file = i
|
91
|
+
end
|
92
|
+
|
93
|
+
# ========================================================================== #
|
94
|
+
# === reset
|
95
|
+
# ========================================================================== #
|
96
|
+
def reset
|
97
|
+
# ======================================================================== #
|
98
|
+
# === @dataset
|
99
|
+
# ======================================================================== #
|
100
|
+
@dataset = nil
|
101
|
+
# ======================================================================== #
|
102
|
+
# === @seen_case
|
103
|
+
#
|
104
|
+
# Whether we saw "case" yet or did not.
|
105
|
+
# ======================================================================== #
|
106
|
+
@seen_case = false
|
107
|
+
# ======================================================================== #
|
108
|
+
# === @array_keeping_all_when_entries
|
109
|
+
# ======================================================================== #
|
110
|
+
@array_keeping_all_when_entries = []
|
111
|
+
# ======================================================================== #
|
112
|
+
# === @shall_we_debug
|
113
|
+
#
|
114
|
+
# If true then we will debug.
|
115
|
+
# ======================================================================== #
|
116
|
+
@shall_we_debug = SHALL_WE_DEBUG
|
117
|
+
# ======================================================================== #
|
118
|
+
# === @may_we_exit_on_missing_file
|
119
|
+
# ======================================================================== #
|
120
|
+
@may_we_exit_on_missing_file = true
|
121
|
+
end
|
122
|
+
|
123
|
+
# ========================================================================== #
|
124
|
+
# === feedback
|
125
|
+
# ========================================================================== #
|
126
|
+
def feedback
|
127
|
+
opn; pp results
|
128
|
+
end
|
129
|
+
|
130
|
+
# ========================================================================== #
|
131
|
+
# === sanitize_array
|
132
|
+
#
|
133
|
+
# This will try to sanitize the array.
|
134
|
+
# ========================================================================== #
|
135
|
+
def sanitize_array
|
136
|
+
@array_keeping_all_when_entries.flatten!
|
137
|
+
begin # Musct rescue this in case the encoding is bad.
|
138
|
+
@array_keeping_all_when_entries.map! {|entry|
|
139
|
+
entry.delete("'").strip
|
140
|
+
}
|
141
|
+
rescue; end
|
142
|
+
# ======================================================================== #
|
143
|
+
# Now, we remove all Regexes from this menu, because Regexes
|
144
|
+
# are not really useful to keep for tab completion in most
|
145
|
+
# scripts.
|
146
|
+
# ======================================================================== #
|
147
|
+
@array_keeping_all_when_entries.reject! {|entry|
|
148
|
+
entry.start_with? '/' or
|
149
|
+
entry.start_with? ':' # Symbols also get rejected.
|
150
|
+
}
|
151
|
+
end
|
152
|
+
|
153
|
+
# ========================================================================== #
|
154
|
+
# === remove_potential_comments
|
155
|
+
#
|
156
|
+
# This gets rid of potential '#' comments.
|
157
|
+
# ========================================================================== #
|
158
|
+
def remove_potential_comments(i)
|
159
|
+
i = i[0, i.index('#')].strip if i.include? '#' # If there is a #, return all up to that.
|
160
|
+
return i
|
161
|
+
end
|
162
|
+
|
163
|
+
# ========================================================================== #
|
164
|
+
# === red
|
165
|
+
# ========================================================================== #
|
166
|
+
def red
|
167
|
+
Colours::RED
|
168
|
+
end
|
169
|
+
|
170
|
+
# ========================================================================== #
|
171
|
+
# === read_file
|
172
|
+
# ========================================================================== #
|
173
|
+
def read_file
|
174
|
+
if File.exist? @file
|
175
|
+
@dataset = File.readlines(@file, encoding: USE_THIS_ENCODING)
|
176
|
+
else
|
177
|
+
if @may_we_exit_on_missing_file
|
178
|
+
opn; e red+'Can not continue. File `'+sfile(@file)+
|
179
|
+
red+'` does not exist.'+rev
|
180
|
+
exit
|
181
|
+
end
|
182
|
+
end
|
183
|
+
if @shall_we_debug
|
184
|
+
opn; pp dataset?
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
# ========================================================================== #
|
189
|
+
# === report_n_entries
|
190
|
+
# ========================================================================== #
|
191
|
+
def report_n_entries
|
192
|
+
opn; e "We found #{simp(dataset?.size.to_s)} entries."
|
193
|
+
end
|
194
|
+
|
195
|
+
# ========================================================================== #
|
196
|
+
# === results
|
197
|
+
# ========================================================================== #
|
198
|
+
def results
|
199
|
+
_ = @array_keeping_all_when_entries
|
200
|
+
_.sort! if _
|
201
|
+
_
|
202
|
+
end
|
203
|
+
|
204
|
+
# ========================================================================== #
|
205
|
+
# === extract_first_when
|
206
|
+
#
|
207
|
+
# Use this method here if you wish to extract the first word.
|
208
|
+
# ========================================================================== #
|
209
|
+
def extract_first_when(
|
210
|
+
mode = :extract_only_first_entry
|
211
|
+
)
|
212
|
+
dataset?.each { |d|
|
213
|
+
if d.include? 'when' # Work on when-entries only. This is not perfect, as we miss other entries.
|
214
|
+
begin
|
215
|
+
_ = d.chomp.strip.gsub(/when /,'') # Eliminate 'when ' here.
|
216
|
+
rescue ArgumentError
|
217
|
+
opn; e 'ArgumentError - invalid byte sequence in US-ASCII'
|
218
|
+
end
|
219
|
+
if _
|
220
|
+
_ = _.split(',') if _.include? ',' # Split on ',' if they exist.
|
221
|
+
_ = _.delete("'") if _.include? "'" # Eliminate "'" characters.
|
222
|
+
append_to_main_array(_)
|
223
|
+
end
|
224
|
+
end
|
225
|
+
}
|
226
|
+
end
|
227
|
+
|
228
|
+
# ========================================================================== #
|
229
|
+
# === extract_case_menu
|
230
|
+
#
|
231
|
+
# This method will extract the case menu.
|
232
|
+
# ========================================================================== #
|
233
|
+
def extract_case_menu
|
234
|
+
_ = []
|
235
|
+
dataset?.each {|line|
|
236
|
+
begin
|
237
|
+
@seen_case = true if line.include? 'case'
|
238
|
+
_ << remove_potential_comments(line) if @seen_case
|
239
|
+
@seen_case = false if line =~ /end$/
|
240
|
+
rescue ArgumentError # This is here to rescue against "invalid byte sequence in US-ASCII"
|
241
|
+
end
|
242
|
+
} if dataset?
|
243
|
+
_.reject!(&:empty?)
|
244
|
+
@dataset = _
|
245
|
+
end
|
246
|
+
|
247
|
+
# ========================================================================== #
|
248
|
+
# === append_to_main_array
|
249
|
+
# ========================================================================== #
|
250
|
+
def append_to_main_array(i)
|
251
|
+
@array_keeping_all_when_entries << i
|
252
|
+
end
|
253
|
+
|
254
|
+
# ========================================================================== #
|
255
|
+
# === dataset?
|
256
|
+
# ========================================================================== #
|
257
|
+
def dataset?
|
258
|
+
@dataset
|
259
|
+
end
|
260
|
+
|
261
|
+
# ========================================================================== #
|
262
|
+
# === found_entries?
|
263
|
+
#
|
264
|
+
# We return all entries found in a sorted manner here.
|
265
|
+
# ========================================================================== #
|
266
|
+
def found_entries?
|
267
|
+
@array_keeping_all_when_entries.sort
|
268
|
+
end
|
269
|
+
|
270
|
+
# ========================================================================== #
|
271
|
+
# === run
|
272
|
+
#
|
273
|
+
# General run method. We will also set_file() in this method.
|
274
|
+
# ========================================================================== #
|
275
|
+
def run(which_file)
|
276
|
+
set_file(which_file)
|
277
|
+
read_file
|
278
|
+
extract_case_menu
|
279
|
+
extract_first_when
|
280
|
+
sanitize_array
|
281
|
+
end
|
282
|
+
|
283
|
+
end
|
284
|
+
|
285
|
+
if __FILE__ == $PROGRAM_NAME
|
286
|
+
_ = CaseParser.new(ARGV.first)
|
287
|
+
_.feedback
|
288
|
+
_.report_n_entries
|
289
|
+
end # cparse
|
290
|
+
# cparse $RUBY_SRC/beautiful_url/lib/beautiful_url/menu/beautiful_menu.rb
|
291
|
+
#
|
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
require 'case_parser/case_parser.rb'
|
6
|
+
|
7
|
+
class CaseParser
|
8
|
+
|
9
|
+
# ========================================================================== #
|
10
|
+
# === CaseParser.parse
|
11
|
+
#
|
12
|
+
# This method will return a (sorted) array.
|
13
|
+
#
|
14
|
+
# To use this, do something like:
|
15
|
+
#
|
16
|
+
# CaseParser.parse(__FILE__)
|
17
|
+
#
|
18
|
+
# ========================================================================== #
|
19
|
+
def self.parse(i, optional_run_already = :do_not_exit)
|
20
|
+
return CaseParser.new(i, optional_run_already).results
|
21
|
+
end; self.instance_eval { alias [] parse } # === CaseParser[]
|
22
|
+
self.instance_eval { alias return_array parse } # === CaseParser.return_array
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
class CaseParser
|
6
|
+
|
7
|
+
# ========================================================================= #
|
8
|
+
# === VERSION
|
9
|
+
# ========================================================================= #
|
10
|
+
VERSION = '1.0.17'
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# =========================================================================== #
|
4
|
+
require 'pp'
|
5
|
+
require 'colours/autoinclude'
|
6
|
+
require 'case_parser'
|
7
|
+
e 'We will now test class CaseParser'
|
8
|
+
menu = '$RUBY_SRC/beautiful_url/lib/beautiful_url/menu/beautiful_menu.rb'
|
9
|
+
case_parser = CaseParser.new(menu)
|
10
|
+
pp case_parser
|
11
|
+
cliner
|
12
|
+
e 'The found entries were:'
|
13
|
+
pp case_parser.found_entries?
|
14
|
+
cliner
|
15
|
+
pp CaseParser.return_array(menu)
|
16
|
+
cliner
|
17
|
+
array = CaseParser.return_array(:beautiful_menu)
|
18
|
+
e 'The array has '+sfancy(array.size.to_s)+' elements.'
|
19
|
+
array = CaseParser.return_array(:dia_menu)
|
20
|
+
e 'The array has '+sfancy(array.size.to_s)+' elements.'
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: case_parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.17
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert A. Heiler
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-12-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: colours
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: opn
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: |2+
|
42
|
+
|
43
|
+
This library is called case_parser.
|
44
|
+
|
45
|
+
It will try to parse a case/when menu from a given
|
46
|
+
.rb file and then give back an Array of all entries
|
47
|
+
found after the first occurance of when.
|
48
|
+
|
49
|
+
email: shevegen@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- case_parser.gemspec
|
55
|
+
- lib/case_parser.rb
|
56
|
+
- lib/case_parser/case_parser.rb
|
57
|
+
- lib/case_parser/class_methods.rb
|
58
|
+
- lib/case_parser/version/version.rb
|
59
|
+
- test/testing_case_parser.rb
|
60
|
+
homepage: http://rubygems.org/gems/case_parser
|
61
|
+
licenses:
|
62
|
+
- GPL-2.0
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 2.7.2
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 3.2.1
|
78
|
+
requirements: []
|
79
|
+
rubygems_version: 3.2.1
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: 'It will try to parse a case/when menu from a given .rb file and then give
|
83
|
+
back an Array of all entries found after the first occurance of when. If you have
|
84
|
+
specific suggestions to make this gem more useful for others, please drop me an
|
85
|
+
email at: shevegen@gmail.com Thank you.'
|
86
|
+
test_files: []
|