Class: Puppet::Util::IniConfig::PhysicalFile

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  @file = file
  @contents = []
  @filetype = Puppet::Util::FileType.filetype(:flat).new(file)

  @destroy_empty = options.fetch(:destroy_empty, false)
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



117
118
119
# File 'lib/puppet/util/inifile.rb', line 117

def contents
  @contents
end

#destroy_emptyObject

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_collectionPuppet::Util::IniConfig::FileCollection



126
127
128
# File 'lib/puppet/util/inifile.rb', line 126

def file_collection
  @file_collection
end

#filetypeObject (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

Parameters:

  • name (String)

    The name of the section to create

Returns:



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

#formatObject



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.

Returns:



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

#readObject

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

#sectionsArray<Puppet::Util::IniConfig::Section>

Returns All sections defined in this file.

Returns:



201
202
203
# File 'lib/puppet/util/inifile.rb', line 201

def sections
  @contents.select { |entry| entry.is_a? Section }
end

#storeObject



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