rb_cfg_parser 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 53d596560d131b757edbbb6366355fb61abbd23b3f2af43ef2cd340f3dad408d
4
+ data.tar.gz: aefe4e5a4049d4d27c401cc277d5258c6efd0a4a80d36d4986210ac15a3f0cf0
5
+ SHA512:
6
+ metadata.gz: f77a2803b3baac5592a660e851dd460c67ed3c113f2c6c8f3dc809d478bad2c7757b7e7c3eae13a2a59c39189ada5d920d767f57e7534e329804675a4552b2bb
7
+ data.tar.gz: c832cffd945b30329cee4c9ab0b98c5b280dac126d228b500d1c9adea4b8ffdaf79bb4cf25e4030d5171dc9471f3de4ad352b91808ca26b6070d0a90c3fe9b3a
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+ MIT License
2
+
3
+ Copyright (c) since 2025: rb-cfg-parser ruby App
4
+ description: ruby gem to parse and load .cfg files
5
+ author: J. Arrizza email: cppgent0@gmail.com
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ website: <https://link-here>
2
+
3
+ ## Summary
4
+
5
+ This project TODO...
6
+
7
+ * run
8
+
9
+ ```bash
10
+ ./doit
11
+ ```
12
+
13
+ Typical output:
14
+
15
+ ```bash
16
+ TODO
17
+ ```
18
+
19
+ ## How to use
20
+
21
+ TODO
22
+
@@ -0,0 +1,253 @@
1
+ # frozen_string_literal: true
2
+
3
+ ###
4
+ # Configuration parser
5
+ class RbCfgParser
6
+ ###
7
+ # constructor
8
+ def initialize
9
+ _reset_all
10
+ end
11
+
12
+ ###
13
+ # parse the given file
14
+ #
15
+ # @param path the path to the file
16
+ # @return (hash) the content
17
+ def parse_file(path)
18
+ file_content = File.read(path)
19
+ parse_str(file_content.lines)
20
+ end
21
+
22
+ ###
23
+ # parse the given array of lines
24
+ #
25
+ # @param lines (array) the lines to parse
26
+ # @return (hash) the content
27
+ def parse_str(lines)
28
+ # uncomment to debug
29
+ # puts("\nlines: >>>")
30
+ # lineno = 0
31
+ # lines.each_line do |line|
32
+ # lineno += 1
33
+ # line = line.delete!("\n")
34
+ # puts("#{lineno}] '#{line}'")
35
+ # end
36
+ # puts("<<< lines: #{lineno}")
37
+
38
+ _reset_all
39
+
40
+ lineno = 0
41
+ lines.each do |line|
42
+ lineno += 1
43
+ line = line.strip
44
+
45
+ # empty line
46
+ next if line == ''
47
+
48
+ # comment
49
+ next if %w[; #].include?(line[0])
50
+
51
+ if line[0] == '['
52
+ _parse_section(line)
53
+ else
54
+ _parse_single_line(line)
55
+ end
56
+ end
57
+
58
+ # save previous section
59
+ @content[@curr_section_name] = @curr_section if @curr_section_name == :unnamed && @curr_section != {}
60
+
61
+ @content
62
+ end
63
+
64
+ ###
65
+ # reset content info
66
+ #
67
+ # @return None
68
+ def _reset_all
69
+ @content = {}
70
+ @curr_param_name = :unnamed
71
+ @curr_param = nil
72
+ @curr_section_name = :unnamed
73
+ @curr_section = {}
74
+ end
75
+
76
+ ###
77
+ # parse a line containing a section name
78
+ #
79
+ # @param line the line to parse
80
+ # @return None
81
+ def _parse_section(line)
82
+ # save previous section if not already
83
+ if !@content.key?(@curr_section_name) && (@curr_section_name != :unnamed || @curr_section != {})
84
+ @content[@curr_section_name] = @curr_section
85
+ end
86
+
87
+ # TODO: check section_name; what chars are allowed?
88
+ m = line.match(/\[\s*(.*)\s*\]/)
89
+ @curr_section_name = m[1].strip
90
+ @curr_section = {}
91
+ @content[@curr_section_name] = @curr_section
92
+ @curr_param = nil
93
+ @curr_param_name = :unnamed
94
+ end
95
+
96
+ ###
97
+ # parse the given line.
98
+ # If it is an id or an assigment it is handled
99
+ # otherwise it is ignored.
100
+ #
101
+ # @param line the line to parse
102
+ # @return None
103
+ def _parse_single_line(line)
104
+ # TODO: alphanum+; check if line has only alphanum
105
+ unless line.include?('=')
106
+ id = _clean_it(line)
107
+ _parse_raw_id(id)
108
+ return
109
+ end
110
+
111
+ tokens = line.split('=')
112
+ if tokens.length == 1
113
+ _parse_assignment(tokens[0], '')
114
+ elsif tokens.length == 2
115
+ _parse_assignment(tokens[0], tokens[1])
116
+ else
117
+ # TODO: raise excp?
118
+ puts("BUG unknown format, tokens: #{tokens}")
119
+ end
120
+ end
121
+
122
+ ###
123
+ # handle an assigment given the name and values found.
124
+ #
125
+ # @param name the name found
126
+ # @param val the value found
127
+ # @return None
128
+ def _parse_assignment(name, val)
129
+ @curr_section[@curr_param_name] = @curr_param if @curr_param_name != :unnamed
130
+
131
+ @curr_param_name = name.strip
132
+ @curr_param = nil
133
+
134
+ val = val.strip
135
+ if val == ''
136
+ @curr_param = val
137
+ @curr_section[@curr_param_name] = @curr_param
138
+ return
139
+ end
140
+
141
+ # handle single quoted values
142
+ m = val.scan(/'([^']*)'/)
143
+ if m.length == 1
144
+ _clean_and_parse1(val)
145
+ return
146
+ end
147
+
148
+ if m.length > 1
149
+ _clean_and_parse_scan_many(m)
150
+ return
151
+ end
152
+
153
+ # handle double quoted values
154
+ m = val.scan(/"([^"]*)"/)
155
+ if m.length == 1
156
+ _clean_and_parse1(val)
157
+ return
158
+ end
159
+
160
+ if m.length > 1
161
+ _clean_and_parse_scan_many(m)
162
+ return
163
+ end
164
+
165
+ # handle unquoted values
166
+ vals = val.split
167
+ if vals.length == 1
168
+ _clean_and_parse1(val)
169
+ else
170
+ _clean_and_parse_many(vals)
171
+ end
172
+ end
173
+
174
+ ###
175
+ # clean quotes etc. and then submit the single value as the current parameter
176
+ #
177
+ # @param val a single value
178
+ # @return None
179
+ def _clean_and_parse1(val)
180
+ id = _clean_it(val)
181
+ _parse_raw_id(id)
182
+ end
183
+
184
+ ###
185
+ # clean quotes etc. and then submit the resulting values as the current parameter
186
+ #
187
+ # @param vals a result from a .scan()
188
+ # @return None
189
+ def _clean_and_parse_scan_many(vals)
190
+ results = []
191
+ vals.each_with_index do |v, i|
192
+ results[i] = _clean_it(v[0])
193
+ end
194
+ @curr_param = results
195
+ @curr_section[@curr_param_name] = @curr_param
196
+ end
197
+
198
+ ###
199
+ # clean quotes etc. and then submit the resulting values as the current parameter
200
+ #
201
+ # @param vals a multiple value
202
+ # @return None
203
+ def _clean_and_parse_many(vals)
204
+ results = []
205
+ vals.each do |v|
206
+ id = _clean_it(v)
207
+ results.append(id)
208
+ end
209
+ @curr_param = results
210
+ @curr_section[@curr_param_name] = @curr_param
211
+ end
212
+
213
+ ###
214
+ # clean quotes or commas from the given value.
215
+ #
216
+ # @param val (string) the value to clean up
217
+ # @return None
218
+ def _clean_it(val)
219
+ if val[0] == '\''
220
+ m = val.match(/'(.*)'/)
221
+ val = m[1].strip
222
+ elsif val[0] == '"'
223
+ m = val.match(/"(.*)"/)
224
+ val = m[1].strip
225
+ elsif val.end_with?(',')
226
+ val = val.delete_suffix(',')
227
+ end
228
+
229
+ val = '' if val == '[]'
230
+
231
+ val
232
+ end
233
+
234
+ ###
235
+ # check and store the given value
236
+ #
237
+ # @param id the entity to store
238
+ # @return None
239
+ def _parse_raw_id(id)
240
+ if @curr_param.nil?
241
+ @curr_param = id
242
+ elsif @curr_param.instance_of?(Array)
243
+ @curr_param.append(id)
244
+ elsif @curr_param.instance_of?(String)
245
+ @curr_param = [@curr_param, id]
246
+ else
247
+ # :nocov: coverage: unknown how to cause
248
+ puts("BUG parse_raw_id: unknown format '#{id}'")
249
+ # :nocov:
250
+ end
251
+ @curr_section[@curr_param_name] = @curr_param
252
+ end
253
+ end
@@ -0,0 +1,2 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'parser/config_parser'
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+ ## current version
3
+ VERSION = '0.1.0'
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rb_cfg_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - John Arrizza
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-03-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ruby gem to parse and load .cfg files
14
+ email: cppgent@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE.txt
20
+ - README.md
21
+ - lib/parser/config_parser.rb
22
+ - lib/rb_cfg_parser.rb
23
+ - lib/version.rb
24
+ homepage: https://rubygems.org/gems/rb_cfg_parser
25
+ licenses:
26
+ - MIT
27
+ metadata:
28
+ homepage_uri: https://arrizza.com/rb-cfg-parser
29
+ source_code_uri: https://bitbucket.org/arrizza-public/rb-cfg-parser/src/master
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: 3.2.0
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubygems_version: 3.4.20
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: parse a .cfg text file
49
+ test_files: []