Class: OpenStudio::Extension::RunnerConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/openstudio/extension/runner_config.rb

Constant Summary collapse

FILENAME =
'runner.conf'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dirname) ⇒ RunnerConfig

Class to store configuration of the runner options.

@param [String] dirname Directory where runner.conf file is stored, typically the root of the extension.


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/openstudio/extension/runner_config.rb', line 47

def initialize(dirname)
  # read in the runner config from file, if it exists, otherwise inform the user to run
  # the rake task
  @dirname = dirname

  check_file = File.join(@dirname, FILENAME)
  if File.exist? check_file
    @data = JSON.parse(File.read(check_file), symbolize_names: true)
  else
    raise "There is no runner.conf in directory #{@dirname}. Run `rake openstudio:runner:init`"
  end
end

Class Method Details

.default_configObject

Return the default runner configuration



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/openstudio/extension/runner_config.rb', line 75

def self.default_config
  return {
    file_version: '0.1.0',
    max_datapoints: 1E9.to_i,
    num_parallel: 2,
    run_simulations: true,
    verbose: false,
    gemfile_path: '',
    bundle_install_path: ''
  }
end

.init(dirname) ⇒ Object

Save a templated runner configuration to the specified directory. Note that this will override any config that has been created

@param [String] dirname Directory where runner.conf file is stored, typically the root of the extension.


92
93
94
95
96
97
98
# File 'lib/openstudio/extension/runner_config.rb', line 92

def self.init(dirname)
  File.open(File.join(dirname, FILENAME), 'w') do |f|
    f << JSON.pretty_generate(default_config)
  end

  return default_config
end

Instance Method Details

#add_config_option(name, value) ⇒ Object

Add runner configuration that may be used in other extensions

@param [String] name, Name of the new config option


64
65
66
67
68
69
70
71
# File 'lib/openstudio/extension/runner_config.rb', line 64

def add_config_option(name, value)
  if @data.key? name.to_sym
    raise "Runner config already has the named option of #{name}"
  end

  # TODO: do we need to verify that name is allowed to be a key?
  @data[name.to_sym] = value
end

#optionsObject

Return the options as hash



124
125
126
# File 'lib/openstudio/extension/runner_config.rb', line 124

def options
  return @data
end

#saveObject

Save the updated config options, if they have changed. Changes will typically only occur if calling add_config option



103
104
105
106
107
# File 'lib/openstudio/extension/runner_config.rb', line 103

def save
  File.open(File.join(@dirname, FILENAME), 'w') do |f|
    f << JSON.pretty_generate(@data)
  end
end

#update_config(key, new_value) ⇒ Object

Update a runner config value

Parameters:

  • key, (String)

    The name of the key to update

  • new_value, (Variant)

    The new value to set the `key` to.



114
115
116
117
118
119
120
# File 'lib/openstudio/extension/runner_config.rb', line 114

def update_config(key, new_value)
  if @data.key? key.to_sym
    @data[key.to_sym] = new_value
  else
    raise "Could not find key '#{key}' to update in RunnerConfig."
  end
end