Class: Gaudi::Configuration::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/gaudi/helpers/configuration.rb

Overview

Class to load the configuration from a key=value textfile with a few additions.

Configuration classes derived from Loader override the Loader#keys method to specify characteristics for the keys.

Paths are interpreted relative to the configuration file.

In addition to the classic property format (key=value) Loader allows you to split the configuration in multiple files and use import to put them together: import additional.cfg

Also use setenv to set the value of an environment variable

setenv GAUDI=brilliant builder

Direct Known Subclasses

BuildConfiguration, SystemConfiguration

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Loader


124
125
126
127
# File 'lib/gaudi/helpers/configuration.rb', line 124

def initialize filename
  @configuration_files=[File.expand_path(filename)]
  @config=read_configuration(File.expand_path(filename))
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config


123
124
125
# File 'lib/gaudi/helpers/configuration.rb', line 123

def config
  @config
end

#configuration_filesObject (readonly)

Returns all the configuration files processed (e.g. those read with import and the platform configuration files) The main file (the one used in initialize) is always first in the collection


122
123
124
# File 'lib/gaudi/helpers/configuration.rb', line 122

def configuration_files
  @configuration_files
end

Class Method Details

.load(configuration_files, klass) ⇒ Object

Goes through a list of configuration files and returns the resulting merged configuration


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/gaudi/helpers/configuration.rb', line 104

def self.load configuration_files,klass
  cfg=nil
  configuration_files.each do |cfg_file|
    if cfg
      cfg.merge(cfg_file)
    else
      cfg=klass.new(cfg_file)
    end
  end
  if cfg
    return cfg
  else
    raise GaudiConfigurationError, "No #{klass.to_s} configuration files in '#{configuration_files}'"
  end
end

Instance Method Details

#keysObject

Returns an Array containing two arrays.

Override in Loader derived classes.

The first one lists the names of all keys that correspond to list values (comma separated values, e.g. key=value1,value2,value3) Loader will assign an Array of the values to the key, e.g. config=[value1,value2,value3]

The second Array is a list of keys that correspond to pathnames.

Loader will then expand_path the value so that the key contains the absolute path.


138
139
140
# File 'lib/gaudi/helpers/configuration.rb', line 138

def keys
  return [],[]
end

#merge(cfg_file) ⇒ Object

Merges the parameters from cfg_file into this instance


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/gaudi/helpers/configuration.rb', line 145

def merge cfg_file
  begin
    cfg=read_configuration(cfg_file)
    list_keys,path_keys=*keys
    cfg.keys.each do |k|
      if @config.keys.include?(k) && list_keys.include?(k)
        @config[k]+=cfg[k]
      else
        @config[k]=cfg[k] #last one wins
      end
    end
    @configuration_files<<cfg_file
  rescue

  end
end

#read_configuration(filename) ⇒ Object

Reads a configuration file and returns a hash with the configuration as key-value pairs


163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/gaudi/helpers/configuration.rb', line 163

def read_configuration filename
  if File.exists?(filename)
    lines=File.readlines(filename)
    cfg_dir=File.dirname(filename)
    begin
      cfg=parse_content(lines,cfg_dir,*keys)
    rescue GaudiConfigurationError
      raise GaudiConfigurationError,"In #{filename} - #{$!.message}"
    end
  else
    raise GaudiConfigurationError,"Cannot load configuration.'#{filename}' not found"
  end
  return cfg
end

#to_sObject


141
142
143
# File 'lib/gaudi/helpers/configuration.rb', line 141

def to_s
  "Gaudi #{Gaudi::Version::STRING} with #{configuration_files.first}"
end