ini-phile 0.1.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.
- data/lib/ini-phile.rb +262 -0
- metadata +45 -0
data/lib/ini-phile.rb
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
#
|
|
2
|
+
# This class represents the INI file and can be used to parse, modify,
|
|
3
|
+
# and write INI files.
|
|
4
|
+
#
|
|
5
|
+
class Ini
|
|
6
|
+
|
|
7
|
+
# :stopdoc:
|
|
8
|
+
class Error < StandardError; end
|
|
9
|
+
# :startdoc:
|
|
10
|
+
|
|
11
|
+
#
|
|
12
|
+
# call-seq:
|
|
13
|
+
# IniFile.load( filename )
|
|
14
|
+
# IniFile.load( filename, options )
|
|
15
|
+
#
|
|
16
|
+
# Open the given _filename_ and load the contetns of the INI file.
|
|
17
|
+
# The following _options_ can be passed to this method:
|
|
18
|
+
#
|
|
19
|
+
# :comment => ';' The line comment character(s)
|
|
20
|
+
# :parameter => '=' The parameter / value separator
|
|
21
|
+
#
|
|
22
|
+
def self.load( filename, opts = {} )
|
|
23
|
+
new(filename, opts)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
#
|
|
27
|
+
# call-seq:
|
|
28
|
+
# IniFile.new( filename )
|
|
29
|
+
# IniFile.new( filename, options )
|
|
30
|
+
#
|
|
31
|
+
# Create a new INI file using the given _filename_. If _filename_
|
|
32
|
+
# exists and is a regular file, then its contents will be parsed.
|
|
33
|
+
# The following _options_ can be passed to this method:
|
|
34
|
+
#
|
|
35
|
+
# :comment => ';' The line comment character(s)
|
|
36
|
+
# :parameter => '=' The parameter / value separator
|
|
37
|
+
#
|
|
38
|
+
def initialize( filename, opts = {} )
|
|
39
|
+
@fn = filename
|
|
40
|
+
@comment = opts[:comment] || ';'
|
|
41
|
+
@param = opts[:parameter] || '='
|
|
42
|
+
@ini = Hash.new {|h,k| h[k] = Hash.new}
|
|
43
|
+
|
|
44
|
+
@rgxp_comment = %r/\A\s*\z|\A\s*[#{@comment}]/
|
|
45
|
+
@rgxp_section = %r/\A\s*\[([^\]]+)\]/o
|
|
46
|
+
@rgxp_param = %r/\A([^#{@param}]+)#{@param}(.*)\z/
|
|
47
|
+
|
|
48
|
+
parse
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
#
|
|
52
|
+
# call-seq:
|
|
53
|
+
# write
|
|
54
|
+
# write( filename )
|
|
55
|
+
#
|
|
56
|
+
# Write the INI file contents to the filesystem. The given _filename_
|
|
57
|
+
# will be used to write the file. If _filename_ is not given, then the
|
|
58
|
+
# named used when constructing this object will be used.
|
|
59
|
+
#
|
|
60
|
+
def write( filename = nil )
|
|
61
|
+
@fn = filename unless filename.nil?
|
|
62
|
+
|
|
63
|
+
::File.open(@fn, 'w') do |f|
|
|
64
|
+
@ini.each do |section,hash|
|
|
65
|
+
f.puts "[#{section}]"
|
|
66
|
+
hash.each {|param,val| f.puts "#{param} #{@param} #{val}"}
|
|
67
|
+
f.puts
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
self
|
|
71
|
+
end
|
|
72
|
+
alias :save :write
|
|
73
|
+
|
|
74
|
+
#
|
|
75
|
+
# call-seq:
|
|
76
|
+
# each {|section, parameter, value| block}
|
|
77
|
+
#
|
|
78
|
+
# Yield each _section_, _parameter_, _value_ in turn to the given
|
|
79
|
+
# _block_. The method returns immediately if no block is supplied.
|
|
80
|
+
#
|
|
81
|
+
def each
|
|
82
|
+
return unless block_given?
|
|
83
|
+
@ini.each do |section,hash|
|
|
84
|
+
hash.each do |param,val|
|
|
85
|
+
yield section, param, val
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
self
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
#
|
|
92
|
+
# call-seq:
|
|
93
|
+
# each_section {|section| block}
|
|
94
|
+
#
|
|
95
|
+
# Yield each _section_ in turn to the given _block_. The method returns
|
|
96
|
+
# immediately if no block is supplied.
|
|
97
|
+
#
|
|
98
|
+
def each_section
|
|
99
|
+
return unless block_given?
|
|
100
|
+
@ini.each_key {|section| yield section}
|
|
101
|
+
self
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
#
|
|
105
|
+
# call-seq:
|
|
106
|
+
# delete_section( section )
|
|
107
|
+
#
|
|
108
|
+
# Deletes the named _section_ from the INI file. Returns the
|
|
109
|
+
# parameter / value pairs if the section exists in the INI file. Otherwise,
|
|
110
|
+
# returns +nil+.
|
|
111
|
+
#
|
|
112
|
+
def delete_section( section )
|
|
113
|
+
@ini.delete section.to_s
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
#
|
|
117
|
+
# call-seq:
|
|
118
|
+
# ini_file[section]
|
|
119
|
+
#
|
|
120
|
+
# Get the hash of parameter/value pairs for the given _section_. If the
|
|
121
|
+
# _section_ hash does not exist it will be created.
|
|
122
|
+
#
|
|
123
|
+
def []( section )
|
|
124
|
+
return nil if section.nil?
|
|
125
|
+
@ini[section.to_s]
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
#
|
|
129
|
+
# call-seq:
|
|
130
|
+
# has_section?( section )
|
|
131
|
+
#
|
|
132
|
+
# Returns +true+ if the named _section_ exists in the INI file.
|
|
133
|
+
#
|
|
134
|
+
def has_section?( section )
|
|
135
|
+
@ini.has_key? section.to_s
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
#
|
|
139
|
+
# call-seq:
|
|
140
|
+
# sections
|
|
141
|
+
#
|
|
142
|
+
# Returns an array of the section names.
|
|
143
|
+
#
|
|
144
|
+
def sections
|
|
145
|
+
@ini.keys
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
#
|
|
149
|
+
# call-seq:
|
|
150
|
+
# freeze
|
|
151
|
+
#
|
|
152
|
+
# Freeze the state of the +IniFile+ object. Any attempts to change the
|
|
153
|
+
# object will raise an error.
|
|
154
|
+
#
|
|
155
|
+
def freeze
|
|
156
|
+
super
|
|
157
|
+
@ini.each_value {|h| h.freeze}
|
|
158
|
+
@ini.freeze
|
|
159
|
+
self
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
#
|
|
163
|
+
# call-seq:
|
|
164
|
+
# taint
|
|
165
|
+
#
|
|
166
|
+
# Marks the INI file as tainted -- this will traverse each section marking
|
|
167
|
+
# each section as tainted as well.
|
|
168
|
+
#
|
|
169
|
+
def taint
|
|
170
|
+
super
|
|
171
|
+
@ini.each_value {|h| h.taint}
|
|
172
|
+
@ini.taint
|
|
173
|
+
self
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
#
|
|
177
|
+
# call-seq:
|
|
178
|
+
# dup
|
|
179
|
+
#
|
|
180
|
+
# Produces a duplicate of this INI file. The duplicate is independent of the
|
|
181
|
+
# original -- i.e. the duplicate can be modified without changing the
|
|
182
|
+
# orgiinal. The tainted state of the original is copied to the duplicate.
|
|
183
|
+
#
|
|
184
|
+
def dup
|
|
185
|
+
other = super
|
|
186
|
+
other.instance_variable_set(:@ini, Hash.new {|h,k| h[k] = Hash.new})
|
|
187
|
+
@ini.each_pair {|s,h| other[s].merge! h}
|
|
188
|
+
other.taint if self.tainted?
|
|
189
|
+
other
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
#
|
|
193
|
+
# call-seq:
|
|
194
|
+
# clone
|
|
195
|
+
#
|
|
196
|
+
# Produces a duplicate of this INI file. The duplicate is independent of the
|
|
197
|
+
# original -- i.e. the duplicate can be modified without changing the
|
|
198
|
+
# orgiinal. The tainted state and the frozen state of the original is copied
|
|
199
|
+
# to the duplicate.
|
|
200
|
+
#
|
|
201
|
+
def clone
|
|
202
|
+
other = dup
|
|
203
|
+
other.freeze if self.frozen?
|
|
204
|
+
other
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
#
|
|
208
|
+
# call-seq:
|
|
209
|
+
# eql?( other )
|
|
210
|
+
#
|
|
211
|
+
# Returns +true+ if the _other_ object is equivalent to this INI file. For
|
|
212
|
+
# two INI files to be equivalent, they must have the same sections with the
|
|
213
|
+
# same parameter / value pairs in each section.
|
|
214
|
+
#
|
|
215
|
+
def eql?( other )
|
|
216
|
+
return true if equal? other
|
|
217
|
+
return false unless other.instance_of? self.class
|
|
218
|
+
@ini == other.instance_variable_get(:@ini)
|
|
219
|
+
end
|
|
220
|
+
alias :== :eql?
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
private
|
|
224
|
+
#
|
|
225
|
+
# call-seq
|
|
226
|
+
# parse
|
|
227
|
+
#
|
|
228
|
+
# Parse the ini file contents.
|
|
229
|
+
#
|
|
230
|
+
def parse
|
|
231
|
+
return unless ::Kernel.test ?f, @fn
|
|
232
|
+
section = nil
|
|
233
|
+
|
|
234
|
+
::File.open(@fn, 'r') do |f|
|
|
235
|
+
while line = f.gets
|
|
236
|
+
line = line.chomp
|
|
237
|
+
|
|
238
|
+
case line
|
|
239
|
+
# ignore blank lines and comment lines
|
|
240
|
+
when @rgxp_comment then next
|
|
241
|
+
|
|
242
|
+
# this is a section declaration
|
|
243
|
+
when @rgxp_section then section = @ini[$1.strip]
|
|
244
|
+
|
|
245
|
+
# otherwise we have a parameter
|
|
246
|
+
when @rgxp_param
|
|
247
|
+
begin
|
|
248
|
+
section[$1.strip] = $2.strip
|
|
249
|
+
rescue NoMethodError
|
|
250
|
+
raise Error, "parameter encountered before first section"
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
else
|
|
254
|
+
raise Error, "could not parse line '#{line}"
|
|
255
|
+
end
|
|
256
|
+
end # while
|
|
257
|
+
end # File.open
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# EOF
|
metadata
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ini-phile
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Julien Durillon
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2013-02-18 00:00:00.000000000 Z
|
|
13
|
+
dependencies: []
|
|
14
|
+
description: Read, write, modify ini fils seamlessly.
|
|
15
|
+
email: julien.durillon@clever-cloud.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- lib/ini-phile.rb
|
|
21
|
+
homepage: http://github.com/judu/ruby-ini-gem
|
|
22
|
+
licenses: []
|
|
23
|
+
post_install_message:
|
|
24
|
+
rdoc_options: []
|
|
25
|
+
require_paths:
|
|
26
|
+
- lib
|
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
28
|
+
none: false
|
|
29
|
+
requirements:
|
|
30
|
+
- - ! '>='
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
34
|
+
none: false
|
|
35
|
+
requirements:
|
|
36
|
+
- - ! '>='
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '0'
|
|
39
|
+
requirements: []
|
|
40
|
+
rubyforge_project:
|
|
41
|
+
rubygems_version: 1.8.25
|
|
42
|
+
signing_key:
|
|
43
|
+
specification_version: 3
|
|
44
|
+
summary: Edit ini files with ease
|
|
45
|
+
test_files: []
|