class Puppet::Util::IniConfig::File

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

Public Class Methods

new() click to toggle source
# File lib/puppet/util/inifile.rb, line 90
def initialize
  @files = {}
end

Public Instance Methods

[](name) click to toggle source

Return the Section with the given name or nil

# File lib/puppet/util/inifile.rb, line 180
def [](name)
  name = name.to_s
  each_section do |section|
    return section if section.name == name
  end
  nil
end
add_section(name, file) click to toggle source

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

# File lib/puppet/util/inifile.rb, line 194
def add_section(name, file)
  raise "A section with name #{name} already exists" if include?(name)
  result = Section.new(name, file)
  @files[file] ||= []
  @files[file] << result
  result
end
each_file() { |file| ... } click to toggle source

Execute BLOCK, passing each file constituting this inifile as an argument

# File lib/puppet/util/inifile.rb, line 173
def each_file(&block)
  @files.keys.each do |file|
    yield(file)
  end
end
each_section() { |entry| ... } click to toggle source

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

# File lib/puppet/util/inifile.rb, line 163
def each_section(&block)
  @files.each do |file, list|
    list.each do |entry|
      yield(entry) if entry.is_a?(Section)
    end
  end
end
include?(name) click to toggle source

Return true if the file contains a section with name NAME

# File lib/puppet/util/inifile.rb, line 189
def include?(name)
  ! self[name].nil?
end
read(file) click to toggle source

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

# File lib/puppet/util/inifile.rb, line 96
def read(file)
  text = Puppet::Util::FileType.filetype(:flat).new(file).read
  raise "Could not find #{file}" if text.nil?

  section = nil   # The name of the current section
  optname = nil   # The name of the last option in section
  line = 0
  @files[file] = []
  text.each_line do |l|
    line += 1
    if l.strip.empty? || "#;".include?(l[0,1]) ||
        (l.split(nil, 2)[0].downcase == "rem" && l[0,1].downcase == "r")
      # Whitespace or comment
      if section.nil?
        @files[file] << l
      else
        section.add_line(l)
      end
    elsif " \t\r\n\f".include?(l[0,1]) && section && optname
      # continuation line
      section[optname] += "\n#{l.chomp}"
    elsif l =~ /^\[([^\]]+)\]/
      # section heading
      section.mark_clean unless section.nil?
      section = add_section($1, file)
      optname = nil
    elsif l =~ /^\s*([^\s=]+)\s*\=(.*)$/
      # We allow space around the keys, but not the values
      # For the values, we don't know if space is significant
      if section.nil?
        raise "#{file}:#{line}:Key/value pair outside of a section for key #{$1}"
      else
        section[$1] = $2
        optname = $1
      end
    else
      raise "#{file}:#{line}: Can't parse '#{l.chomp}'"
    end
  end
  section.mark_clean unless section.nil?
end
store() click to toggle source

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

# File lib/puppet/util/inifile.rb, line 141
def store
  @files.each do |file, lines|
    text = ""
    dirty = false
    lines.each do |l|
      if l.is_a?(Section)
        dirty ||= l.dirty?
        text << l.format
        l.mark_clean
      else
        text << l
      end
    end
    if dirty
      Puppet::Util::FileType.filetype(:flat).new(file).write(text)
      return file
    end
  end
end