Class: CFA::AugeasParser

Inherits:
Object
  • Object
show all
Defined in:
lib/cfa/augeas_parser.rb

Overview

Examples:

read, print, modify and serialize again

require "cfa/augeas_parser"

parser = CFA::AugeasParser.new("Sysconfig.lns")
data = parser.parse(File.read("/etc/default/grub"))

puts data["GRUB_DISABLE_OS_PROBER"]
data["GRUB_DISABLE_OS_PROBER"] = "true"
puts parser.serialize(data)

Instance Method Summary collapse

Constructor Details

#initialize(lens) ⇒ AugeasParser

Returns a new instance of AugeasParser.

Parameters:

  • lens (String)

    a lens name, like “Sysconfig.lns”



308
309
310
# File 'lib/cfa/augeas_parser.rb', line 308

def initialize(lens)
  @lens = lens
end

Instance Method Details

#emptyAugeasTree

Returns an empty tree that can be filled for future serialization.

Returns:

  • (AugeasTree)

    an empty tree that can be filled for future serialization



354
355
356
# File 'lib/cfa/augeas_parser.rb', line 354

def empty
  AugeasTree.new
end

#parse(raw_string) ⇒ AugeasTree

Returns the parsed data.

Parameters:

  • raw_string (String)

    a string to be parsed

Returns:



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/cfa/augeas_parser.rb', line 314

def parse(raw_string)
  require "cfa/augeas_parser/reader"
  # Workaround for augeas lenses that don't handle files
  # without a trailing newline (bsc#1064623, bsc#1074891, bsc#1080051
  # and gh#hercules-team/augeas#547)
  raw_string += "\n" unless raw_string.end_with?("\n")
  @old_content = raw_string

  # open augeas without any autoloading and it should not touch disk and
  # load lenses as needed only
  root = load_path = nil
  Augeas.open(root, load_path, Augeas::NO_MODL_AUTOLOAD) do |aug|
    aug.set("/input", raw_string)
    report_error(aug) unless aug.text_store(@lens, "/input", "/store")

    return AugeasReader.read(aug, "/store")
  end
end

#serialize(data) ⇒ String

Returns a string to be written.

Parameters:

Returns:

  • (String)

    a string to be written



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/cfa/augeas_parser.rb', line 335

def serialize(data)
  require "cfa/augeas_parser/writer"
  # open augeas without any autoloading and it should not touch disk and
  # load lenses as needed only
  root = load_path = nil
  Augeas.open(root, load_path, Augeas::NO_MODL_AUTOLOAD) do |aug|
    aug.set("/input", @old_content || "")
    aug.text_store(@lens, "/input", "/store") if @old_content
    AugeasWriter.new(aug).write("/store", data)

    res = aug.text_retrieve(@lens, "/input", "/store", "/output")
    report_error(aug) unless res

    return aug.get("/output")
  end
end