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.


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/openstudio/extension/runner_config.rb', line 17

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



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

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.


62
63
64
65
66
67
68
# File 'lib/openstudio/extension/runner_config.rb', line 62

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


34
35
36
37
38
39
40
41
# File 'lib/openstudio/extension/runner_config.rb', line 34

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



94
95
96
# File 'lib/openstudio/extension/runner_config.rb', line 94

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



73
74
75
76
77
# File 'lib/openstudio/extension/runner_config.rb', line 73

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.



84
85
86
87
88
89
90
# File 'lib/openstudio/extension/runner_config.rb', line 84

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