Module: OpenStudio::Extension

Defined in:
lib/openstudio/extension.rb,
lib/openstudio/extension/runner.rb,
lib/openstudio/extension/version.rb,
lib/openstudio/extension/rake_task.rb,
lib/openstudio/extension/runner_config.rb

Defined Under Namespace

Classes: Extension, RakeTask, Runner, RunnerConfig

Constant Summary collapse

VERSION =
'0.6.1'.freeze

Class Method Summary collapse

Class Method Details

.all_extensionsObject

Module method to return all classes derived from OpenStudio::Extension::Extension Note all extension classes must be loaded before calling this method

@return [Array]: Array of classes


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/openstudio/extension.rb', line 55

def self.all_extensions
  # DLM: consider calling Bundler.require in this method
  # do not call Bundler.require when requiring this file, only when calling this method
  result = []
 
  ObjectSpace.each_object(::Class) do |obj|
    next if !obj.ancestors.include?(OpenStudio::Extension::Extension)
    result << obj
  end

  return result.uniq
end

.all_file_dirsObject

Module method to return file directories from all extensions

@return [Array]  Array of measure resource directories


86
87
88
89
90
91
92
93
94
# File 'lib/openstudio/extension.rb', line 86

def self.all_file_dirs
  result = []
  all_extensions.each do |obj|
    dir = obj.new.files_dir
    result << dir if dir
  rescue StandardError
  end
  return result.uniq.sort
end

.all_measure_dirsObject

Module method to return measure directories from all extensions

@return [Array]: Array of measure directories


72
73
74
75
76
77
78
79
80
# File 'lib/openstudio/extension.rb', line 72

def self.all_measure_dirs
  result = []
  all_extensions.each do |obj|
    dir = obj.new.measures_dir
    result << dir if dir
  rescue StandardError
  end
  return result.uniq.sort
end

.check_for_name_conflictsObject

Module method to check for duplicate file, measure, or measure resource names across all extensions

Will raise an error if conflicting file names are found. Note that file names in measure_files_dir names (e.g. License.md) are expected to be the same across all extensions.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/openstudio/extension.rb', line 102

def self.check_for_name_conflicts
  measure_dirs = all_measure_dirs
  file_dirs = all_file_dirs
  conflicts = []

  measure_dir_names = {}
  measure_dirs.each do |dir|
    Dir.glob(File.join(dir, '*')).each do |file|
      next if !File.directory?(file)
      next if !File.exist?(File.join(file, 'measure.rb'))

      # puts file
      file_name = File.basename(file).downcase
      if measure_dir_names[file_name]
        conflicts << "Measure '#{file}' conflicts with '#{measure_dir_names[file_name]}'"
      else
        measure_dir_names[file_name] = file
      end
    end
  end

  file_names = {}
  file_dirs.each do |dir|
    Dir.glob(File.join(dir, '*')).each do |file|
      next if !File.file?(file)

      # puts file
      file_name = File.basename(file).downcase
      if file_names[file_name]
        conflicts << "File '#{file}' conflicts with '#{file_names[file_name]}'"
      else
        file_names[file_name] = file
      end
    end
  end

  if !conflicts.empty?
    raise "Conflicting file names found: [#{conflicts.join(', ')}]"
  end

  return false
end

.configure_osw(in_osw) ⇒ Object

Module method used to configure an input OSW with paths to all OpenStudio::Extension measure and file directories

@param [Hash] in_osw Initial OSW object as a Hash, keys should be symbolized

@return [Hash]  Output OSW with measure and file paths configured


151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/openstudio/extension.rb', line 151

def self.configure_osw(in_osw)
  check_for_name_conflicts

  measure_dirs = all_measure_dirs
  file_dirs = all_file_dirs

  in_osw[:measure_paths] = [] if in_osw[:measure_paths].nil?
  in_osw[:file_paths] = [] if in_osw[:file_paths].nil?

  in_osw[:measure_paths] = in_osw[:measure_paths].concat(measure_dirs).uniq
  in_osw[:file_paths] = in_osw[:file_paths].concat(file_dirs).uniq

  return in_osw
end

.measure_in_osw(osw, measure_dir_name, step_name = nil) ⇒ Object

Module method used to check whether a measure is present in an OSW file

@param [Hash] in_osw Initial OSW object as a Hash, keys should be symbolized
@param [String] measure_dir_name Directory name of measure to set argument on
@param [String] step_name Optional argument, if present used to further identify the measure

@return [Boolean] true or false


207
208
209
210
211
212
213
214
215
216
# File 'lib/openstudio/extension.rb', line 207

def self.measure_in_osw(osw, measure_dir_name, step_name = nil)
  result = false
  osw[:steps].each do |step|
    if step[:measure_dir_name] == measure_dir_name && (step_name.nil? || step[:name] == step_name)
      result = true
    end
  end

  return result
end

.set_measure_argument(osw, measure_dir_name, argument_name, argument_value, step_name = nil) ⇒ Object

Module method used to set the measure argument for measure_dir_name to argument_value, argument_name must appear in the OSW or exception will be raised. If step_name is nil then all workflow steps matching measure_dir_name will be affected. If step_name is not nil, then only workflow steps matching measure_dir_name and step_name will be affected.

@param [Hash] in_osw Initial OSW object as a Hash, keys should be symbolized
@param [String] measure_dir_name Directory name of measure to set argument on
@param [String] argument_name Name of the argument to set
@param [String] argument_value Value to set the argument name to
@param [String] step_name Optional argument, if present used to select workflow step to modify

@return [Hash] Output OSW with measure argument set to argument value


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/openstudio/extension.rb', line 179

def self.set_measure_argument(osw, measure_dir_name, argument_name, argument_value, step_name = nil)
  result = false
  osw[:steps].each do |step|
    if step[:measure_dir_name] == measure_dir_name && (step_name.nil? || step[:name] == step_name)
      step[:arguments][argument_name.to_sym] = argument_value
      result = true
    end
  end

  if !result
    if step_name
      raise "Could not set '#{argument_name}' to '#{argument_value}' for measure '#{measure_dir_name}' in step '#{step_name}'"
    else
      raise "Could not set '#{argument_name}' to '#{argument_value}' for measure '#{measure_dir_name}'"
    end
  end

  return osw
end