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*=\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.
128 129 130 131 132 133 134 |
# File 'lib/puppet/util/inifile.rb', line 128 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.
117 118 119 |
# File 'lib/puppet/util/inifile.rb', line 117 def contents @contents end |
#destroy_empty ⇒ Object
Whether empty files should be removed if no sections are defined. Defaults to false
122 123 124 |
# File 'lib/puppet/util/inifile.rb', line 122 def destroy_empty @destroy_empty end |
#file_collection ⇒ Puppet::Util::IniConfig::FileCollection
126 127 128 |
# File 'lib/puppet/util/inifile.rb', line 126 def file_collection @file_collection end |
#filetype ⇒ Object (readonly)
Returns the value of attribute filetype.
112 113 114 |
# File 'lib/puppet/util/inifile.rb', line 112 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
240 241 242 243 244 245 246 247 248 249 |
# File 'lib/puppet/util/inifile.rb', line 240 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
211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/puppet/util/inifile.rb', line 211 def format text = ''.dup @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.
207 208 209 |
# File 'lib/puppet/util/inifile.rb', line 207 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.
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 197 |
# File 'lib/puppet/util/inifile.rb', line 156 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)) # the regex strips leading white space from the value, and here we strip the trailing white space as well key = match[1] val = match[2].rstrip if section.nil? raise IniParseError, _("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
137 138 139 140 141 142 143 144 |
# File 'lib/puppet/util/inifile.rb', line 137 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.
201 202 203 |
# File 'lib/puppet/util/inifile.rb', line 201 def sections @contents.select { |entry| entry.is_a? Section } end |
#store ⇒ Object
225 226 227 228 229 230 231 232 233 |
# File 'lib/puppet/util/inifile.rb', line 225 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 |