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