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


127
128
129
130
131
132
133
# File 'lib/puppet/util/inifile.rb', line 127

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


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

def contents
  @contents
end

#destroy_emptyObject

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


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

def file_collection
  @file_collection
end

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


238
239
240
241
242
243
244
245
246
247
# File 'lib/puppet/util/inifile.rb', line 238

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


209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/puppet/util/inifile.rb', line 209

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?


205
206
207
# File 'lib/puppet/util/inifile.rb', line 205

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
# 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))
      # 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.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

#readObject

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

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


199
200
201
# File 'lib/puppet/util/inifile.rb', line 199

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

#storeObject


223
224
225
226
227
228
229
230
231
# File 'lib/puppet/util/inifile.rb', line 223

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