lineparser 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +4 -0
- data/lib/lineparser.rb +142 -0
- data.tar.gz.sig +0 -0
- metadata +82 -0
- metadata.gz.sig +2 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 427c2283c5e90e522e5a8c2a198300f37557901d
|
4
|
+
data.tar.gz: 85e6d7959ea2bbd9823461c762ab679f8c996efe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 877272b35e42867559b252eddb1db7f414fdbbbb067851f178dbb28359bb0aee9e0e5bf67631f200ed7f70123999b88986a2ae811972cedeacb0b25964deca52
|
7
|
+
data.tar.gz: e501ae78cc83ff03bef5532907935b9085ec73bf6f16b385c9abc65988362598cc948ae5ff13f6223ad601ee00b0a4bc416a23e97a33c8c796329c8c730e9aa7
|
checksums.yaml.gz.sig
ADDED
data/lib/lineparser.rb
ADDED
@@ -0,0 +1,142 @@
|
|
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=[])
|
11
|
+
|
12
|
+
@h = {
|
13
|
+
|
14
|
+
String: lambda do |s, pattern|
|
15
|
+
|
16
|
+
labels = []
|
17
|
+
r = s.match(/#{pattern.gsub(/:\w+/) {|x| labels << x; '(\\w+)'}}/)
|
18
|
+
|
19
|
+
if r then
|
20
|
+
params = Hash[*labels.zip(r.captures).flatten(1)]
|
21
|
+
end
|
22
|
+
|
23
|
+
end,
|
24
|
+
|
25
|
+
Regexp: lambda do |s, regex|
|
26
|
+
|
27
|
+
r = s.match(regex)
|
28
|
+
|
29
|
+
if r then
|
30
|
+
{captures: r.captures}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
}
|
34
|
+
|
35
|
+
@patterns = patterns.select {|x| x.first == :all}
|
36
|
+
|
37
|
+
hpatterns = {root: []}.merge patterns.inject({}){|r,x| r.merge(x[2] => x)}
|
38
|
+
|
39
|
+
hpatterns.reverse_each do |k,v|
|
40
|
+
hpatterns[v.first] << v if hpatterns[v.first]
|
41
|
+
end
|
42
|
+
|
43
|
+
@tree_patterns = hpatterns[:root].reverse
|
44
|
+
end
|
45
|
+
|
46
|
+
def parse(s)
|
47
|
+
scan @tree_patterns, LineTree.new(s).to_a
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def scan(xpatterns, items)
|
53
|
+
|
54
|
+
records = []
|
55
|
+
|
56
|
+
while items.any? do
|
57
|
+
|
58
|
+
x = items.shift
|
59
|
+
params = nil
|
60
|
+
|
61
|
+
xpatterns = [xpatterns] unless xpatterns[0].is_a? Array
|
62
|
+
|
63
|
+
found = @patterns.detect do |_, pattern|
|
64
|
+
params = @h[pattern.class.to_s.to_sym].call x.first, pattern
|
65
|
+
end
|
66
|
+
|
67
|
+
if found then
|
68
|
+
children = nil
|
69
|
+
children = scan(found.last, x[1..-1]) if found.last.is_a? Array
|
70
|
+
records << [found.first, params, x, children]
|
71
|
+
else
|
72
|
+
|
73
|
+
found = xpatterns.detect do |_, pattern|
|
74
|
+
params = @h[pattern.class.to_s.to_sym].call x.first, pattern
|
75
|
+
end
|
76
|
+
|
77
|
+
if found then
|
78
|
+
children = nil
|
79
|
+
children = scan(found.last, x[1..-1]) if found.last.is_a? Array
|
80
|
+
records << [found.first, params, x, children]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
return records
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
=begin
|
91
|
+
|
92
|
+
Basic example:
|
93
|
+
|
94
|
+
lines =<<LINES
|
95
|
+
resources: posts
|
96
|
+
# winning
|
97
|
+
#
|
98
|
+
post
|
99
|
+
model
|
100
|
+
Post
|
101
|
+
orange 123
|
102
|
+
fff
|
103
|
+
comments
|
104
|
+
model
|
105
|
+
Comment
|
106
|
+
orange 576
|
107
|
+
ggg
|
108
|
+
LINES
|
109
|
+
|
110
|
+
patterns = [
|
111
|
+
[:root, 'resources: :resources', :resources],
|
112
|
+
[:root, ':resource', :resource],
|
113
|
+
[:resource, 'model', :model],
|
114
|
+
[:model, ':class_name', :model_class],
|
115
|
+
[:model_class, /orange (\w+)/, :model_class_attribute],
|
116
|
+
[:all, /#/]
|
117
|
+
]
|
118
|
+
|
119
|
+
lp = LineParser.new patterns
|
120
|
+
r = lp.parse lines
|
121
|
+
#=>
|
122
|
+
[
|
123
|
+
[:root, {":resources"=>"posts"}, ["resources: posts"], nil],
|
124
|
+
[:all, {:captures=>[]}, ["# winning"], nil],
|
125
|
+
[:all, {:captures=>[]}, ["#"], nil],
|
126
|
+
[:root, {":resource"=>"post"}, ["post", ["model", ["Post", ["orange 123"], ["fff"]]]],
|
127
|
+
[[:resource, {}, ["model", ["Post", ["orange 123"], ["fff"]]],
|
128
|
+
[[:model, {":class_name"=>"Post"}, ["Post", ["orange 123"], ["fff"]],
|
129
|
+
[[:model_class, {:captures=>["123"]}, ["orange 123"], nil]]
|
130
|
+
]]
|
131
|
+
]]
|
132
|
+
],
|
133
|
+
[:root, {":resource"=>"comments"}, ["comments", ["model", ["Comment", ["orange 576"], ["ggg"]]]],
|
134
|
+
[[:resource, {}, ["model", ["Comment", ["orange 576"], ["ggg"]]],
|
135
|
+
[[:model, {":class_name"=>"Comment"}, ["Comment", ["orange 576"], ["ggg"]],
|
136
|
+
[[:model_class, {:captures=>["576"]}, ["orange 576"], nil]]
|
137
|
+
]]
|
138
|
+
]]
|
139
|
+
]
|
140
|
+
]
|
141
|
+
|
142
|
+
=end
|
data.tar.gz.sig
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lineparser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Robertson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain:
|
11
|
+
- |
|
12
|
+
-----BEGIN CERTIFICATE-----
|
13
|
+
MIIDljCCAn6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBIMRIwEAYDVQQDDAlnZW1t
|
14
|
+
YXN0ZXIxHjAcBgoJkiaJk/IsZAEZFg5qYW1lc3JvYmVydHNvbjESMBAGCgmSJomT
|
15
|
+
8ixkARkWAmV1MB4XDTE0MDMwOTEzMzUzMVoXDTE1MDMwOTEzMzUzMVowSDESMBAG
|
16
|
+
A1UEAwwJZ2VtbWFzdGVyMR4wHAYKCZImiZPyLGQBGRYOamFtZXNyb2JlcnRzb24x
|
17
|
+
EjAQBgoJkiaJk/IsZAEZFgJldTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
|
18
|
+
ggEBAKT8WDhUkAxMakZC25fSVb0H82lTzSl1OUCA3ZignFhCumvtbdEjyaL/eSKE
|
19
|
+
VCrFjjoizjOYMRtBq1Kxa1wKpoYBGpjIFpXejRoQCR0LSUu8lLmlgHt3OP85+DUm
|
20
|
+
Y2YawBaNejN9SY0V5jZqhg716EV5WMhXFSx88zYKq72hWvNjbW4LWKR08AlMRPEe
|
21
|
+
xC5ZstKQ3VUuNmpoMM69F7Qe+K4Zk+Mb56FQQjuHWPAgYPA7xOuiSemIu/A1LGL3
|
22
|
+
39Rx3fAZQPtz64ipETny3xj6U2cXfheg1VwqX9HICSoy6l9SyNKt1z/zs02lqOIV
|
23
|
+
VG7nsDwggMCg7vsj+TvsCEhRaY8CAwEAAaOBijCBhzAJBgNVHRMEAjAAMAsGA1Ud
|
24
|
+
DwQEAwIEsDAdBgNVHQ4EFgQUAHPlqIu4sStVa3sL20phQBqdprowJgYDVR0RBB8w
|
25
|
+
HYEbZ2VtbWFzdGVyQGphbWVzcm9iZXJ0c29uLmV1MCYGA1UdEgQfMB2BG2dlbW1h
|
26
|
+
c3RlckBqYW1lc3JvYmVydHNvbi5ldTANBgkqhkiG9w0BAQUFAAOCAQEALJjImPoy
|
27
|
+
AtgoUr0vGIrsqgKwIhDD4GzreuBgak8u87h24jF7E3pN+kyr82EGLO5/pY1jNl9k
|
28
|
+
e5sP8da58yYU69qhx9HTZoL3hsQpgAf9E1WclT2IcqvuqVkkezWfrT+XdJvCX9L5
|
29
|
+
LavWfrM5JgrVtMeBsZNtsaotXY7nDdG4pv2IubOyXJ/gQd2UdpcGhN2xx+WRKQbr
|
30
|
+
8RbGeWgNHEFGKJeCuHYEmE/uOudVm/drBc00dAagWDy0OUxKtwQiZ9TzbDmEYKcj
|
31
|
+
91WD0+jYVX++HpUKNCp85+avduv17ztFqGGok26cpRFYNdIIFFu8T4cl2jsqGI2l
|
32
|
+
hT3uS3yIJ12YEQ==
|
33
|
+
-----END CERTIFICATE-----
|
34
|
+
date: 2014-03-09 00:00:00.000000000 Z
|
35
|
+
dependencies:
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: line-tree
|
38
|
+
requirement: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
description:
|
51
|
+
email: james@r0bertson.co.uk
|
52
|
+
executables: []
|
53
|
+
extensions: []
|
54
|
+
extra_rdoc_files: []
|
55
|
+
files:
|
56
|
+
- lib/lineparser.rb
|
57
|
+
homepage: https://github.com/jrobertson/lineparser
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.1.11
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: Lineparser is suited to parsing configuration files, however it can parse
|
81
|
+
any type of text file which has repeating patterns identified by a heading etc.
|
82
|
+
test_files: []
|
metadata.gz.sig
ADDED