iniparse 1.1.6 → 1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b3fc88cab6fd9b214be0c4baa4b5d3b6729add3e
4
+ data.tar.gz: 006036392b16391ab8b80394b22607975144cf73
5
+ SHA512:
6
+ metadata.gz: 83b9c5fb87539797e3cda761063bbbc5d7c26fe5eda392d7d19d60e99e7f5f2833e5744887bef07ed6bc3d2c474337922334b12b1c9e8526dda00a6a8e9eba62
7
+ data.tar.gz: a4858ba30df58a5bf5c10a9928f6ccaf44bc7f9ade77f8e53effed28b31c4230f4d63391231ff5180c532fe8dfab6a16465774e82c1d4f1809a87158d1151e9c
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
  gemspec
data/README.rdoc CHANGED
@@ -64,6 +64,22 @@ be supplied to you.
64
64
  document['my_section']['key']
65
65
  # => ['value', 'another value', 'third value']
66
66
 
67
+ Options which appear before the first section will be added to a section called
68
+ "__anonymous__".
69
+
70
+ document = IniParse.parse <<-EOS
71
+ driver = true
72
+
73
+ [driver]
74
+ key = value
75
+ EOS
76
+
77
+ document['__anonymous__']['driver']
78
+ # => true
79
+
80
+ document['driver']['key']
81
+ # => 'value'
82
+
67
83
  === Updating an INI file
68
84
 
69
85
  document['a_section']['an_option']
data/Rakefile CHANGED
@@ -85,7 +85,7 @@ task :gemspec => :validate do
85
85
  split("\n").
86
86
  sort.
87
87
  reject { |file| file =~ /^\./ }.
88
- reject { |file| file =~ /^(rdoc|pkg)/ }.
88
+ reject { |file| file =~ /^(rdoc|pkg|spec)/ }.
89
89
  map { |file| " #{file}" }.
90
90
  join("\n")
91
91
 
data/iniparse.gemspec CHANGED
@@ -12,14 +12,19 @@ Gem::Specification.new do |s|
12
12
  ## If your rubyforge_project name is different, then edit it and comment out
13
13
  ## the sub! line in the Rakefile
14
14
  s.name = 'iniparse'
15
- s.version = '1.1.6'
16
- s.date = '2012-11-07'
15
+ s.version = '1.2.0'
16
+ s.date = '2014-04-08'
17
17
  s.rubyforge_project = 'iniparse'
18
18
 
19
19
  s.summary = 'A pure Ruby library for parsing INI documents.'
20
20
  s.authors = ['Anthony Williams']
21
21
  s.email = 'hi@antw.me'
22
22
  s.homepage = 'http://github.com/antw/iniparse'
23
+ s.licenses = ['MIT']
24
+
25
+ s.description = 'A pure Ruby library for parsing INI documents. ' \
26
+ 'Preserves the structure of the original document, ' \
27
+ 'including whitespace and comments'
23
28
 
24
29
  s.require_paths = %w(lib)
25
30
 
@@ -27,7 +32,7 @@ Gem::Specification.new do |s|
27
32
  s.extra_rdoc_files = %w(History LICENSE README.rdoc)
28
33
 
29
34
  # Dependencies.
30
- s.add_development_dependency('rspec', '>= 2.11.0')
35
+ s.add_development_dependency('rspec', '~> 2.14')
31
36
 
32
37
  # = MANIFEST =
33
38
  s.files = %w[
@@ -44,24 +49,6 @@ Gem::Specification.new do |s|
44
49
  lib/iniparse/line_collection.rb
45
50
  lib/iniparse/lines.rb
46
51
  lib/iniparse/parser.rb
47
- spec/document_spec.rb
48
- spec/fixture_spec.rb
49
- spec/fixtures/openttd.ini
50
- spec/fixtures/race07.ini
51
- spec/fixtures/smb.ini
52
- spec/generator/method_missing_spec.rb
53
- spec/generator/with_section_blocks_spec.rb
54
- spec/generator/without_section_blocks_spec.rb
55
- spec/iniparse_spec.rb
56
- spec/line_collection_spec.rb
57
- spec/lines_spec.rb
58
- spec/parser/document_parsing_spec.rb
59
- spec/parser/line_parsing_spec.rb
60
- spec/spec_fixtures.rb
61
- spec/spec_helper.rb
62
- spec/spec_helper_spec.rb
63
52
  ]
64
53
  # = MANIFEST =
