Module: Rbbt::Config

Defined in:
lib/rbbt/util/config.rb

Constant Summary collapse

CACHE =
IndiferentHash.setup({})
GOT_KEYS =
[]

Class Method Summary collapse

Class Method Details

.add_entry(key, value, tokens) ⇒ Object



10
11
12
13
14
15
# File 'lib/rbbt/util/config.rb', line 10

def self.add_entry(key, value, tokens)
  tokens = [tokens] unless Array === tokens
  tokens << "key:#{key}" unless tokens.include?("key:#{key}")
  CACHE[key.to_s] ||= [] 
  CACHE[key.to_s] << [tokens, value]
end

.get(key, *tokens) ⇒ Object

For equal priorities the matching prioritizes tokens ealier in the list



89
90
91
92
93
94
95
96
97
98
99
100
101
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
# File 'lib/rbbt/util/config.rb', line 89

def self.get(key, *tokens)
  options = tokens.pop if Hash === tokens.last
  default = options.nil? ? nil : options[:default]

  tokens = ["key:" + key] if tokens.empty?

  tokens = tokens.flatten
  file, _sep, line = caller.reject{|l| 
    l =~ /rbbt\/(?:resource\.rb|workflow\.rb)/ or
      l =~ /rbbt\/resource\/path\.rb/ or
      l =~ /rbbt\/util\/misc\.rb/ or
      l =~ /accessor\.rb/ or
      l =~ /progress-monitor\.rb/ 
  }.first.partition(":")

  File.expand_path(file)

  tokens << ("file:" << file)
  tokens << ("line:" << file << ":" << line.sub(/:in \`.*/,''))

  entries = CACHE[key.to_s]
  priorities = {}
  tokens.each do |token|
    token_prio = match entries, token.to_s
    token_prio.each do |prio, values|
      priorities[prio] ||= []
      priorities[prio].concat(values)
    end
  end

  value = priorities.empty? ? default : priorities.collect{|p| p }.sort_by{|p,v| p}.first.last.first
  value = false if value == 'false'

  Log.debug "Value #{value.inspect} for config key '#{ key }': #{tokens * ", "}"
  GOT_KEYS << [key, value, tokens]

  if String === value && m = value.match(/^env:(.*)/)
    variable = m.captures.first
    ENV[variable]
  elsif value == 'nil'
    nil
  else
    value
  end
end

.load_configObject



27
28
29
30
31
# File 'lib/rbbt/util/config.rb', line 27

def self.load_config
  Rbbt.etc.config.find_all.reverse.each do |file|
    self.load_file(file)
  end
end

.load_file(file) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/rbbt/util/config.rb', line 17

def self.load_file(file)
  Log.debug "Loading config file: #{ file }"
  TSV.traverse file, :type => :array do |line|
    next if line =~ /^#/
    key, value, *tokens = line.strip.split(/\s+/)

    self.add_entry(key, value, tokens) if key
  end
end

.match(entries, give_token) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rbbt/util/config.rb', line 69

def self.match(entries, give_token)
  priorities = {}
  entries.each do |tokens, value|
    best_prio = nil
    tokens = [tokens] unless Array === tokens
    tokens.each do |tok|
      tok, prio = token_priority tok
      next unless tok == give_token

      best_prio = prio if best_prio.nil? or best_prio > prio
      next if prio > best_prio

      priorities[prio] ||= []
      priorities[prio].unshift value
    end
  end if entries
  priorities
end

.process_config(config) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rbbt/util/config.rb', line 149

def self.process_config(config)
  if Misc.is_filename?(config) && File.exist?(config)
    Rbbt::Config.load_file(config)
  elsif Rbbt.etc.config_profile[config].exists?
    Rbbt::Config.load_file(Rbbt.etc.config_profile[config].find)
  else
    key, value, *tokens = config.split(/\s/)
    tokens = tokens.collect do |tok|
      tok, _sep, prio = tok.partition("::")
      prio = "0" if prio.nil? or prio.empty?
      [tok, prio] * "::"
    end
    Rbbt::Config.set({key => value}, *tokens)
  end
end

.set(values, *tokens) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/rbbt/util/config.rb', line 33

def self.set(values, *tokens)
  if not Hash === values
    values = {values => tokens.shift}
  end

  values.each do |key,value|
    add_entry key, value, tokens
  end
end

.token_priority(token) ⇒ Object



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
# File 'lib/rbbt/util/config.rb', line 43

def self.token_priority(token)
  token, _sep, priority = token.to_s.partition("::")

  if priority.nil? || priority.empty?
    type, _sep, rest = token.partition(":")
    priority = case type
               when "workflow"
                 4
               when "task"
                 3
               when "file"
                 2
               when "line"
                 1
               when "key"
                 20
               else
                 10
               end
  else
    priority = priority.to_i
  end

  [token, priority]
end

.with_configObject



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rbbt/util/config.rb', line 135

def self.with_config
    saved_config = {}
    CACHE.each do |k,v|
      saved_config[k] = v.dup
    end
    saved_got_keys = GOT_KEYS.dup
  begin
    yield
  ensure
    CACHE.replace(saved_config)
    GOT_KEYS.replace(saved_got_keys)
  end
end