class Orocos::ConfigurationManager
Class that manages a set of configurations
Attributes
A mapping from the task model names to the corresponding {TaskConfigurations} object
@return [{String=>TaskConfigurations}]
Public Class Methods
# File lib/orocos/configurations.rb, line 1077 def initialize(loader = Orocos.default_loader) @loader = loader @conf = Hash.new end
Public Instance Methods
Applies the specified configuration on task
@param task (see Orocos::TaskConfigurations#apply) @param names (see Orocos::TaskConfigurations#apply) @option options [String] :model_name (task.model.name) the name of the
model that should be used to resolve the configurations
@option options [Boolean] :override (false) see the documentation of
{TaskConfigurations#apply}
@raise (see Orocos::TaskConfigurations#apply)
# File lib/orocos/configurations.rb, line 1177 def apply(task, names=Array.new, options = Hash.new) if options == true || options == false # Backward compatibility options = Hash[:override => options] end options, find_options = Kernel.filter_options options, :override => false, :model_name => task.model.name model_name = options[:model_name] if model_name.nil? raise ArgumentError, "applying configuration on #{task.name} failed. #{task.class} has no model name." end task_conf = find_task_configuration_object(task, find_options.merge(:model_name => model_name)) if names = resolve_requested_configuration_names(model_name, task_conf, names) ConfigurationManager.info "applying configuration #{names.join(", ")} on #{task.name} of type #{model_name}" task_conf.apply(task, names, options[:override]) else ConfigurationManager.info "required default configuration on #{task.name} of type #{model_name}, but #{model_name} has no registered configurations" end true end
# File lib/orocos/configurations.rb, line 1160 def find_task_configuration_object(task, options = Hash.new) if !task.model raise ArgumentError, "cannot use ConfigurationManager#apply for non-orogen tasks" end options = Kernel.validate_options options, :model_name => task.model.name conf[options[:model_name]] end
Loads all configuration files present in the given directory
The directory is assumed to be populated with files of the form
orogen_project::TaskName.yml
each file being a YAML file that follows the format described in the documentation of {TaskConfigurations}. It will ignore files that do not match this pattern, as well as file that refer to task models that cannot be found.
@param [String] dir the path to the directory @return [{String=>Array<String>}] a mapping from the task model
name to the list of configuration sections that got modified or added. Note that the set of sections is guaranteed to not be empty
# File lib/orocos/configurations.rb, line 1097 def load_dir(dir) if !File.directory?(dir) raise ArgumentError, "#{dir} is not a directory" end changed = Hash.new Dir.glob(File.join(dir, '*.yml')) do |file| next if !File.file?(file) changed_configurations = begin load_file(file) rescue OroGen::TaskModelNotFound ConfigurationManager.warn "ignoring configuration file #{file} as there are no corresponding task model" next end if changed_configurations changed.merge!(changed_configurations) do |model_name, old, new| old.concat(new).uniq end changed_configurations.each do |model_name, conf| ConfigurationManager.info " configuration #{conf} of #{model_name} changed" end end end changed end
Loads configuration from a YAML file
@param [String] file the path to the file @param [String,OroGen::Spec] model it is either an oroGen task context
model or the name of such a model If nil, the model is inferred from the file name, which is expected to be of the form orogen_project::TaskName.yml
@return [{String=>Array<String>},nil] if some configuration sections
changed or got added, the method returns a mapping from the task model name to the list of modified sections. Otherwise, it returns false
@raise ArgumentError if the file does not exist @raise OroGen::TaskModelNotFound if the task model cannot be found
# File lib/orocos/configurations.rb, line 1138 def load_file(file, model = nil) if !File.file?(file) raise ArgumentError, "#{file} does not exist or is not a file" end if !model || model.respond_to?(:to_str) model_name = model || File.basename(file, '.yml') model = loader.task_model_from_name(model_name) end ConfigurationManager.info "loading configuration file #{file} for #{model.name}" conf[model.name] ||= TaskConfigurations.new(model) changed_configurations = conf[model.name].load_from_yaml(file) ConfigurationManager.info " #{model.name} available configurations: #{conf[model.name].sections.keys.join(", ")}" if changed_configurations.empty? return false else Hash[model.name => changed_configurations] end end
Returns a resolved configuration value for a task model name
@param [String] task_model_name the name of the task model @param [Array<String>] conf_names the name of the configuration
sections
@param [Boolean] override if true, values that are set by early
elements in conf_names will be overriden if set in later elements. Otherwise, ArgumentError is thrown when this happens.
@return [Object] a configuration object as formatted by the rules
described in the {TaskConfigurations#sections} attribute description
# File lib/orocos/configurations.rb, line 1266 def resolve(task_model_name, conf_names = Array.new, override = false) if task_model_name.respond_to?(:model) task_model_name = task_model_name.model.name end task_conf = conf[task_model_name] if conf_names = resolve_requested_configuration_names(task_model_name, task_conf, conf_names) task_conf.conf(conf_names, override) else Hash.new end end
# File lib/orocos/configurations.rb, line 1198 def resolve_requested_configuration_names(model_name, task_conf, names) if !task_conf if names == ['default'] || names == [] return else section_name = names.find { |n| n != 'default' } raise TaskConfigurations::SectionNotFound.new(section_name), "no configuration available for #{model_name} (expected #{names.join(", ")})" end end # If no names are given try to figure them out if !names || names.empty? if(task_conf.sections.size == 1) [task_conf.sections.keys.first] else ["default"] end else Array(names) end end
Saves the configuration for a task and dumps it to a YAML file
This method adds the current configuration of the given task to the existing configuration(s) for the task's model, and saves all of them in a YAML file.
@param [TaskContext] task the task whose configuration should be saved @param [String] path the file or directory it should be saved to.
If it is a directory, the configuration is saved in a file whose name is based on the task's model name (project_name::TaskName.yml). Otherwise, it is saved in the file. The directories leading to the file must exist.
@option options :model (task.model) the oroGen model used to dump the
configuration
@option options :name (task.name) the name of the section that should
be created
@overload save(task, path, name)
@deprecated old signature. One should use the option hash now.
# File lib/orocos/configurations.rb, line 1238 def save(task, path, options = Hash.new) if options.respond_to?(:to_str) || !options # for backward compatibility options = Hash[:name => options] end options, find_options = Kernel.filter_options options, :name => nil, :model => task.model model_name = options[:model].name task_conf = find_task_configuration_object(task, find_options.merge(:model_name => model_name)) if !task_conf task_conf = conf[model_name] = TaskConfigurations.new(options[:model]) end name = options[:name] || task.name task_conf.extract(name, task) task_conf.save(name, path, task_model: task.model) end