65
-
66
- s.test_files = s.files.select { |path| path =~ /^spec\/.*\.rb/ }
67
54
  end
data/lib/iniparse.rb CHANGED
@@ -7,7 +7,7 @@ require File.join(dir, 'lines')
7
7
  require File.join(dir, 'parser')
8
8
 
9
9
  module IniParse
10
- VERSION = '1.1.6'
10
+ VERSION = '1.2.0'
11
11
 
12
12
  # A base class for IniParse errors.
13
13
  class IniParseError < StandardError; end
@@ -110,8 +110,10 @@ module IniParse
110
110
 
111
111
  def <<(line)
112
112
  if line.kind_of?(IniParse::Lines::Option)
113
- raise IniParse::LineNotAllowed,
114
- "You can't add an Option to a SectionCollection."
113
+ option = line
114
+ line = IniParse::Lines::AnonymousSection.new
115
+
116
+ line.lines << option if option
115
117
  end
116
118
 
117
119
  if line.blank? || (! has_key?(line.key))
@@ -8,6 +8,7 @@ module IniParse
8
8
  def initialize(opts = {})
9
9
  @comment = opts.fetch(:comment, nil)
10
10
  @comment_sep = opts.fetch(:comment_sep, ';')
11
+ @comment_prefix = opts.fetch(:comment_prefix, ' ')
11
12
  @comment_offset = opts.fetch(:comment_offset, 0)
12
13
  @indent = opts.fetch(:indent, '')
13
14
  end
@@ -40,7 +41,7 @@ module IniParse
40
41
  # Returns the inline comment for this line. Includes the comment
41
42
  # separator at the beginning of the string.
42
43
  def comment
43
- '%s %s' % [@comment_sep, @comment]
44
+ "#{ @comment_sep }#{ @comment_prefix }#{ @comment }"
44
45
  end
45
46
 
46
47
  # Returns whether this is a line which has no data.
@@ -192,6 +193,28 @@ module IniParse
192
193
  end
193
194
  end
194
195
 
196
+ # Stores options which appear at the beginning of a file, without a
197
+ # preceding section.
198
+ class AnonymousSection < Section
199
+ def initialize
200
+ super('__anonymous__')
201
+ end
202
+
203
+ def to_ini
204
+ # Remove the leading space which is added by joining the blank line
205
+ # content with the options.
206
+ super.gsub(/\A\n/, '')
207
+ end
208
+
209
+ #######
210
+ private
211
+ #######
212
+
213
+ def line_contents
214
+ ''
215
+ end
216
+ end
217
+
195
218
  # Represents probably the most common type of line in an INI document:
196
219
  # an option. Consists of a key and value, usually separated with an =.
197
220
  #
@@ -77,9 +77,10 @@ module IniParse
77
77
  # Strips in inline comment from a line (or value), removes trailing
78
78
  # whitespace and sets the comment options as applicable.
79
79
  def strip_comment(line, opts)
