Class: WCC::Conf

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/wcc.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConf

Returns a new instance of Conf.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/wcc.rb', line 112

def initialize
	@options = {}
	
	OptionParser.new do |opts|
		opts.banner =  "Usage: ruby wcc.rb [options] [config-yaml-file]"
		opts.banner += "\nOptions:\n"
		opts.on('-v', '--verbose', 'Output more information') do self[:verbose] = true end
		opts.on('-d', '--debug', 'Enable debug mode') do self[:debug] = true end
		opts.on('--cache-dir DIR', 'Save hash and diff files to DIR') do |dir| self[:cache_dir] = dir end
		opts.on('-s', '--simulate', 'Check for update but do not save hash or diff files') do self[:simulate] = true end
		opts.on('--clean', 'Remove all saved hash and diff files') do self[:clean] = true end
		opts.on('-t', '--tag TAG', 'Set TAG used in output') do |t| self[:tag] = t end
		opts.on('-n', '--no-mails', 'Do not notify users in any way') do self[:nomails] = true end
		opts.on('--show-config', 'Show config after loading config file (debug purposes)') do self[:show_config] = true end
		opts.on('-h', '-?', '--help', 'Display this screen') do
			puts opts
			exit
		end
	end.parse!
	
	WCC.logger.progname = 'wcc'

	# latest flag overrides everything
	WCC.logger.level = Logger::ERROR
	WCC.logger.level = Logger::INFO if self[:verbose]
	WCC.logger.level = Logger::DEBUG if self[:debug]
	
	WCC.logger.formatter = LogFormatter.new((self[:verbose] or self[:debug]))

	# main
	WCC.logger.info "web change checker (aka wcc) #{WCC::VERSION}"
	WCC.logger.info "Licensed under Apache License Version 2.0"
	
	WCC.logger.info "No config file given, using default 'conf.yml' file" if ARGV.length == 0

	self[:conf] = ARGV[0] || 'conf.yml'
	
	if !File.exists?(self[:conf])
		WCC.logger.fatal "Config file '#{self[:conf]}' does not exist!"
		Prog.exit 1
	end
	
	# register standard notificators - these are already loaded
	Notificators.map 'email', MailNotificator
	Notificators.map 'syslog', SyslogNotificator
	
	WCC.logger.debug "Load config from '#{self[:conf]}'"
	
	# may be false if file is empty
	yaml = YAML.load_file(self[:conf])
	
	# inject dummy value {} for 'email' in 'conf' section to make the parser
	# load MailNotificator and it's defaults even if the key is missing
	# since email has always been the backbone of wcc
	yaml = {'conf' => {'email' => {}}}.recursive_merge(yaml) if yaml.is_a?(Hash)
	
	if yaml.is_a?(Hash) and yaml['conf'].is_a?(Hash)
		yaml['conf'].each do |key,val|
			case key
			when 'cache_dir'
				@options[:cache_dir] ||= val
			when 'tag'
				@options[:tag] ||= val
			when 'filterd'
				@options[:filter_dir] ||= val
			when 'templated'
				@options[:template_dir] ||= val
			when 'stats'
				@options[:stats] ||= val
			else
				if not Notificators.mappings.include?(key)
					plugin_name = "wcc-#{key}-notificator"
					WCC.logger.info "Trying to load plugin #{plugin_name}..."
					begin
						require plugin_name
					rescue LoadError
						WCC.logger.error "Plugin #{plugin_name} not found - maybe try `gem install #{plugin_name}`"
						next
					end
				end
				Notificators.mappings[key].parse_conf(val).each { |k,v| @options[k] ||= v }
			end
		end
	end
	
	if self[:show_config]
		Conf.default.merge(@options).each do |k,v|
			puts "  #{k.to_s} => #{self[k]}"
		end
		Prog.exit 0
	end
	
	@recipients = {}
	WCC.logger.debug "Load recipients from '#{self[:conf]}'"
	# may be *false* if file is empty
	yaml = YAML.load_file(self[:conf])
	if not yaml
		WCC.logger.info "No recipients loaded"
	else
		yaml['recipients'].to_a.each do |yaml_rec|
			name = yaml_rec.keys.first
			rec = []
			yaml_rec[name].to_a.each do |yaml_way|
				# TODO: find options and pass them to every notificator
				if yaml_way.is_a?(Hash)
					new_notificator(name, yaml_way.keys.first, yaml_way[yaml_way.keys.first], rec)
				else
					new_notificator(name, yaml_way, nil, rec)
				end
			end
			@recipients[name] = rec
		end
	end
	
	# attach --no-mails filter
	WCC::Filters.add '--no-mails' do |data|
		!self[:nomails]
	end
