lineparser 0.1.15 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/lib/lineparser.rb +274 -190
  5. metadata +32 -29
  6. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: fbaa04ee1873fa49ae03df2a7bc95c9884963abc
4
- data.tar.gz: 6b5617d478478ec1caef1eb74be16925d8e80a88
2
+ SHA256:
3
+ metadata.gz: da9c29d07840e85a7898267fd02c3af3327661472f37422187d3c728bcfc0b47
4
+ data.tar.gz: b018de36af3c41e709236ae27066ce6cabe31f8ce92a719d0d68f810a7fd2704
5
5
  SHA512:
6
- metadata.gz: b1946c7cec2d57fc347180b9dbbf7500ff1ae4f24a7813fe736c79ac3c067c5a57c6ed4e84565eb0fbec3e7436236f4451b0014b8823a7f752f995f0e23fdd74
7
- data.tar.gz: da3de64e96337a37fc443ea18f5c96ac88b4456c5c921cee1dd0bcfe8ddc2b4708a378011ebe8ce8fa078a74ea9b7e298148d2bdb14db17b00af76e8b6bed563
6
+ metadata.gz: 65d5e8b1185f3980b58598a4b32445ca6bb4cafe24ebcc9ba1ba5db65843f1ba8fc336276aa26ccbfa256b40faf7cff98375363dca733de45fe3b703a269326d
7
+ data.tar.gz: 52e55cea8d3cabaec9f333f0c3ead7a47e160de4b97aa2f871d3677655367fa608bad68a332b397887a8325934c96d9ff059f6fefc6db63bdc34b078e8038293
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -1,190 +1,274 @@
1
- #!/usr/bin/env ruby
2
-
3
- # file: lineparser.rb
4
-
5
- require 'line-tree'
6
-
7
-
8
- class LineParser
9
-
10
- def initialize(patterns=[], lines=nil, ignore_blank_lines: true)
11
-
12
- @ibl = ignore_blank_lines
13
- @h = {
14
-
15
- String: lambda do |s, pattern|
16
-
17
- labels = []
18
-
19
- pattern.gsub!('+',"\\\\+")
20
- r = s.match(/#{pattern.gsub(/:\w+/) {|x| labels << x; '(\\S+)'}}/)
21
-
22
- if r then
23
- params = Hash[*labels.zip(r.captures).flatten(1)]
24
- end
25
-
26
- end,
27
-
28
- Regexp: lambda do |s, regex|
29
-
30
- r = s.match(regex)
31
-
32
- if r then
33
- h = {captures: r.captures}
34
- r.names.inject(h) {|rn,x| rn.merge(x.to_sym => r[x])} if r.names
35
- end
36
- end
37
- }
38
-
39
- @patterns = patterns.select {|x| x.first == :all}
40
-
41
- hpatterns = {root: []}.merge patterns.inject({}){|r,x| r.merge(x[2] => x)}
42
-
43
- hpatterns.reverse_each do |k,v|
44
- hpatterns[v.first] << v if hpatterns[v.first]
45
- end
46
-
47
- @tree_patterns = hpatterns[:root].reverse
48
-
49
- parse lines if lines
50
-
51
- end
52
-
53
- def parse(s)
54
- @a = scan @tree_patterns, LineTree.new(s, ignore_blank_lines: @ibl).to_a
55
- end
56
-
57
- def to_a
58
- @a
59
- end
60
-
61
- def to_xml
62
- Rexle.new(xmlize(@a).inject([:root, '', {}]){|r,x| r << x}).xml if @a
63
- end
64
-
65
- private
66
-
67
- def join(lines, indent='')
68
- lines.map do |x|
69
- indent + (x.is_a?(Array) ? join(x, indent + ' ') : x)
70
- end.join("\n")
71
- end
72
-
73
- def scan(xpatterns, items)
74
-
75
- records = []
76
-
77
- while items.any? do
78
-
79
- x = items.shift
80
-
81
- params, context = nil, nil
82
-
83
- xpatterns = [xpatterns] unless xpatterns[0].is_a? Array
84
-
85
- found = @patterns.detect do |_, pattern|
86
- params = @h[pattern.class.to_s.to_sym].call x.first, pattern
87
- end
88
-
89
- if found then
90
-
91
- children = nil
92
- children = scan(found.last, x[1..-1]) if found.last.is_a? Array
93
- records << [found[2], params, x, children]
94
-
95
- else
96
-
97
- found = xpatterns.detect do |_, pattern, id|
98
-
99
- if pattern == :root then
100
-
101
- found = @tree_patterns.detect do |_, pattern2, id|
102
- params = @h[pattern2.class.to_s.to_sym].call x.first, pattern2
103
- context = id if params
104
- end
105
-
106
- else
107
-
108
- params = @h[pattern.class.to_s.to_sym].call x.first, pattern
109
- context = id if params
110
- end
111
- end
112
-
113
- if found then
114
-
115
- children = nil
116
- children = scan(found[3..-1], x[1..-1]) if found.last.is_a? Array
117
- records << [context, params, x, children]
118
- end
119
- end
120
- end
121
-
122
- return records
123
- end
124
-
125
- def xmlize(rows)
126
-
127
- r = rows.map do |row|
128
-
129
- label, h, lines, children = row
130
-
131
- new_h = h.inject({}) do |r,k|
132
-
133
- if k.first == :captures then
134
-
135
- k[-1].map.with_index.to_a.inject({}) do |r2,x|
136
- x[0] ? r.merge!(('captures' + x[-1].to_s).to_sym => x[0]) : r
137
- end
138
- else
139
- r.merge k[0][/\w+/] => k[-1]
140
- end
141
- end
142
-
143
- c = children ? xmlize(children) : []
144
-
145
- [label, join(lines), new_h, *c]
146
- end
147
-
148
- r
149
- end
150
-
151
- end
152
-
153
-
154
- =begin
155
-
156
- Basic example:
157
-
158
- lines =<<LINES
159
- resources: posts
160
- # winning
161
- #
162
- post
163
- model
164
- Post
165
- orange 123
166
- fff
167
- comments
168
- model
169
- Comment
170
- orange 576
171
- ggg
172
- LINES
173
-
174
- patterns = [
175
- [:root, 'resources: :resources', :resources],
176
- [:root, ':resource', :resource],
177
- [:resource, 'model', :model],
178
- [:model, ':class_name', :model_class],
179
- [:model_class, /orange (\w+)/, :model_class_attribute],
180
- [:all, /#/, :comment]
181
- ]
182
-
183
- lp = LineParser.new patterns
184
- r = lp.parse lines
185
- #=>
186
- => [
187
- [:app_path, {":app_path"=>"/tmp"}, ["app_path: /tmp"], nil],
188
- [:app, {":app"=>"blog"}, ["app: blog"], nil],
189
- [:resources, {":resources"=>"posts"}, ["resources: posts"], ...
190
- =end
1
+ #!/usr/bin/env ruby
2
+
3
+ # file: lineparser.rb
4
+
5
+ require 'line-tree'
6
+
7
+
8
+ class LineParser
9
+
10
+ def initialize(patterns=[], lines=nil, ignore_blank_lines: true, debug: false)
11
+
12
+ @ibl, @debug = ignore_blank_lines, debug
13
+ @h = {
14
+
15
+ String: lambda do |s, pattern|
16
+
17
+ labels = []
18
+
19
+ pattern.gsub!('+',"\\\\+")
20
+ r = s.match(/#{pattern.gsub(/:\w+/) {|x| labels << x; '([^\\n]*)'}}/)
21
+
22
+ if r then
23
+ params = Hash[*labels.zip(r.captures).flatten(1)]
24
+ end
25
+
26
+ end,
27
+
28
+ Regexp: lambda do |s, regex|
29
+
30
+ r = s.match(regex)
31
+
32
+ if r then
33
+ h = {captures: r.captures}
34
+ r.names.inject(h) {|rn,x| rn.merge(x.to_sym => r[x])} if r.names
35
+ end
36
+ end
37
+ }
38
+
39
+ @patterns = patterns.select {|x| x.first == :all}
40
+
41
+ hpatterns = {root: []}.merge patterns.inject({}){|r,x| r.merge(x[2] => x)}
42
+
43
+ hpatterns.reverse_each do |k,v|
44
+ hpatterns[v.first] << v if hpatterns[v.first]
45
+ end
46
+
47
+ @tree_patterns = hpatterns[:root].reverse
48
+
49
+ parse lines if lines
50
+
51
+ end
52
+
53
+ def parse(s)
54
+
55
+ puts 'inside parse()' if @debug
56
+ a = scan @tree_patterns, LineTree.new(s, ignore_blank_lines: @ibl).to_a
57
+ @h2 = build_hash a
58
+ @a = a
59
+
60
+ end
61
+
62
+ def to_a
63
+ @a
64
+ end
65
+
66
+ def to_h()
67
+ @h2
68
+ end
69
+
70
+ def to_xml
71
+ raw_doc = xmlize(@a).inject([:root, {}, '']){|r,x| r << x}
72
+ puts 'raw_doc: ' + raw_doc.inspect if @debug
73
+ Rexle.new(raw_doc).xml if @a
74
+ end
75
+
76
+ private
77
+
78
+
79
+ def build_hash(a)
80
+
81
+ def filter(h2)
82
+
83
+ h = {}
84
+ puts 'h2: ' + h2.inspect if @debug
85
+
86
+ h2.each do |k, v|
87
+
88
+ puts 'v:' + v.inspect if @debug
89
+
90
+ a3 = v.flat_map do |row|
91
+
92
+ a2 = []
93
+
94
+ puts 'row: ' + row.inspect
95
+ puts 'row[3]: ' + row[3].inspect
96
+
97
+ if row[3] and row[3].any? then
98
+
99
+ puts 'row[3][0][1]: ' + row[3][0][1].inspect if @debug
100
+
101
+ if row[3][0][1].has_key? :captures and row[3][0][1][:captures].any? then
102
+
103
+ a2 = row[3].map {|x| x[2].first }
104
+
105
+ else
106
+ a2 = filter(row[3].group_by {|x| x.first })
107
+ end
108
+
109
+ else
110
+ a2 = row[1].values.first
111
+ end
112
+
113
+ key = row[1].values.first
114
+ key ||= a2
115
+ (key.empty? or key == a2) ? a2 : {key => a2}
116
+
117
+ end
118
+
119
+ h[k] = a3.length > 1 ? a3 : a3.first
120
+
121
+ end
122
+
123
+ return h
124
+ end
125
+
126
+ h3 = a.group_by {|x| x.first }
127
+
128
+ filter(h3)
129
+
130
+ end
131
+
132
+ def join(lines, indent='')
133
+ lines.map do |x|
134
+ indent + (x.is_a?(Array) ? join(x, indent + ' ') : x)
135
+ end.join("\n")
136
+ end
137
+
138
+ def scan(xpatterns, items)
139
+
140
+ puts 'inside scan()' if @debug
141
+
142
+ records = []
143
+
144
+ while items.any? do
145
+
146
+ x = items.shift
147
+
148
+ params, context = nil, nil
149
+
150
+ xpatterns = [xpatterns] unless xpatterns[0].is_a? Array
151
+
152
+ found = @patterns.detect do |_, pattern|
153
+ params = @h[pattern.class.to_s.to_sym].call x.first, pattern
154
+ end
155
+
156
+ puts 'found: ' + found.inspect if @debug
157
+
158
+ if found then
159
+
160
+ children = nil
161
+ children = scan(found.last, x[1..-1]) if found.last.is_a? Array
162
+ records << [found[2], params, x, children]
163
+
164
+ else
165
+
166
+ puts 'xpatterns: ' + xpatterns.inspect if @debug
167
+
168
+ found = xpatterns.detect do |_, pattern, id|
169
+
170
+ puts 'found2: ' + found.inspect if @debug
171
+
172
+ if pattern == :root then
173
+
174
+ found = @tree_patterns.detect do |_, pattern2, id|
175
+ params = @h[pattern2.class.to_s.to_sym].call x.first, pattern2
176
+ context = id if params
177
+ end
178
+
179
+ puts 'found3: ' + found.inspect if @debug
180
+
181
+ else
182
+
183
+ if @debug then
184
+ puts '@h: ' + @h.inspect
185
+ puts 'pattern: ' + pattern.inspect
186
+ puts 'x.first: ' + x.first.inspect
187
+ end
188
+
189
+ params = @h[pattern.class.to_s.to_sym].call x.first, pattern
190
+ puts 'params: ' + params.inspect if @debug
191
+ context = id if params
192
+ end
193
+ end
194
+
195
+ if found then
196
+
197
+ children = nil
198
+ children = scan(found[3..-1], x[1..-1]) if found.last.is_a? Array
199
+ records << [context, params, x, children]
200
+ end
201
+ end
202
+ end
203
+
204
+ return records
205
+ end
206
+
207
+ def xmlize(rows)
208
+
209
+ r = rows.map do |row|
210
+
211
+ label, h, lines, children = row
212
+
213
+ new_h = h.inject({}) do |r,k|
214
+
215
+ if k.first == :captures and k.last.any? then
216
+
217
+ puts 'k: ' + k.inspect if @debug
218
+
219
+ k[-1].map.with_index.to_a.inject({}) do |r2,x|
220
+ x[0] ? r.merge!(('captures' + x[-1].to_s).to_sym => x[0]) : r
221
+ end
222
+ else
223
+ r.merge k[0][/\w+/] => k[-1]
224
+ end
225
+ end
226
+
227
+ c = children ? xmlize(children) : []
228
+
229
+ [label, new_h, join(lines), *c]
230
+ end
231
+
232
+ r
233
+ end
234
+
235
+ end
236
+
237
+
238
+ =begin
239
+
240
+ Basic example:
241
+
242
+ lines =<<LINES
243
+ resources: posts
244
+ # winning
245
+ #
246
+ post
247
+ model
248
+ Post
249
+ orange 123
250
+ fff
251
+ comments
252
+ model
253
+ Comment
254
+ orange 576
255
+ ggg
256
+ LINES
257
+
258
+ patterns = [
259
+ [:root, 'resources: :resources', :resources],
260
+ [:root, ':resource', :resource],
261
+ [:resource, 'model', :model],
262
+ [:model, ':class_name', :model_class],
263
+ [:model_class, /orange (\w+)/, :model_class_attribute],
264
+ [:all, /#/, :comment]
265
+ ]
266
+
267
+ lp = LineParser.new patterns
268
+ r = lp.parse lines
269
+ #=>
270
+ => [
271
+ [:app_path, {":app_path"=>"/tmp"}, ["app_path: /tmp"], nil],
272
+ [:app, {":app"=>"blog"}, ["app: blog"], nil],
273
+ [:resources, {":resources"=>"posts"}, ["resources: posts"], ...
274
+ =end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lineparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.15
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -10,28 +10,32 @@ bindir: bin
10
10
  cert_chain:
11
11
  - |
12
12
  -----BEGIN CERTIFICATE-----
13
- MIIDljCCAn6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBIMRIwEAYDVQQDDAlnZW1t
14
- YXN0ZXIxHjAcBgoJkiaJk/IsZAEZFg5qYW1lc3JvYmVydHNvbjESMBAGCgmSJomT
15
- 8ixkARkWAmV1MB4XDTE1MDcwOTIyMTk1OFoXDTE2MDcwODIyMTk1OFowSDESMBAG
16
- A1UEAwwJZ2VtbWFzdGVyMR4wHAYKCZImiZPyLGQBGRYOamFtZXNyb2JlcnRzb24x
17
- EjAQBgoJkiaJk/IsZAEZFgJldTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
18
- ggEBAJ4i0fv1FiTLo/nP6kFVtXNB0rYEcqrynSgTgsz7DtXAxJSWOB7fcyFG/Sfg
19
- 9sXRsoiqnf4JR1ipnnB6FPiikzSTkxTrkiyJcS/wryNWBhoghUlXRl43pBi6jVDe
20
- LRz1xGYZJYephGGzpaHpNBxjFNxoiBWAWlJIyLIkj6n7Bn7S7JjLRwuC7NuvKuyA
21
- /TSOe+ONQ7monyzRf388vwN3IFVXEnwRbGyywfaKZENtrY9haRECqObouTXm2FO3
22
- XvUYSONPV+ujqxpIKjzepJLwsP/hvy0uaLqGOFP1S1SV7E7i3x/oemMS717ddBbV
23
- SfzmXWratsDvWPkYS0M9e5bqzasCAwEAAaOBijCBhzAJBgNVHRMEAjAAMAsGA1Ud
24
- DwQEAwIEsDAdBgNVHQ4EFgQUb/1jXj2E9ch7RD60+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DK
33
37
  -----END CERTIFICATE-----
34
- date: 2015-07-09 00:00:00.000000000 Z
38
+ date: 2020-09-17 00:00:00.000000000 Z
35
39
  dependencies:
36
40
  - !ruby/object:Gem::Dependency
37
41
  name: line-tree
@@ -39,22 +43,22 @@ dependencies:
39
43
  requirements:
40
44
  - - "~>"
41
45
  - !ruby/object:Gem::Version
42
- version: '0.3'
46
+ version: '0.9'
43
47
  - - ">="
44
48
  - !ruby/object:Gem::Version
45
- version: 0.3.17
49
+ version: 0.9.1
46
50
  type: :runtime
47
51
  prerelease: false
48
52
  version_requirements: !ruby/object:Gem::Requirement
49
53
  requirements:
50
54
  - - "~>"
51
55
  - !ruby/object:Gem::Version
52
- version: '0.3'
56
+ version: '0.9'
53
57
  - - ">="
54
58
  - !ruby/object:Gem::Version
55
- version: 0.3.17
59
+ version: 0.9.1
56
60
  description:
57
- email: james@r0bertson.co.uk
61
+ email: james@jamesrobertson.eu
58
62
  executables: []
59
63
  extensions: []
60
64
  extra_rdoc_files: []
@@ -79,8 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
83
  - !ruby/object:Gem::Version
80
84
  version: '0'
81
85
  requirements: []
82
- rubyforge_project:
83
- rubygems_version: 2.4.6
86
+ rubygems_version: 3.0.3
84
87
  signing_key:
85
88
  specification_version: 4
86
89
  summary: Lineparser is suited to parsing configuration files, however it can parse
metadata.gz.sig CHANGED
Binary file