80
- if m = /^(^)(?:(;|\#)\s*(.*))$$/.match(line) ||
81
- m = /^(.*?)(?:\s+(;|\#)\s*(.*))$/.match(line) # Comment lines.
82
- opts[:comment] = m[3].rstrip
80
+ if m = /^(^)(?:(;|\#)(\s*)(.*))$$/.match(line) ||
81
+ m = /^(.*?)(?:\s+(;|\#)(\s*)(.*))$/.match(line) # Comment lines.
82
+ opts[:comment] = m[4].rstrip
83
+ opts[:comment_prefix] = m[3]
83
84
  opts[:comment_sep] = m[2]
84
85
  # Remove the line content (since an option value may contain a
85
86
  # semi-colon) _then_ get the index of the comment separator.
metadata CHANGED
@@ -1,33 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iniparse
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.6
5
- prerelease:
4
+ version: 1.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Anthony Williams
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-07 00:00:00.000000000 Z
11
+ date: 2014-04-08 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
- version: 2.11.0
19
+ version: '2.14'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: 2.11.0
30
- description:
26
+ version: '2.14'
27
+ description: A pure Ruby library for parsing INI documents. Preserves the structure
28
+ of the original document, including whitespace and comments
31
29
  email: hi@antw.me
32
30
  executables: []
33
31
  extensions: []
@@ -49,59 +47,30 @@ files:
49
47
  - lib/iniparse/line_collection.rb
50
48
  - lib/iniparse/lines.rb
51
49
  - lib/iniparse/parser.rb
52
- - spec/document_spec.rb
53
- - spec/fixture_spec.rb
54
- - spec/fixtures/openttd.ini
55
- - spec/fixtures/race07.ini
56
- - spec/fixtures/smb.ini
57
- - spec/generator/method_missing_spec.rb
58
- - spec/generator/with_section_blocks_spec.rb
59
- - spec/generator/without_section_blocks_spec.rb
60
- - spec/iniparse_spec.rb
61
- - spec/line_collection_spec.rb
62
- - spec/lines_spec.rb
63
- - spec/parser/document_parsing_spec.rb
64
- - spec/parser/line_parsing_spec.rb
65
- - spec/spec_fixtures.rb
66
- - spec/spec_helper.rb
67
- - spec/spec_helper_spec.rb
68
50
  homepage: http://github.com/antw/iniparse
69
- licenses: []
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
70
54
  post_install_message:
71
55
  rdoc_options:
72
- - --charset=UTF-8
56
+ - "--charset=UTF-8"
73
57
  require_paths:
74
58
  - lib
75
59
  required_ruby_version: !ruby/object:Gem::Requirement
76
- none: false
77
60
  requirements:
78
- - - ! '>='
61
+ - - ">="
79
62
  - !ruby/object:Gem::Version
80
63
  version: '0'
81
64
  required_rubygems_version: !ruby/object:Gem::Requirement
82
- none: false
83
65
  requirements:
84
- - - ! '>='
66
+ - - ">="
85
67
  - !ruby/object:Gem::Version
86
68
  version: '0'
87
69
  requirements: []
88
70
  rubyforge_project: iniparse
89
- rubygems_version: 1.8.23
71
+ rubygems_version: 2.2.2
90
72
  signing_key:
91
73
  specification_version: 2
92
74
  summary: A pure Ruby library for parsing INI documents.
93
- test_files:
94
- - spec/document_spec.rb
95
- - spec/fixture_spec.rb
96
- - spec/generator/method_missing_spec.rb
97
- - spec/generator/with_section_blocks_spec.rb
98
- - spec/generator/without_section_blocks_spec.rb
99
- - spec/iniparse_spec.rb
100
- - spec/line_collection_spec.rb
101
- - spec/lines_spec.rb
102
- - spec/parser/document_parsing_spec.rb
103
- - spec/parser/line_parsing_spec.rb
104
- - spec/spec_fixtures.rb
105
- - spec/spec_helper.rb
106
- - spec/spec_helper_spec.rb
75
+ test_files: []
107
76
  has_rdoc:
@@ -1,88 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "IniParse::Document" do
4
- it 'should have a +lines+ reader' do
5
- methods = IniParse::Document.instance_methods.map { |m| m.to_sym }
6
- methods.should include(:lines)
7
- end
8
-
9
- it 'should not have a +lines+ writer' do
10
- methods = IniParse::Document.instance_methods.map { |m| m.to_sym }
11
- methods.should_not include(:lines=)
12
- end
13
-
14
- it 'should delegate #[] to +lines+' do
15
- doc = IniParse::Document.new
16
- doc.lines.should_receive(:[]).with('key')
17
- doc['key']
18
- end
19
-
20
- it 'should call #each to +lines+' do
21
- doc = IniParse::Document.new
22
- doc.lines.should_receive(:each)
23
- doc.each { |l| }
24
- end
25
-
26
- it 'should be enumerable' do
27
- IniParse::Document.included_modules.should include(Enumerable)
28
-
29
- sections = [
30
- IniParse::Lines::Section.new('first section'),
31
- IniParse::Lines::Section.new('second section')
32
- ]
33
-
34
- doc = IniParse::Document.new
35
- doc.lines << sections[0] << sections[1]
36
-
37
- doc.map { |line| line }.should == sections
38
- end
39
-
40
- describe '#has_section?' do
41
- before(:all) do
42
- @doc = IniParse::Document.new
43
- @doc.lines << IniParse::Lines::Section.new('first section')
44
- end
45
-
46
- it 'should return true if a section with the given key exists' do
47
- @doc.should have_section('first section')
48
- end
49
-
50
- it 'should return true if no section with the given key exists' do
51
- @doc.should_not have_section('second section')
52
- end
53
- end
54
-
55
- describe '#save' do
56
- describe 'when no path is given to save' do
57
- it 'should save the INI document if a path was given when initialized' do
58
- doc = IniParse::Document.new('/a/path/to/a/file.ini')
59
- File.should_receive(:open).with('/a/path/to/a/file.ini', 'w')
60
- doc.save
61
- end
62
-
63
- it 'should raise IniParseError if no path was given when initialized' do
64
- lambda { IniParse::Document.new.save }.should \
65
- raise_error(IniParse::IniParseError)
66
- end
67
- end
68
-
69
- describe 'when a path is given to save' do
70
- it "should update the document's +path+" do
71
- File.stub!(:open).and_return(true)
72
- doc = IniParse::Document.new('/a/path/to/a/file.ini')
73
- doc.save('/a/new/path.ini')
74
- doc.path.should == '/a/new/path.ini'
75
- end
76
-
77
- it 'should save the INI document to the given path' do
78
- File.should_receive(:open).with('/a/new/path.ini', 'w')
79
- IniParse::Document.new('/a/path/to/a/file.ini').save('/a/new/path.ini')
80
- end
81
-
82
- it 'should raise IniParseError if no path was given when initialized' do
83
- lambda { IniParse::Document.new.save }.should \
84
- raise_error(IniParse::IniParseError)
85
- end
86
- end
87
- end
88
- end
data/spec/fixture_spec.rb DELETED
@@ -1,166 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "IniParse" do
4
- describe 'openttd.ini fixture' do
5
- before(:all) do
6
- @fixture = fixture('openttd.ini')
7
- end
8
-
9
- it 'should parse without any errors' do
10
- lambda { IniParse.parse(@fixture) }.should_not raise_error
11
- end
12
-
13
- it 'should have the correct sections' do
14
- IniParse.parse(fixture('openttd.ini')).lines.keys.should == [
15
- 'misc', 'music', 'difficulty', 'game_creation', 'vehicle',
16
- 'construction', 'station', 'economy', 'pf', 'order', 'gui', 'ai',
17
- 'locale', 'network', 'currency', 'servers', 'bans', 'news_display',
18
- 'version', 'preset-J', 'newgrf', 'newgrf-static'
19
- ]
20
- end
21
-
22
- it 'should have the correct options' do
23
- # Test the keys from one section.
24
- doc = IniParse.parse(@fixture)
25
- section = doc['misc']
26
-
27
- section.lines.keys.should == [
28
- 'display_opt', 'news_ticker_sound', 'fullscreen', 'language',
29
- 'resolution', 'screenshot_format', 'savegame_format',
30
- 'rightclick_emulate', 'small_font', 'medium_font', 'large_font',
31
- 'small_size', 'medium_size', 'large_size', 'small_aa', 'medium_aa',
32
- 'large_aa', 'sprite_cache_size', 'player_face',
33
- 'transparency_options', 'transparency_locks', 'invisibility_options',
34
- 'keyboard', 'keyboard_caps'
35
- ]
36
-
37
- # Test some of the options.
38
- section['display_opt'].should == 'SHOW_TOWN_NAMES|SHOW_STATION_NAMES|SHOW_SIGNS|FULL_ANIMATION|FULL_DETAIL|WAYPOINTS'
39
- section['news_ticker_sound'].should be_false
40
- section['language'].should == 'english_US.lng'
41
- section['resolution'].should == '1680,936'
42
- section['large_size'].should == 16
43
-
44
- # Test some other options.
45
- doc['currency']['suffix'].should == '" credits"'
46
- doc['news_display']['production_nobody'].should == 'summarized'
47
- doc['version']['version_number'].should == '070039B0'
48
-
49
- doc['preset-J']['gcf/1_other/BlackCC/mauvetoblackw.grf'].should be_nil
50
- doc['preset-J']['gcf/1_other/OpenGFX/OpenGFX_-_newFaces_v0.1.grf'].should be_nil
51
- end
52
-
53
- it 'should be identical to the original when calling #to_ini' do
54
- IniParse.parse(@fixture).to_ini.should == @fixture
55
- end
56
- end
57
-
58
- describe 'race07.ini fixture' do
59
- before(:all) do
60
- @fixture = fixture('race07.ini')
61
- end
62
-
63
- it 'should parse without any errors' do
64
- lambda { IniParse.parse(@fixture) }.should_not raise_error
65
- end
66
-
67
- it 'should have the correct sections' do
68
- IniParse.parse(fixture('race07.ini')).lines.keys.should == [
69
- 'Header', 'Race', 'Slot010', 'Slot016', 'Slot013', 'Slot018',
70
- 'Slot002', 'END'
71
- ]
72
- end
73
-
74
- it 'should have the correct options' do
75
- # Test the keys from one section.
76
- doc = IniParse.parse(@fixture)
77
- section = doc['Slot010']
78
-
79
- section.lines.keys.should == [
80
- 'Driver', 'SteamUser', 'SteamId', 'Vehicle', 'Team', 'QualTime',
81
- 'Laps', 'Lap', 'LapDistanceTravelled', 'BestLap', 'RaceTime'
82
- ]
83
-
84
- # Test some of the options.
85
- section['Driver'].should == 'Mark Voss'
86
- section['SteamUser'].should == 'mvoss'
87
- section['SteamId'].should == 1865369
88
- section['Vehicle'].should == 'Chevrolet Lacetti 2007'
89
- section['Team'].should == 'TEMPLATE_TEAM'
90
- section['QualTime'].should == '1:37.839'
91
- section['Laps'].should == 13
92
- section['LapDistanceTravelled'].should == 3857.750244
93
- section['BestLap'].should == '1:38.031'
94
- section['RaceTime'].should == '0:21:38.988'
95
-
96
- section['Lap'].should == [
97
- '(0, -1.000, 1:48.697)', '(1, 89.397, 1:39.455)',
98
- '(2, 198.095, 1:38.060)', '(3, 297.550, 1:38.632)',
99
- '(4, 395.610, 1:38.031)', '(5, 494.242, 1:39.562)',
100
- '(6, 592.273, 1:39.950)', '(7, 691.835, 1:38.366)',
101
- '(8, 791.785, 1:39.889)', '(9, 890.151, 1:39.420)',
102
- '(10, 990.040, 1:39.401)', '(11, 1089.460, 1:39.506)',
103
- '(12, 1188.862, 1:40.017)'
104
- ]
105
-
106
- doc['Header']['Version'].should == '1.1.1.14'
107
- doc['Header']['TimeString'].should == '2008/09/13 23:26:32'
108
- doc['Header']['Aids'].should == '0,0,0,0,0,1,1,0,0'
109
-
110
- doc['Race']['AIDB'].should == 'GameData\Locations\Anderstorp_2007\2007_ANDERSTORP.AIW'
111
- doc['Race']['Race Length'].should == 0.1
112
- end
113
-
114
- it 'should be identical to the original when calling #to_ini' do
115
- pending('awaiting presevation (or lack) of whitespace around =') do
116
- IniParse.parse(@fixture).to_ini.should == @fixture
117
- end
118
- end
119
- end
120
-
121
- describe 'smb.ini fixture' do
122
- before(:all) do
123
- @fixture = fixture('smb.ini')
124
- end
125
-
126
- it 'should parse without any errors' do
127
- lambda { IniParse.parse(@fixture) }.should_not raise_error
128
- end
129
-
130
- it 'should have the correct sections' do
131
- IniParse.parse(@fixture).lines.keys.should == [
132
- 'global', 'printers'
133
- ]
134
- end
135
-
136
- it 'should have the correct options' do
137
- # Test the keys from one section.
138
- doc = IniParse.parse(@fixture)
139
- section = doc['global']
140
-
141
- section.lines.keys.should == [
142
- 'debug pid', 'log level', 'server string', 'printcap name',
143
- 'printing', 'encrypt passwords', 'use spnego', 'passdb backend',
144
- 'idmap domains', 'idmap config default: default',
145
- 'idmap config default: backend', 'idmap alloc backend',
146
- 'idmap negative cache time', 'map to guest', 'guest account',
147
- 'unix charset', 'display charset', 'dos charset', 'vfs objects',
148
- 'os level', 'domain master', 'max xmit', 'use sendfile',
149
- 'stream support', 'ea support', 'darwin_streams:brlm',
150
- 'enable core files', 'usershare max shares', 'usershare path',
151
- 'usershare owner only', 'usershare allow guests',
152
- 'usershare allow full config', 'com.apple:filter shares by access',
153
- 'obey pam restrictions', 'acl check permissions',
154
- 'name resolve order', 'include'
155
- ]
156
-
157
- section['display charset'].should == 'UTF-8-MAC'
158
- section['vfs objects'].should == 'darwinacl,darwin_streams'
159
- section['usershare path'].should == '/var/samba/shares'
160
- end
161
-
162
- it 'should be identical to the original when calling #to_ini' do
163
- IniParse.parse(@fixture).to_ini.should == @fixture
164
- end
165
- end
166
- end