module Buryspam::Config

Module to process and store buryspam's configuration file and any configuration parameters given as the argument to the --override command line option.

Constants

COMPULSORY

Configuration parameters that must be set.

META_CACHE

The values of the following configuration parameters are stored in the cache metadata file. If any of them are changed, then the cache is outdated and will have to be regenerated.

Param

Structure to hold the type, default value, validation proc, error message, value and initialization status of each configuration parameter. The init field is true if value has been set, and nil otherwise. (Note that value may legitimately be set to nil, so we can't use value.nil? as a test for whether value has been initialized.)

RCFILE

The configuration file, itself.

Attributes

config[RW]

Public Class Methods

process(overrides) click to toggle source

Process and store configuration parameters from the rc file and on the command line. Set defaults for parameters that are not set.

# File buryspam.rb, line 2418
def process(overrides)
  # Parse .buryspamrc configuration file.
  parse_config
  # Override configuration file with any command line settings (--override).
  parse_overrides(overrides)
  # If any parameters are still not set, then use their defaults.
  set_defaults
end

Private Class Methods

parse_config() click to toggle source

Parse the entire configuration file, storing the settings and notifying the user of any problems.

# File buryspam.rb, line 2431
def parse_config
  errors = []
  File.open(RCFILE, 'rb') { |f|
    f.each { |line|
      if err_str = parse_line(line)
        errors << "line #{f.lineno}: > #{line.chomp}\n  " + err_str
      end
    }
  }
  unless errors.empty?
    raise Error, "File '#{RCFILE}' had errors:\n" + errors.join("\n")
  end
end
parse_line(line) click to toggle source

Parse a line from either the configuration file or the command line's --override option. Returns an error string if there were problems; nil otherwise.

# File buryspam.rb, line 2488
def parse_line(line)
  begin
    line.strip!
    return nil if line.empty? || line[0] == ##
    raise Error, "Missing '='" unless /(.*?)=(.*)$/.match(line)
    attrib, value = $1.strip.to_sym, $2
    value_str = value.gsub(/^[\s"']*(.*?)[\s"']*$/, '\1')
    Converter.store(attrib, value_str)
  rescue Error
    return $!.message
  end
  return nil
end
parse_overrides(overrides) click to toggle source

Parse all the parameters specified by any --override command line arguments.

# File buryspam.rb, line 2447
def parse_overrides(overrides)
  return if overrides.nil? || overrides.empty?
  errors = []
  overrides.each { |override|
    override.strip.split(/;/).each { |setting|
      if err_str = parse_line(setting)
        errors << "> #{setting}\n  " + err_str
      end
    }
  }
  unless errors.empty?
    raise Error, "--override argument invalid:\n" + errors.join("\n")
  end
end
set_defaults() click to toggle source

Identify any unset configuration parameters and set their values to their respective default values. If any compulsory parameters have not be set, then raise an error.

# File buryspam.rb, line 2465
def set_defaults
  missing = []
  @config.each { |attrib, param|
    next if param.init
    if COMPULSORY.include?(attrib.to_s)
      missing << attrib
    else
      begin
        Converter.store(attrib, param.dflt_value)
      rescue Error
        raise Error, "Internal parameter error for '#{attrib}': #$!\n"
      end
    end
  }
  return if missing.empty?  # No missing parameters, everything's okay.
  raise Error,
    "Compulsory parameter%s not defined in '#{RCFILE}' file:\n\t%s" %
    [missing.size == 1 ? "" : "s", missing.map{|p| "'#{p}'"}.join(",\n\t")]
end