Class Puppet::Util::IniConfig::File
In: lib/puppet/util/inifile.rb
Parent: Object

A logical .ini-file that can be spread across several physical files. For each physical file, call read with the filename

Methods

[]   add_section   each_section   include?   new   read   store  

Public Class methods

[Source]

    # File lib/puppet/util/inifile.rb, line 97
97:         def initialize
98:             @files = {}
99:         end

Public Instance methods

Return the Section with the given name or nil

[Source]

     # File lib/puppet/util/inifile.rb, line 183
183:         def [](name)
184:             name = name.to_s
185:             each_section do |section|
186:                 return section if section.name == name
187:             end
188:             return nil
189:         end

Add a section to be stored in FILE when store is called

[Source]

     # File lib/puppet/util/inifile.rb, line 197
197:         def add_section(name, file)
198:             if include?(name)
199:                 raise "A section with name #{name} already exists"
200:             end
201:             result = Section.new(name, file)
202:             @files[file] ||= []
203:             @files[file] << result
204:             return result
205:         end

Execute BLOCK, passing each section in this file as an argument

[Source]

     # File lib/puppet/util/inifile.rb, line 172
172:         def each_section(&block)
173:             @files.each do |file, list|
174:                 list.each do |entry|
175:                     if entry.is_a?(Section)
176:                         yield(entry)
177:                     end
178:                 end
179:             end
180:         end

Return true if the file contains a section with name NAME

[Source]

     # File lib/puppet/util/inifile.rb, line 192
192:         def include?(name)
193:             return ! self[name].nil?
194:         end

Add the contents of the file with name FILE to the already existing sections

[Source]

     # File lib/puppet/util/inifile.rb, line 103
103:         def read(file)
104:             text = Puppet::Util::FileType.filetype(:flat).new(file).read
105:             if text.nil?
106:                 raise "Could not find #{file}"
107:             end
108: 
109:             section = nil   # The name of the current section
110:             optname = nil   # The name of the last option in section
111:             line = 0
112:             @files[file] = []
113:             text.each_line do |l|
114:                 line += 1
115:                 if l.strip.empty? || "#;".include?(l[0,1]) ||
116:                         (l.split(nil, 2)[0].downcase == "rem" && 
117:                          l[0,1].downcase == "r")
118:                     # Whitespace or comment
119:                     if section.nil?
120:                         @files[file] << l
121:                     else
122:                         section.add_line(l)
123:                     end
124:                 elsif " \t\r\n\f".include?(l[0,1]) && section && optname
125:                     # continuation line
126:                     section[optname] += "\n" + l.chomp
127:                 elsif l =~ /^\[([^\]]+)\]/
128:                     # section heading
129:                     section.mark_clean unless section.nil?
130:                     section = add_section($1, file)
131:                     optname = nil
132:                 elsif l =~ /^\s*([^\s=]+)\s*\=(.*)$/
133:                     # We allow space around the keys, but not the values
134:                     # For the values, we don't know if space is significant
135:                     if section.nil?
136:                         raise "#{file}:#{line}:Key/value pair outside of a section for key #{$1}"
137:                     else
138:                         section[$1] = $2
139:                         optname = $1
140:                     end
141:                 else
142:                     raise "#{file}:#{line}: Can't parse '#{l.chomp}'"
143:                 end
144:             end
145:             section.mark_clean unless section.nil?
146:         end

Store all modifications made to sections in this file back to the physical files. If no modifications were made to a physical file, nothing is written

[Source]

     # File lib/puppet/util/inifile.rb, line 151
151:         def store
152:             @files.each do |file, lines|
153:                 text = ""
154:                 dirty = false
155:                 lines.each do |l|
156:                     if l.is_a?(Section)
157:                         dirty ||= l.dirty?
158:                         text << l.format
159:                         l.mark_clean
160:                     else
161:                         text << l
162:                     end
163:                 end
164:                 if dirty
165:                     Puppet::Util::FileType.filetype(:flat).new(file).write(text)
166:                 end
167:             end
168:         end

[Validate]