Class: OpenC3::MetaConfigParser

Inherits:
Object
  • Object
show all
Defined in:
lib/openc3/config/meta_config_parser.rb

Overview

Reads YAML formatted files describing a configuration file

Class Method Summary collapse

Class Method Details

.dump(object, filename) ⇒ Object



71
72
73
74
75
# File 'lib/openc3/config/meta_config_parser.rb', line 71

def self.dump(object, filename)
  File.open(filename, 'w') do |file|
    file.write Psych.dump(object)
  end
end

.load(filename) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/openc3/config/meta_config_parser.rb', line 44

def self.load(filename)
  data = nil
  if File.exist?(filename)
    path = filename
    @basedir = File.dirname(filename)
  else
    path = File.join(@basedir, filename)
  end
  tf = Tempfile.new("temp.yaml")

  output = nil
  OpenC3.set_working_dir(File.dirname(path)) do
    output = ERB.new(File.read(path), trim_mode: "-").result(binding)
  end
  tf.write(output)
  tf.close
  begin
    data = Psych.safe_load(File.read(tf.path), aliases: true)
  rescue => error
    error_file = "#{filename}.err"
    File.open(error_file, 'w') { |file| file.puts output }
    raise error.exception("#{error.message}\n\nParsed output written to #{File.expand_path(error_file)}\n")
  end
  tf.unlink
  data
end