class Puppet::Util::IniConfig::PhysicalFile
Constants
- INI_COMMENT
- INI_CONTINUATION
- INI_PROPERTY
- INI_SECTION_NAME
Attributes
contents[R]
@!attribute [r] contents
@api private @return [Array<String, Puppet::Util::IniConfig::Section>]
destroy_empty[RW]
@!attribute [rw] destroy_empty
Whether empty files should be removed if no sections are defined. Defaults to false
file_collection[RW]
@!attribute [rw] file_collection
@return [Puppet::Util::IniConfig::FileCollection]
filetype[R]
@!attribute [r] filetype
@api private @return [Puppet::Util::FileType::FileTypeFlat]
Public Class Methods
new(file, options = {})
click to toggle source
# File lib/puppet/util/inifile.rb 128 def initialize(file, options = {}) 129 @file = file 130 @contents = [] 131 @filetype = Puppet::Util::FileType.filetype(:flat).new(file) 132 133 @destroy_empty = options.fetch(:destroy_empty, false) 134 end
Public Instance Methods
add_section(name)
click to toggle source
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 239 def add_section(name) 240 if section_exists?(name) 241 raise IniParseError.new(_("Section %{name} is already defined, cannot redefine") % { name: name.inspect }, @file) 242 end 243 244 section = Section.new(name, @file) 245 @contents << section 246 247 section 248 end
format()
click to toggle source
# File lib/puppet/util/inifile.rb 210 def format 211 text = String.new 212 213 @contents.each do |content| 214 if content.is_a? Section 215 text << content.format 216 else 217 text << content 218 end 219 end 220 221 text 222 end
get_section(name)
click to toggle source
@return [Puppet::Util::IniConfig::Section, nil] The section with the
given name if it exists, else nil.
# File lib/puppet/util/inifile.rb 206 def get_section(name) 207 @contents.find { |entry| entry.is_a? Section and entry.name == name } 208 end
parse(text)
click to toggle source
@api private
# File lib/puppet/util/inifile.rb 155 def parse(text) 156 section = nil # The name of the current section 157 optname = nil # The name of the last option in section 158 line_num = 0 159 160 text.each_line do |l| 161 line_num += 1 162 if l.match(INI_COMMENT) 163 # Whitespace or comment 164 if section.nil? 165 @contents << l 166 else 167 section.add_line(l) 168 end 169 elsif l.match(INI_CONTINUATION) && section && optname 170 # continuation line 171 section[optname] += "\n#{l.chomp}" 172 elsif (match = l.match(INI_SECTION_NAME)) 173 # section heading 174 section.mark_clean if section 175 176 section_name = match[1] 177 178 section = add_section(section_name) 179 optname = nil 180 elsif (match = l.match(INI_PROPERTY)) 181 # the regex strips leading white space from the value, and here we strip the trailing white space as well 182 key = match[1] 183 val = match[2].rstrip 184 185 if section.nil? 186 raise IniParseError.new(_("Property with key %{key} outside of a section") % { key: key.inspect }) 187 end 188 189 section[key] = val 190 optname = key 191 else 192 raise IniParseError.new(_("Can't parse line '%{line}'") % { line: l.chomp }, @file, line_num) 193 end 194 end 195 section.mark_clean unless section.nil? 196 end
read()
click to toggle source
Read and parse the on-disk file associated with this object
# File lib/puppet/util/inifile.rb 137 def read 138 text = @filetype.read 139 if text.nil? 140 raise IniParseError, _("Cannot read nonexistent file %{file}") % { file: @file.inspect } 141 end 142 parse(text) 143 end
sections()
click to toggle source
@return [Array<Puppet::Util::IniConfig::Section>] All sections defined in
this file.
# File lib/puppet/util/inifile.rb 200 def sections 201 @contents.select { |entry| entry.is_a? Section } 202 end
store()
click to toggle source
# File lib/puppet/util/inifile.rb 224 def store 225 if @destroy_empty and (sections.empty? or sections.all?(&:destroy?)) 226 ::File.unlink(@file) 227 elsif sections.any?(&:dirty?) 228 text = self.format 229 @filetype.write(text) 230 end 231 sections.each(&:mark_clean) 232 end
Private Instance Methods
section_exists?(name)
click to toggle source
# File lib/puppet/util/inifile.rb 252 def section_exists?(name) 253 if self.get_section(name) 254 true 255 elsif @file_collection and @file_collection.get_section(name) 256 true 257 else 258 false 259 end 260 end