end

Instance Attribute Details

#recipientsObject (readonly)

Returns the value of attribute recipients.



86
87
88
# File 'lib/wcc.rb', line 86

def recipients
  @recipients
end

Class Method Details

.[](key) ⇒ Object



286
# File 'lib/wcc.rb', line 286

def self.[](key); Conf.instance[key] end

.defaultObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/wcc.rb', line 96

def self.default
	@default_conf ||= {
		:verbose => false,
		:debug => false,
		:simulate => false,
		:clean => false,
		:nomails => false,
		:stats => false,
		# when you want to use ./tmp it must be writeable
		:cache_dir => '/var/tmp/wcc',
		:tag => 'wcc',
		:filter_dir => './filter.d',
		:template_dir => './template.d'
	}
end

.file(path = nil) ⇒ Object



284
# File 'lib/wcc.rb', line 284

def self.file(path = nil); File.join(self[:cache_dir], path) end

.recipientsObject



280
281
282
# File 'lib/wcc.rb', line 280

def self.recipients
	return Conf.instance.recipients
end

.simulate?Boolean

Returns:

  • (Boolean)


285
# File 'lib/wcc.rb', line 285

def self.simulate?; self[:simulate] end

.sitesObject



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/wcc.rb', line 232

def self.sites
	return @sites unless @sites.nil?
	
	@sites = []
	
	WCC.logger.debug "Load sites from '#{Conf[:conf]}'"
	# may be *false* if file is empty
	yaml = YAML.load_file(Conf[:conf])
	if not yaml
		WCC.logger.info "No sites loaded"
		return @sites
	end
	
	yaml['sites'].to_a.each do |yaml_site|
		# query --no-mails filter for every site
		frefs = [FilterRef.new('--no-mails')]
		(yaml_site['filters'] || []).each do |entry|
			if entry.is_a?(Hash)
				# hash containing only one key (filter id),
				# the value is the argument hash
				id = entry.keys[0]
				frefs << FilterRef.new(id, entry[id])
			else entry.is_a?(String)
				frefs << FilterRef.new(entry)
			end
		end
		
		if not yaml_site['cookie'].nil?
			cookie = File.open(yaml_site['cookie'], 'r') { |f| f.read }
		end
		
		@sites << Site.new(
			yaml_site['url'], 
			yaml_site['strip_html'] || true,
			yaml_site['notify'] || [],
			frefs,
			yaml_site['auth'] || {},
			cookie,
			yaml_site['check_interval'] || 5
		)
	end
	
	WCC.logger.debug @sites.length.to_s + (@sites.length == 1 ? ' site' : ' sites') + " loaded\n" +
		@sites.map { |s| "  #{s.uri.host.to_s}\n    url: #{s.uri.to_s}\n    id: #{s.id}" }.join("\n")
	
	@sites
end

Instance Method Details

#[](key) ⇒ Object

use Conf like a hash containing all options



89
90
91
# File 'lib/wcc.rb', line 89

def [](key)
	@options[key.to_sym] || Conf.default[key.to_sym]
end

#[]=(key, val) ⇒ Object



92
93
94
# File 'lib/wcc.rb', line 92

def []=(key, val)
	@options[key.to_sym] = val unless val.nil?
end