vipnet_parser 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/vipnet_parser.rb +113 -112
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d834041f66023f7257a3a5a3eb3d86d3aae798ed
4
- data.tar.gz: 12061630839986ebafb911c7b7e15926157a1a7c
3
+ metadata.gz: 6c12c7a620a2e80a808e81ef380e3c84ef4498a4
4
+ data.tar.gz: b5c9c4dd56f9cc43619ea7109705e628cc3fb513
5
5
  SHA512:
6
- metadata.gz: 378f8d2f99939bef16a1d60651571ba829e097c7e998222271d6bfcfbce606be3a3aa51628ae834a14a6628d1f9e893bd75fa4d4235185c0e09e48e6cc1feff2
7
- data.tar.gz: 31b653b65ba55eab00aa7dff8e83ff7155166b5b804b388755dc3ab696ea4527b802b1400b90a2d64fb191b32ce4a4c35edc3b13af3e9b369b2d2d06059b2e6b
6
+ metadata.gz: c6c2300da35ea7e80b52899f17a5ca24063ba09db10625677e22c7d198b19e4cf8bc9c729fe103ca98ff01c28313fab6c32b5db9e6d39c600bd03adb34817697
7
+ data.tar.gz: 83d219af3d3cd9395db168783b08f9b14b70479849b41be6a4a3ce737bb29b04a913b626920130026bf117ebbc1582e6afdfab0675737c03d2fbb2c45d615c2b
data/lib/vipnet_parser.rb CHANGED
@@ -1,13 +1,5 @@
1
- class VipnetParser
2
- def ==(other)
3
- res = true
4
- @props.each do |prop|
5
- res = res && self.send(prop.to_s) == other.send(prop.to_s)
6
- end
7
- res
8
- end
9
-
10
- def self.id(args)
1
+ module VipnetParser
2
+ def id(args)
11
3
  if args.class == String
12
4
  string = args
13
5
  array = Array.new
@@ -27,11 +19,10 @@ class VipnetParser
27
19
  if string =~ regexp && !string_matches_anything
28
20
  string_matches_anything = true
29
21
  array += callback.call({ string: Regexp.last_match(2), threshold: threshold })
30
- if Regexp.last_match(1) != ""
31
- array += VipnetParser::id({ string: Regexp.last_match(1), array: array, threshold: threshold })
32
- end
33
- if Regexp.last_match(3) != ""
34
- array += VipnetParser::id({ string: Regexp.last_match(3), array: array, threshold: threshold })
22
+ [Regexp.last_match(1), Regexp.last_match(3)].each do |side_match|
23
+ unless side_match.empty?
24
+ array += id({ string: side_match, array: array, threshold: threshold })
25
+ end
35
26
  end
36
27
  end
37
28
  end
@@ -69,7 +60,7 @@ class VipnetParser
69
60
  ["0x" + string.rjust(8, "0")]
70
61
  end
71
62
 
72
- def self.network(id)
63
+ def network(id)
73
64
  normal_ids = id(id)
74
65
  if normal_ids
75
66
  normal_id = normal_ids[0]
@@ -78,115 +69,125 @@ class VipnetParser
78
69
  false
79
70
  end
80
71
 
81
- private_class_method :id_parse_variant1, :id_parse_variant2, :id_parse_variant3
82
- end
83
-
84
- class Iplirconf < VipnetParser
85
- PROPS = [:content, :id, :sections]
86
- attr_accessor *PROPS, :last_error
87
- private_constant :PROPS
72
+ module_function :id, :network
88
73
 
