Class: Puppet::Util::IniConfig::PhysicalFile
- Defined in:
- lib/puppet/util/inifile.rb
Constant Summary collapse
- INI_COMMENT =
Regexp.union( /^\s*$/, /^[#;]/, /^\s*rem\s/i )
- INI_CONTINUATION =
/^[ \t\r\n\f]/
- INI_SECTION_NAME =
/^\[([^\]]+)\]/
- INI_PROPERTY =
/^\s*([^\s=]+)\s*\=(.*)$/
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#destroy_empty ⇒ Object
Whether empty files should be removed if no sections are defined.
- #file_collection ⇒ Puppet::Util::IniConfig::FileCollection
-
#filetype ⇒ Object
readonly
Returns the value of attribute filetype.
Instance Method Summary collapse
-
#add_section(name) ⇒ Puppet::Util::IniConfig::Section
private
Create a new section and store it in the file contents.
- #format ⇒ Object
-
#get_section(name) ⇒ Puppet::Util::IniConfig::Section?
The section with the given name if it exists, else nil.
-
#initialize(file, options = {}) ⇒ PhysicalFile
constructor
A new instance of PhysicalFile.
- #parse(text) ⇒ Object private
-
#read ⇒ Object
Read and parse the on-disk file associated with this object.
-
#sections ⇒ Array<Puppet::Util::IniConfig::Section>
All sections defined in this file.
- #store ⇒ Object
Constructor Details
#initialize(file, options = {}) ⇒ PhysicalFile
Returns a new instance of PhysicalFile.
127 128 129 130 131 132 133 |
# File 'lib/puppet/util/inifile.rb', line 127 def initialize(file, = {}) @file = file @contents = [] @filetype = Puppet::Util::FileType.filetype(:flat).new(file) @destroy_empty = .fetch(:destroy_empty, false) end |
Instance Attribute Details
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
116 117 118 |
# File 'lib/puppet/util/inifile.rb', line 116 def contents @contents end |
#destroy_empty ⇒ Object
Whether empty files should be removed if no sections are defined. Defaults to false
121 122 123 |
# File 'lib/puppet/util/inifile.rb', line 121 def destroy_empty @destroy_empty end |
#file_collection ⇒ Puppet::Util::IniConfig::FileCollection
125 126 127 |
# File 'lib/puppet/util/inifile.rb', line 125 def file_collection @file_collection end |
#filetype ⇒ Object (readonly)
Returns the value of attribute filetype.
111 112 113 |
# File 'lib/puppet/util/inifile.rb', line 111 def filetype @filetype end |
Instance Method Details
#add_section(name) ⇒ Puppet::Util::IniConfig::Section
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a new section and store it in the file contents
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/puppet/util/inifile.rb', line 239 def add_section(name) if section_exists?(name) raise IniParseError.new(_("Section %{name} is already defined, cannot redefine") % { name: name.inspect }, @file) end section = Section.new(name, @file) @contents << section section end |
#format ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 |
# 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 |
#get_section(name) ⇒ Puppet::Util::IniConfig::Section?
Returns The section with the given name if it exists, else nil.
206 207 208 |
# File 'lib/puppet/util/inifile.rb', line 206 def get_section(name) @contents.find { |entry| entry.is_a? Section and entry.name == name } end |
#parse(text) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# 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} outside of a section") % { key: key.inspect }) end section[key] = val optname = key else raise IniParseError.new(_("Can't parse line '%{line}'") % { line: l.chomp }, @file, line_num) end end section.mark_clean unless section.nil? end |
#read ⇒ Object
Read and parse the on-disk file associated with this object
136 137 138 139 140 141 142 |
# File 'lib/puppet/util/inifile.rb', line 136 def read text = @filetype.read if text.nil? raise IniParseError, _("Cannot read nonexistent file %{file}") % { file: @file.inspect } end parse(text) end |
#sections ⇒ Array<Puppet::Util::IniConfig::Section>
Returns All sections defined in this file.
200 201 202 |
# File 'lib/puppet/util/inifile.rb', line 200 def sections @contents.select { |entry| entry.is_a? Section } end |
#store ⇒ Object
224 225 226 227 228 229 230 231 232 |
# 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 |