File: RuntimeConfig.rb

package info (click to toggle)
tj3 3.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,048 kB
  • sloc: ruby: 36,481; javascript: 1,113; sh: 19; makefile: 17
file content (97 lines) | stat: -rw-r--r-- 2,457 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = RuntimeConfig.rb -- The TaskJuggler III Project Management Software
#
# Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
#               by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#

require 'yaml'

# The RuntimeConfig searches for a YAML config file in a list of directories.
# When a file is found it is read-in. The read-in config values are grouped in
# a tree of sections. The values of a section can then be used to overwrite
# the instance variable of a passed object.
class RuntimeConfig

  attr_accessor :debugMode

  def initialize(appName, configFile = nil)
    @appName = appName
    @config = nil
    @debugMode = false

    if configFile
      # Read user specified config file.
      unless loadConfigFile(configFile)
        error("Config file #{configFile} not found!")
      end
    else
      # Search config files in certain directories.
      [ '.', ENV['HOME'], '/etc' ].each do |path|
        # Try UNIX style hidden file first, then .rc.
        [ "#{path}/.#{appName}rc", "#{path}/#{appName}.rc" ].each do |file|
          break if loadConfigFile(file)
        end
      end
    end
  end

  def configure(object, section)
    debug("Configuring object of type #{object.class}")
    sections = section.split('.')
    return false unless (p = @config)
    sections.each do |sec|
      p = p['_' + sec]
      unless p && p.is_a?(Hash)
        debug("Section #{section} not found in config file")
        return false
      end
    end

    object.instance_variables.each do |iv|
      ivName = iv[1..-1]
      debug("Processing class variable #{ivName}")
      if p.include?(ivName)
        debug("Setting @#{ivName} to #{p[ivName]}")
        object.instance_variable_set(iv, p[ivName])
      end
    end

    true
  end

  private

  def loadConfigFile(fileName)
    if File.exist?(fileName)
      debug("Loading #{fileName}")
      begin
        @config = YAML::load(File.read(fileName))
      rescue
        error("Error in config file #{fileName}: #{$!}")
      end
      debug(@config.to_s)
      return true
    end
    false
  end

  def debug(message)
    return unless @debugMode

    puts message
  end

  def error(message)
    $stderr.puts message
    exit 1
  end

end