89
- def initialize(*args)
90
- @props = PROPS
91
- unless args.size == 1
92
- return false
93
- end
94
- @content = args[0]
95
- # remove comments
96
- content_nc = @content.gsub(/^#.*\n/, "")
97
- # remove ending
98
- @sections = Hash.new
99
- adapter_position = content_nc.index("[adapter]")
100
- unless adapter_position
101
- @last_error = "unable to parse iplirconf (no [adapter] section)"
102
- return false
74
+ class VipnetParser
75
+ def ==(other)
76
+ res = true
77
+ @props.each do |prop|
78
+ res = res && self.send(prop.to_s) == other.send(prop.to_s)
79
+ end
80
+ res
103
81
  end
104
- # prepare for split
105
- content_nc = content_nc[0..(adapter_position - 2)]
106
- content_nc = "\n" + content_nc
107
- content_nc.split("\n[id]\n").reject{ |t| t.empty? }.each_with_index do |section_content, i|
108
- tmp_section = Hash.new
109
- props = {
110
- :single => [
111
- :id, :name, :filterdefault, :tunnel,
112
- :firewallip, :port, :proxyid, :dynamic_timeout, :accessip,
113
- :usefirewall, :fixfirewall, :virtualip, :version,
114
- ],
115
- :multi => [:ip, :filterudp, :filtertcp]
116
- }
117
- props.each do |type, props|
118
- props.each do |prop|
119
- get_section_param({ prop: prop, section: tmp_section, content: section_content, type: type })
82
+ end
83
+
84
+ class Iplirconf < VipnetParser
85
+ PROPS = [:content, :id, :sections]
86
+ attr_accessor *PROPS, :last_error
87
+ private_constant :PROPS
88
+
89
+ def initialize(*args)
90
+ @props = PROPS
91
+ unless args.size == 1
92
+ return false
93
+ end
94
+ @content = args[0]
95
+ # remove comments
96
+ content_nc = @content.gsub(/^#.*\n/, "")
97
+ # remove ending
98
+ @sections = Hash.new
99
+ adapter_position = content_nc.index("[adapter]")
100
+ unless adapter_position
101
+ @last_error = "unable to parse iplirconf (no [adapter] section)"
102
+ return false
103
+ end
104
+ # prepare for split
105
+ content_nc = content_nc[0..(adapter_position - 2)]
106
+ content_nc = "\n" + content_nc
107
+ content_nc.split("\n[id]\n").reject{ |t| t.empty? }.each_with_index do |section_content, i|
108
+ tmp_section = Hash.new
109
+ props = {
110
+ :single => [
111
+ :id, :name, :filterdefault, :tunnel,
112
+ :firewallip, :port, :proxyid, :dynamic_timeout, :accessip,
113
+ :usefirewall, :fixfirewall, :virtualip, :version,
114
+ ],
115
+ :multi => [:ip, :filterudp, :filtertcp]
116
+ }
117
+ props.each do |type, props|
118
+ props.each do |prop|
119
+ get_section_param({ prop: prop, section: tmp_section, content: section_content, type: type })
120
+ end
120
121
  end
122
+ # self section id
123
+ @id = tmp_section[:id] if i == 0
124
+ @sections[tmp_section[:id]] = tmp_section
121
125
  end
122
- # self section id
123
- @id = tmp_section[:id] if i == 0
124
- @sections[tmp_section[:id]] = tmp_section
126
+ true
125
127
  end
126
- true
127
- end
128
128
 
129
- def get_section_param(args)
130
- value_regexp = Regexp.new("^#{args[:prop].to_s}=\s(.*)$")
131
- if args[:type] == :multi
132
- tmp_array = Array.new
133
- args[:content].each_line do |line|
134
- value = line[value_regexp, 1]
135
- tmp_array.push(value) if value
129
+ def get_section_param(args)
130
+ value_regexp = Regexp.new("^#{args[:prop].to_s}=\s(.*)$")
131
+ if args[:type] == :multi
132
+ tmp_array = Array.new
133
+ args[:content].each_line do |line|
134
+ value = line[value_regexp, 1]
135
+ tmp_array.push(value) if value
136
+ end
137
+ args[:section][args[:prop]] = tmp_array unless tmp_array.empty?
138
+ elsif args[:type] == :single
139
+ value = args[:content][value_regexp, 1]
140
+ args[:section][args[:prop]] = value if value
136
141
  end
137
- args[:section][args[:prop]] = tmp_array unless tmp_array.empty?
138
- elsif args[:type] == :single
139
- value = args[:content][value_regexp, 1]
140
- args[:section][args[:prop]] = value if value
141
142
  end
142
- end
143
143
 
144
- private :get_section_param
145
- end
144
+ private :get_section_param
145
+ end
146
146
 
147
- class Nodename < VipnetParser
148
- PROPS = [:content, :records]
149
- attr_accessor *PROPS, :last_error
150
- private_constant :PROPS
147
+ class Nodename < VipnetParser
148
+ PROPS = [:content, :records]
149
+ attr_accessor *PROPS, :last_error
150
+ private_constant :PROPS
151
151
 
152
- def initialize(*args)
153
- @props = PROPS
154
- unless args.size == 1
155
- return false
156
- end
157
- @content = args[0]
158
- lines = content.force_encoding("cp866").encode("utf-8", replace: nil).split("\r\n")
159
- if lines.size == 0
160
- @last_error = "error parsing nodename"
161
- return false
152
+ def initialize(*args)
153
+ @props = PROPS
154
+ unless args.size == 1
155
+ return false
156
+ end
157
+ @content = args[0]
158
+ lines = content.force_encoding("cp866").encode("utf-8", replace: nil).split("\r\n")
159
+ if lines.size == 0
160
+ @last_error = "error parsing nodename"
161
+ return false
162
+ end
163
+ @records = Hash.new
164
+ lines.each do |line|
165
+ tmp_record = Hash.new
166
+ tmp_record = get_record_params(line)
167
+ tmp_record[:name].rstrip!
168
+ tmp_record[:enabled] = { "1" => true, "0" => false }[tmp_record[:enabled]]
169
+ tmp_record[:category] = { "A" => :client, "S" => :server, "G" => :group }[tmp_record[:category]]
170
+ @records[tmp_record[:id]] = tmp_record
171
+ end
172
+ true
162
173
  end
163
- @records = Hash.new
164
- lines.each do |line|
165
- tmp_record = Hash.new
166
- tmp_record = get_record_params(line)
167
- tmp_record[:name].rstrip!
168
- tmp_record[:enabled] = { "1" => true, "0" => false }[tmp_record[:enabled]]
169
- tmp_record[:category] = { "A" => :client, "S" => :server, "G" => :group }[tmp_record[:category]]
170
- @records[tmp_record[:id]] = tmp_record
174
+
175
+ def get_record_params(line)
176
+ record_regexp = /^
177
+ (?<name>.{50})\s
178
+ (?<enabled>[01])\s
179
+ (?<category>[ASG])\s
180
+ [0-9A-F]{8}
181
+ (?<server_number>[0-9A-F]{4})
182
+ (?<abonent_number>[0-9A-F]{4})\s
183
+ (?<id>[0-9A-F]{8})
184
+ $/x
185
+ match = record_regexp.match(line)
186
+ names = match.names.map { |name| name.to_sym }
187
+ # https://gist.github.com/flarnie/6221219
188
+ return Hash[names.zip(match.captures)]
171
189
  end
172
- true
173
- end
174
190
 
175
- def get_record_params(line)
176
- record_regexp = /^
177
- (?<name>.{50})\s
178
- (?<enabled>[01])\s
179
- (?<category>[ASG])\s
180
- [0-9A-F]{8}
181
- (?<server_number>[0-9A-F]{4})
182
- (?<abonent_number>[0-9A-F]{4})\s
183
- (?<id>[0-9A-F]{8})
184
- $/x
185
- match = record_regexp.match(line)
186
- names = match.names.map { |name| name.to_sym }
187
- # https://gist.github.com/flarnie/6221219
188
- return Hash[names.zip(match.captures)]
191
+ private :get_record_params
189
192
  end
190
-
191
- private :get_record_params
192
193
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vipnet_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Morozov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-29 00:00:00.000000000 Z
11
+ date: 2016-05-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Parses ViPNet strings like 'something 1A0EABCD something' and returns
14
- 0x1a0eabcd (and stuff).
13
+ description: Parses ViPNet strings like 'something 1A0EABCD something', iplir.conf
14
+ and other files.
15
15
  email: ntcomp12@gmail.com
16
16
  executables: []
17
17
  extensions: []