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")
parser.file_name = "/etc/default/grub" # for error reporting
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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lens) ⇒ AugeasParser

Returns a new instance of AugeasParser.

Parameters:

  • lens (String)

    a lens name, like “Sysconfig.lns”



378
379
380
381
# File 'lib/cfa/augeas_parser.rb', line 378

def initialize(lens)
  @lens = lens
  @file_name = nil
end

Instance Attribute Details

#file_nameString

Returns optional, used for error reporting.

Returns:

  • (String)

    optional, used for error reporting



375
376
377
# File 'lib/cfa/augeas_parser.rb', line 375

def file_name
  @file_name
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



426
427
428
# File 'lib/cfa/augeas_parser.rb', line 426

def empty
  AugeasTree.new
end

#parse(raw_string) ⇒ AugeasTree

Returns the parsed data.

Parameters:

  • raw_string (String)

    a string to be parsed

Returns:



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/cfa/augeas_parser.rb', line 385

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, :parsing, file_name, raw_string) \
      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



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/cfa/augeas_parser.rb', line 407

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, :serializing, file_name, data) unless res

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