vipnet_parser 2.0.2 → 2.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7970f04e800c4708b908008d233b801145a23ba6
4
- data.tar.gz: ade58764a0627fbee189144d7891824b1394d447
3
+ metadata.gz: 0018f8a0e0d0d113c4e1e5c203a2a498e52575b6
4
+ data.tar.gz: 01d8e9fc145b3ea3ffa321ea8a5f5f99fa0df178
5
5
  SHA512:
6
- metadata.gz: db265c3b81ebfbfa511b289be370d6aa2e439e51042335495ca957b8dab16bee2a79581ac849da15753fe7070113668acb9265fbcae142fd5e8106f216d19be1
7
- data.tar.gz: 6fda0aa8af392f7a76d430fe0e29b744bd013ba9fff680f65ce43232e95dc1587c3b900f1363f27082c06151ca092f9e19988c9e69eb3044dcba05dd925fd0e7
6
+ metadata.gz: 93f780cb24ab89dbc740feadd338d97ff570cb13578f73dd0f7fa357363e3904f6cc30a1b821077098d821cf2cefdab2d2d501e73eb7242c6060f7489dbab3d1
7
+ data.tar.gz: fde5be2a5e48a79ee5bdc6d59bf99951c8df98143d9ad7c098355dddd3e7e651bfb57b90a7a5016031de5f61056e12ca211d2a74eb5b728e4e2d21cd00c7b3cf
@@ -8,25 +8,33 @@ module VipnetParser
8
8
  @string = iplirconf_file
9
9
  end
10
10
 
11
- DEFAULT_PARSE_ARGS = { format: :hash, encoding: "koi8-r", normalize_names: false }
11
+ DEFAULT_PARSE_ARGS = {
12
+ format: :hash,
13
+ encoding: "koi8-r",
14
+ normalize_names: false,
15
+ }
12
16
 
13
17
  def parse(args = DEFAULT_PARSE_ARGS)
14
18
  args = DEFAULT_PARSE_ARGS.merge(args)
15
- format, encoding, normalize_names = args.values_at(:format, :encoding, :normalize_names)
19
+ format, encoding, normalize_names = args.values_at(
20
+ :format, :encoding, :normalize_names,
21
+ )
16
22
 
17
- # change encoding to utf8 and remove comments
23
+ # Change encoding to utf8 and remove comments.
18
24
  string = self.string
