configparser 0.1.6 → 0.1.7

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: 963a2aeb71783101be9ff9da4eae8ab8f237471e
4
- data.tar.gz: 6280b7525c83c1d1d3a1723c0af4be3c7ee88ae3
3
+ metadata.gz: eb8bc149e5a58829a6df0e783ac6af630c27f44e
4
+ data.tar.gz: a3d86fc549c8b27e45f8a0a0e0efc5ee2cea2cb2
5
5
  SHA512:
6
- metadata.gz: 56a5b36b5ba0bf411945af98586dc0d9a0cc33f3d7157e3277191cfbd5222689c0e41c14c27fb983db78822c3e673bb642283d501c94ec4b05080c6493f14ba0
7
- data.tar.gz: ac46e308fdd5e3387b325f91982c7b637559374bb4036d00d32dc6b444dab15a271bd50633e0316042f0fa8b1c33524afd042b2e1672fef0e80ba5d6758f79d1
6
+ metadata.gz: 5687f331eb4fe910ed142de3140db0a5b903876dd1adef3fdd0cc905ff8a8e7591dae8398336f252b4efd7eb760969013e70264e7aef1494598ee3e666698747
7
+ data.tar.gz: 19ba03f7b1ad9b6476a1a70631fccab8f398ec840a4d8bf3b2b6f8467c5efec4915adcd5456d56a47693137b45ec341ef6e6b0ae1b116e69a029d6546ffcc5f5
@@ -18,28 +18,56 @@ class ConfigParser < Hash
18
18
  if section
19
19
  self[section] = {} unless self[section]
20
20
  key = $1
21
- self[section][key] = $2
21
+ if self[section][key]
22
+ self[section][key] = [self[section][key]] unless self[section][key].is_a?(Array)
23
+ self[section][key] << $2
24
+ else
25
+ self[section][key] = $2
26
+ end
22
27
  else
23
28
  key = $1
24
- self[key] = $2
29
+ if self[key]
30
+ self[key] = [self[key]] unless self[key].is_a?(Array)
31
+ self[key] << $2
32
+ else
33
+ self[key] = $2
34
+ end
25
35
  end
26
36
  elsif line =~ /^\s*\[(.+?)\]/ # handle new sections
27
37
  section = $1
28
- self[section] = {}
38
+ self[section] = {} unless self[section]
29
39
  elsif line =~ /^\s+(.+?)$/ # handle continued lines
30
40
  if section
31
- self[section][key] += " #{$1}";
41
+ if self[section][key].is_a?(Array)
42
+ self[section][key].last << " #{$1}";
43
+ else
44
+ self[section][key] << " #{$1}";
45
+ end
32
46
  else
33
- self[key] += " #{$1}"
47
+ if self[key].is_a?(Array)
48
+ self[key].last << " #{$1}"
49
+ else
50
+ self[key] << " #{$1}"
51
+ end
34
52
  end
35
53
  elsif line =~ /^([\w\d\_\-]+)$/
36
54
  if section
37
55
  self[section] = {} unless self[section]
38
56
  key = $1
39
- self[section][key] = true
57
+ if self[section][key]
58
+ self[section][key] = [self[section][key]] unless self[section][key].is_a?(Array)
59
+ self[section][key] << true
60
+ else
61
+ self[section][key] = true
62
+ end
40
63
  else
41
64
  key = $1
42
- self[key] = true
65
+ if self[key]
66
+ self[key] = [self[key]] unless self[key].is_a?(Array)
67
+ self[key] << true
68
+ else
69
+ self[key] = true
70
+ end
43
71
  end
44
72
  end
45
73
  end
@@ -80,27 +108,37 @@ class ConfigParser < Hash
80
108
  # print globals first
81
109
  self.keys.sort.each do |k|
82
110
  next if self[k].is_a? Hash
83
- if self[k] === true
84
- str << "#{k}\n"
85
- elsif self[k] == ""
86
- str << "#{k}#{sep}\n"
87
- else
88
- str << "#{k}#{sep} #{self[k]}\n"
89
- end
111
+ if not self[k].is_a?(Array)
112
+ self[k] = [self[k]]
113
+ end
114
+ self[k].each do |v|
115
+ if v === true
116
+ str << "#{k}\n"
117
+ elsif v == ""
118
+ str << "#{k}#{sep}\n"
119
+ else
120
+ str << "#{k}#{sep} #{v}\n"
121
+ end
122
+ end
90
123
  end
91
124
 
92
125
  # now print the sections
93
126
  self.keys.sort.each do |k|
94
- next unless self[k].is_a? Hash
95
- str << "[#{k}]\n"
96
- self[k].keys.sort.each do |j|
97
- if self[k][j] === true
98
- str << "#{j}\n"
99
- elsif self[k][j] == ""
100
- str << "#{j}#{sep}\n"
101
- else
102
- str << "#{j}#{sep} #{self[k][j]}\n"
103
- end
127
+ next unless self[k].is_a? Hash
128
+ str << "[#{k}]\n"
129
+ self[k].sort.each do |j,v|
130
+ if not v.is_a?(Array)
131
+ v = [v]
132
+ end
133
+ v.each do |v2|
134
+ if v2 === true
135
+ str << "#{j}\n"
136
+ elsif v2 == ""
137
+ str << "#{j}#{sep}\n"
138
+ else
139
+ str << "#{j}#{sep} #{v2}\n"
140
+ end
141
+ end
104
142
  end
105
143
  end
106
144
  str
@@ -1,3 +1,3 @@
1
1
  class Configparser
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
@@ -170,4 +170,42 @@ foo: bar
170
170
  "
171
171
  assert_equal(doc, cp.to_s)
172
172
  end
173
+
174
+ def test_duplicate_sections
175
+ content = <<end_of_duplicate_section_example
176
+ [test1]
177
+ this=is a test
178
+ [test2]
179
+ pluto=is a dwarf planet
180
+ [test1]
181
+ redundant=am I repeating myself?
182
+ end_of_duplicate_section_example
183
+ answer = <<end_of_answer
184
+ [test1]
185
+ redundant: am I repeating myself?
186
+ this: is a test
187
+ [test2]
188
+ pluto: is a dwarf planet
189
+ end_of_answer
190
+ cp = ConfigParser.new()
191
+ cp.parse(content.each_line)
192
+ assert_equal(answer, cp.to_s)
193
+ end
194
+
195
+ def test_duplicate_options
196
+ content = <<end_of_duplicate_options_example
197
+ [test1]
198
+ this=is a test
199
+ this=is another test
200
+ end_of_duplicate_options_example
201
+ answer = <<end_of_answer
202
+ [test1]
203
+ this: is a test
204
+ this: is another test
205
+ end_of_answer
206
+ cp = ConfigParser.new()
207
+ cp.parse(content.each_line)
208
+ assert_equal(answer, cp.to_s)
209
+ end
210
+
173
211
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: configparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - chrislee35
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-07 00:00:00.000000000 Z
11
+ date: 2017-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest