vipnet_parser 2.4.6 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 55c4590202f1956a350afab1e4176b6358e13975
4
- data.tar.gz: 1f407a462dcb35869af77229dc49592e0a4b5c12
3
+ metadata.gz: fd4aac8bf2f2cea3f9bb09767fdff61ee56cd4f5
4
+ data.tar.gz: 26457db935a0e9f58b7ae5a2a25008d22ea90760
5
5
  SHA512:
6
- metadata.gz: 7a566393249ea90c4eb02a1b0bff09fc464533806c6a03bac5ec541a6e7b49667496f9ca426152dda3b8683ee3ba3d480f391ec952c23e3c90147eb88e6437d9
7
- data.tar.gz: 90eb57f3871ba558327b54a9df85ac31b5d50c6fb0c8aea29dfc629c070bc08017fe67343cdd2bbb8cf9df0c47347ba25cdac8672f5f69880bd27d71b5d80157
6
+ metadata.gz: 1966c6bc0f4b30eb88a80d5954fa0d74068538038f4409711ead9ca6a4617302a3fe6f648d9c847f68e91a2417f626ed51c84b9e447e6f2a03ce4adb7b2845b6
7
+ data.tar.gz: 4cdee64d7a7e96926eac5e9a14fb4c410c3f2a13196b7b2b04064ec313163dc2ecd48c510a6fc23570ea4c44318e95f7a1374331b5df4b3c3c6aebc0110a8827
@@ -0,0 +1,136 @@
1
+ require "vipnet_parser/vipnet_config"
2
+
3
+ module VipnetParser
4
+ class Struct < VipnetConfig
5
+ attr_accessor :string, :hash
6
+
7
+ DEFAULT_PARSE_ARGS = {
8
+ format: :hash,
9
+ encoding: "cp866",
10
+ }
11
+
12
+ def parse(args = DEFAULT_PARSE_ARGS)
13
+ args = DEFAULT_PARSE_ARGS.merge(args)
14
+ format, encoding = args.values_at(
15
+ :format, :encoding, :normalize_names,
16
+ )
17
+
18
+ # Change encoding to utf8.
19
+ string = self.string
20
+ .force_encoding(encoding)
21
+ .encode("utf-8", replace: nil)
22
+
23
+ sign_aliases = { "+" => true, "-" => false }
24
+
25
+ case format
26
+ when :hash
27
+ @hash = { nodes: [] }
28
+
29
+ current_node_id = nil
30
+ previous_node = nil
31
+ current_usergroups = []
32
+ current_users = []
33
+ lines = string.split("\r\n")
34
+ lines.each_with_index do |line, line_index|
35
+ line_hash = _line_hash(line)
36
+ next unless line_hash
37
+
38
+ # Normalize data.
39
+ line_hash.each do |_, higher_name_hash|
40
+ next unless higher_name_hash.class == Hash
41
+
42
+ parsed_ids = VipnetParser.id(higher_name_hash[:id])
43
+ id =
44
+ if parsed_ids.size > 0
45
+ parsed_ids.first
46
+ else
47
+ nil
48
+ end
49
+ higher_name_hash[:id] = id
50
+
51
+ higher_name_hash[:name].rstrip!
52
+ if higher_name_hash[:name].size == 0
53
+ higher_name_hash[:name] = nil
54
+ end
55
+ end
56
+ line_hash[:node][:network_address].rstrip!
57
+ line_hash[:user][:sign] = sign_aliases[line_hash[:user][:sign]]
58
+ line_hash[:usergroup] = nil if (!line_hash[:usergroup][:id] && !line_hash[:usergroup][:name])
59
+
60
+ # first id
61
+ if !current_node_id && line_hash[:node][:id]
62
+ current_node_id = line_hash[:node][:id]
63
+ end
64
+
65
+ # initial previous node
66
+ if !previous_node
67
+ previous_node = line_hash[:node]
68
+ end
69
+
70
+ next_node_appears = (line_hash[:node][:id] && current_node_id != line_hash[:node][:id])
71
+ this_is_the_last_line = (line_index == lines.size - 1)
72
+
73
+ if this_is_the_last_line
74
+ current_usergroups.push(line_hash[:usergroup]) if line_hash[:usergroup]
75
+ current_users.push(line_hash[:user])
76
+ end
77
+
78
+ if next_node_appears || this_is_the_last_line
79
+ final_hash = previous_node
80
+ final_hash[:usergroups] = current_usergroups.dup
81
+ final_hash[:users] = current_users.dup
82
+ @hash[:nodes].push(final_hash)
83
+ end
84
+
85
+ if next_node_appears
86
+ current_node_id = line_hash[:node][:id]
87
+ previous_node = line_hash[:node]
88
+ current_usergroups = []
89
+ current_users = []
90
+ end
91
+
92
+ current_usergroups.push(line_hash[:usergroup]) if line_hash[:usergroup]
93
+ current_users.push(line_hash[:user])
94
+ end
95
+
96
+ @hash
97
+ end
98
+ end
99
+
100
+ def _line_hash(line)
101
+ # NOTE: could be done without regexp, but it seems easier with.
102
+ line_regexp = /^
103
+ (?<node>.{#{15 + 1 + 50 + 1 + 8}})│
104
+ (?<usergroup>.{#{50 + 1 + 8}})│
105
+ (?<user>.{#{56 + 1 + 8 + 1 + 1}})
106
+ $/x
107
+ node_regexp = /(?<network_address>.{15})│(?<name>.{50})│(?<id>.{8})/
108
+ usergroup_regexp = /(?<name>.{50})│(?<id>.{8})/
109
+ user_regexp = /(?<name>.{56})│(?<id>.{8})│(?<sign>.{1})/
110
+ regexps_map = {
111
+ node: node_regexp,
112
+ usergroup: usergroup_regexp,
113
+ user: user_regexp,
114
+ }
115
+
116
+ line_match = line_regexp.match(line)
117
+ return unless line_match
118
+ line_names = line_match.names.map { |name| name.to_sym }
119
+ line_hash = Hash[line_names.zip(line_match.captures)]
120
+
121
+ hash = {}
122
+ regexps_map.each do |higher_name_component, regexp|
123
+ match = regexp.match(line_hash[higher_name_component])
124
+ next unless match
125
+ names = match.names.map { |name| name.to_sym }
126
+ higher_name_hash = Hash[names.zip(match.captures)]
127
+
128
+ hash[higher_name_component] = higher_name_hash
129
+ end
130
+
131
+ hash
132
+ end
133
+
134
+ private :_line_hash
135
+ end
136
+ end
data/lib/vipnet_parser.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require "vipnet_parser/strings"
2
2
  require "vipnet_parser/iplirconf"
3
3
  require "vipnet_parser/nodename"
4
+ require "vipnet_parser/struct"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vipnet_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.6
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Morozov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-25 00:00:00.000000000 Z
11
+ date: 2018-04-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Parses ViPNet™ IDs, iplir.conf and other files
14
14
  email: ntcomp12@gmail.com
@@ -20,6 +20,7 @@ files:
20
20
  - lib/vipnet_parser/iplirconf.rb
21
21
  - lib/vipnet_parser/nodename.rb
22
22
  - lib/vipnet_parser/strings.rb
23
+ - lib/vipnet_parser/struct.rb
23
24
  - lib/vipnet_parser/vipnet_config.rb
24
25
  homepage: https://github.com/kengho/vipnet_parser
25
26
  licenses:
@@ -41,7 +42,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
42
  version: '0'
42
43
  requirements: []
43
44
  rubyforge_project:
44
- rubygems_version: 2.6.6
45
+ rubygems_version: 2.5.2.2
45
46
  signing_key:
46
47
  specification_version: 4
47
48
  summary: ViPNet™ strings parser