@!attribute [r] contents
@api private @return [Array<String, Puppet::Util::IniConfig::Section>]
@!attribute [rw] #destroy_empty
Whether empty files should be removed if no sections are defined. Defaults to false
@!attribute [rw] #file_collection
@return [Puppet::Util::IniConfig::FileCollection]
@!attribute [r] filetype
@api private @return [Puppet::Util::FileType::FileTypeFlat]
Create a new section and store it in the file contents
@api private @param name [String] The name of the section to create @return [Puppet::Util::IniConfig::Section]
# File lib/puppet/util/inifile.rb, line 239 def add_section(name) if section_exists?(name) raise IniParseError.new("Section #{name.inspect} is already defined, cannot redefine", @file) end section = Section.new(name, @file) @contents << section section end
# File lib/puppet/util/inifile.rb, line 210 def format text = "" @contents.each do |content| if content.is_a? Section text << content.format else text << content end end text end
@return [Puppet::Util::IniConfig::Section, nil] The section with the
given name if it exists, else nil.
# File lib/puppet/util/inifile.rb, line 206 def get_section(name) @contents.find { |entry| entry.is_a? Section and entry.name == name } end
@api private
# File lib/puppet/util/inifile.rb, line 154 def parse(text) section = nil # The name of the current section optname = nil # The name of the last option in section line_num = 0 text.each_line do |l| line_num += 1 if l.match(INI_COMMENT) # Whitespace or comment if section.nil? @contents << l else section.add_line(l) end elsif l.match(INI_CONTINUATION) && section && optname # continuation line section[optname] += "\n#{l.chomp}" elsif (match = l.match(INI_SECTION_NAME)) # section heading section.mark_clean if section section_name = match[1] section = add_section(section_name) optname = nil elsif (match = l.match(INI_PROPERTY)) # We allow space around the keys, but not the values # For the values, we don't know if space is significant key = match[1] val = match[2] if section.nil? raise IniParseError.new("Property with key #{key.inspect} outside of a section") end section[key] = val optname = key else raise IniParseError.new("Can't parse line '#{l.chomp}'", @file, line_num) end end section.mark_clean unless section.nil? end
Read and parse the on-disk file associated with this object
# File lib/puppet/util/inifile.rb, line 136 def read text = @filetype.read if text.nil? raise IniParseError, "Cannot read nonexistent file #{@file.inspect}" end parse(text) end
@return [Array<Puppet::Util::IniConfig::Section>] All sections defined in
this file.
# File lib/puppet/util/inifile.rb, line 200 def sections @contents.select { |entry| entry.is_a? Section } end
# File lib/puppet/util/inifile.rb, line 224 def store if @destroy_empty and (sections.empty? or sections.all?(&:destroy?)) ::File.unlink(@file) elsif sections.any?(&:dirty?) text = self.format @filetype.write(text) end sections.each(&:mark_clean) end
# File lib/puppet/util/inifile.rb, line 127 def initialize(file, options = {}) @file = file @contents = [] @filetype = Puppet::Util::FileType.filetype(:flat).new(file) @destroy_empty = options.fetch(:destroy_empty, false) end