19
- .force_encoding(encoding)
20
- .encode("utf-8")
21
- .gsub(/^#.*\n/, "")
22
- .gsub(/^;.*\n/, "")
25
+ .force_encoding(encoding)
26
+ .encode("utf-8")
27
+ .gsub(/^#.*\n/, "")
28
+ .gsub(/^;.*\n/, "")
23
29
 
24
30
  # "[id]something[server]something".split(/(?=\[.+\])/)
25
- # => ["[id]something", "[server]something"]
31
+ # =>
32
+ # ["[id]something", "[server]something"]
26
33
  string = string.split(/(?=\[.+\])/)
27
34
 
28
35
  # ["[id]something1", "[server]something2"]
29
- # => [
36
+ # =>
37
+ # [
30
38
  # { name: :id, content: "something1" },
31
39
  # { name: :server, content: "something2" },
32
40
  # ]
@@ -40,7 +48,7 @@ module VipnetParser
40
48
 
41
49
  case format
42
50
  when :hash
43
- @hash = { _meta: { version: "3" }}
51
+ @hash = {}
44
52
  hash_keys = {
45
53
  id: :id,
46
54
  adapter: :name,
@@ -53,26 +61,44 @@ module VipnetParser
53
61
  hash, current_key = _section_hash(section[:content], hash_key)
54
62
  @hash[section[:name]][current_key] = hash
55
63
 
56
- # normalize names
57
- # (only available for [id] sections, which are processed with current_key == id)
64
+ # Normalize names.
65
+ # (Only available for [id] sections, which are processed with current_key == id.)
58
66
  name = @hash[section[:name]][current_key][:name]
59
- if name && normalize_names
60
- @hash[section[:name]][current_key][:name] = VipnetParser.name(name, current_key)
61
- end
67
+ next unless name && normalize_names
68
+ name = VipnetParser.name(name, current_key)
69
+ @hash[section[:name]][current_key][:name] = name
62
70
  else
63
71
  hash, _ = _section_hash(section[:content])
64
72
  @hash[section[:name]] = hash
65
73
  end
66
74
  end
67
75
 
76
+ # Reduce [servers] section.
68
77
  # :servers => { :server => ["0x1a0e000a, coordinator1"] }
69
- # => :servers => ["0x1a0e000a, coordinator1"]
78
+ # =>
79
+ # :servers => ["0x1a0e000a, coordinator1"]
70
80
  @hash[:servers] = @hash[:servers][:server] || nil
71
81
 
72
- return @hash
82
+ @hash
73
83
  end
74
84
  end
75
85
 
86
+ # Returns config version.
87
+ # If misc => config_version isn't present, put "3",
88
+ # otherwise, get major version:
89
+ # "4.2.3-3" => "4".
90
+ def version
91
+ self.parse(format: :hash) unless self.hash
92
+ config_version = self.hash[:misc][:config_version]
93
+ parsed_config_version = if config_version
94
+ config_version[0]
95
+ else
96
+ "3"
97
+ end
98
+
99
+ parsed_config_version
100
+ end
101
+
76
102
  def _section_hash(section_content, hash_key = nil)
77
103
  hash = {}
78
104
 
@@ -81,8 +107,11 @@ module VipnetParser
81
107
  prop = Regexp.last_match(:prop).to_sym
82
108
  value = Regexp.last_match(:value)
83
109
 
84
- # array-type props
85
- if %i[ip filterudp filtertcp server].include?(prop)
110
+ array_props = %i(
111
+ ip filterudp filtertcp server
112
+ exclude_from_tunnels accessiplist subnet_real subnet_virtual
113
+ )
114
+ if array_props.include?(prop)
86
115
  if hash[prop]
87
116
  hash[prop].push(value)
88
117
  else
@@ -94,13 +123,11 @@ module VipnetParser
94
123
  end
95
124
  end
96
125
 
97
- if hash_key && hash[hash_key]
98
- current_key = hash[hash_key]
99
- hash.delete(hash_key)
100
- return [hash, current_key]
101
- else
102
- return hash
103
- end
126
+ return hash unless hash_key && hash[hash_key]
127
+ current_key = hash[hash_key]
128
+ hash.delete(hash_key)
129
+
130
+ [hash, current_key]
104
131
  end
105
132
 
106
133
  private :_section_hash
@@ -8,17 +8,26 @@ module VipnetParser
8
8
  @string = nodename_file
9
9
  end
10
10
 
11
- DEFAULT_PARSE_ARGS = { format: :hash, encoding: "cp866", normalize_names: false }
11
+ DEFAULT_PARSE_ARGS = {
12
+ format: :hash,
13
+ encoding: "cp866",
14
+ normalize_names: false,
15
+ }
12
16
 
13
17
  def parse(args = DEFAULT_PARSE_ARGS)
14
18
  args = DEFAULT_PARSE_ARGS.merge(args)
15
- format, encoding, normalize_names = args.values_at(:format, :encoding, :normalize_names)
19
+ format, encoding, normalize_names = args.values_at(
20
+ :format, :encoding, :normalize_names,
21
+ )
16
22
 
17
- # change encoding to utf8
23
+ # Change encoding to utf8.
18
24
  string = self.string
19
25
  .force_encoding(encoding)
20
26
  .encode("utf-8", replace: nil)
21
27
 
28
+ enable_aliases = { "1" => true, "0" => false }
29
+ group_aliases = { "A" => :client, "S" => :server, "G" => :group }
30
+
22
31
  case format
23
32
  when :hash
24
33
  @hash = { _meta: { version: "3" }, id: {} }
@@ -26,15 +35,17 @@ module VipnetParser
26
35
  string.split("\r\n").each do |line|
27
36
  record = _record_hash(line)
28
37
  record[:name].rstrip!
29
- record[:enabled] = { "1" => true, "0" => false }[record[:enabled]]
30
- record[:category] = { "A" => :client, "S" => :server, "G" => :group }[record[:category]]
38
+ record[:enabled] = enable_aliases[record[:enabled]]
39
+ record[:category] = group_aliases[record[:category]]
31
40
  normal_id = VipnetParser.id(record[:id]).first
32
- record[:name] = VipnetParser.name(record[:name], normal_id) if normalize_names
41
+ if normalize_names
42
+ record[:name] = VipnetParser.name(record[:name], normal_id)
43
+ end
33
44
  record.delete(:id)
34
45
  @hash[:id][normal_id] = record
35
46
  end
36
47
 
37
- return @hash
48
+ @hash
38
49
  end
39
50
  end
40
51
 
@@ -52,7 +63,7 @@ module VipnetParser
52
63
  names = match.names.map { |name| name.to_sym }
53
64
 
54
65
  # https://gist.github.com/flarnie/6221219
55
- return Hash[names.zip(match.captures)]
66
+ Hash[names.zip(match.captures)]
56
67
  end
57
68
 
58
69
  private :_record_hash
@@ -6,7 +6,10 @@ module VipnetParser
6
6
  elsif args.class == Hash
7
7
  string, array, threshold = args.values_at(:string, :array, :threshold)
8
8
  end
9
+ array ||= []
9
10
  string = string.downcase
11
+
12
+ # Substitute cyrillic symbols.
10
13
  cyrillic_sub = {
11
14
  "а" => "a", "б" => "b", "В" => "b", "с" => "c", "д" => "d", "е" => "e", "ф" => "f",
12
15
  "А" => "a", "Б" => "b", "в" => "b", "С" => "c", "Д" => "d", "Е" => "e", "Ф" => "f",
@@ -15,30 +18,28 @@ module VipnetParser
15
18
  string = string.gsub(cyr, lat)
16
19
  end
17
20
 
18
- array = [] unless array
19
21
  regexps = {
20
22
  /(.*)(0x[0-9a-f]{1,8}-0x[0-9a-f]{1,8})(.*)/m => method(:id_parse_variant1),
21
23
  /(.*)([0-9a-f]{8})(.*)/m => method(:id_parse_variant2),
22
24
  /(.*)0x([0-9a-f]{1,8})(.*)/m => method(:id_parse_variant3),
23
25
  }
26
+
24
27
  string_matches_anything = false
25
28
  regexps.each do |regexp, callback|
26
- if string =~ regexp && !string_matches_anything
27
- string_matches_anything = true
28
- array += callback.call(string: Regexp.last_match(2), threshold: threshold)
29
- [Regexp.last_match(1), Regexp.last_match(3)].each do |side_match|
30
- unless side_match.empty?
31
- array += id(string: side_match, array: array, threshold: threshold)
32
- end
29
+ next if string_matches_anything
30
+ next unless string =~ regexp
31
+ string_matches_anything = true
32
+
33
+ array += callback.call(string: Regexp.last_match(2), threshold: threshold)
34
+ [Regexp.last_match(1), Regexp.last_match(3)].each do |side_match|
35
+ unless side_match.empty?
36
+ array += id(string: side_match, array: array, threshold: threshold)
33
37
  end
34
38
  end
35
39
  end
40
+ return [] unless string_matches_anything
36
41
 
37
- if string_matches_anything
38
- return array.uniq.sort
39
- else
40
- return []
41
- end
42
+ array.uniq.sort
42
43
  end
43
44
 
44
45
  def self.id_parse_variant1(args)
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.0.2
4
+ version: 2.2.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-01-17 00:00:00.000000000 Z
11
+ date: 2017-01-25 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