Project

General

Profile

Download (4.98 KB) Statistics
| Branch: | Tag: | Revision:
d32ee48a Marc Dequenes
#--
# CyborgHood, a distributed system management software.
# Copyright (c) 2009 Marc Dequènes (Duck) <Duck@DuckCorp.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++

d31869c4 Marc Dequenes
$KCODE = 'UTF8'
require 'jcode'
require 'singleton'
require 'yaml'
require 'log4r'
7cae0102 Marc Dequènes (Duck)
require 'gettext'
d4b5798a Marc Dequènes (Duck)
require "cyborghood/config"
require "cyborghood/info"
require "cyborghood/lang_additions"
d31869c4 Marc Dequenes
module CyborgHood
7cae0102 Marc Dequènes (Duck)
include GetText
d4b5798a Marc Dequènes (Duck)
bindtextdomain("cyborghood", {:path => Config::L10N_DIR, :charset => "UTF-8"})
d31869c4 Marc Dequenes
cb0d68fd Marc Dequènes (Duck)
# severities: :dangerous :unrecoverable :processable :ignorable
4b58d4fd Marc Dequenes
# categories:
cb0d68fd Marc Dequènes (Duck)
# - db (not a protocol?)
4b58d4fd Marc Dequenes
# + ldap
# + sql
# - protocol
# + imap
cb0d68fd Marc Dequènes (Duck)
# + mail
4b58d4fd Marc Dequenes
# - service
# + dns
323a6bb6 Marc Dequenes
class CyberError < StandardError
4b58d4fd Marc Dequenes
attr_accessor :severity, :category

#include GetTextSupport

def initialize(severity, category, message)
@severity = severity
@category = category
super(message)
end
end

class Logger < Log4r::Logger
d31869c4 Marc Dequenes
include Singleton

790f61f9 Marc Dequenes
LOG_FORMAT = "[%5l - %d] #PREFIX#%m"
d31869c4 Marc Dequenes
def output_level(level)
l = case level
when :verbose
Log4r::DEBUG
when :quiet
Log4r::FATAL
else # normal
Log4r::WARN
end
@main_outputter.level = l
end

def log_to_file(filename)
file_outputter = Log4r::FileOutputter.new(filename, :filename => filename)
file_outputter.formatter = @default_formatter
self.outputters << file_outputter
end

790f61f9 Marc Dequenes
def set_prefix(prefix = "")
prefix ||= ""
log_format = LOG_FORMAT.gsub("#PREFIX#", prefix)
@default_formatter = Log4r::PatternFormatter.new(:pattern => log_format)
self.outputters.each{|outputter| outputter.formatter = @default_formatter }
end

d31869c4 Marc Dequenes
private

def initialize
super(PRODUCT)

self.level = Log4r::DEBUG
@main_outputter = Log4r::Outputter.stdout
@main_outputter.level = Log4r::WARN
self.outputters = [@main_outputter]
790f61f9 Marc Dequenes
set_prefix()
d31869c4 Marc Dequenes
end
end

class Config
include Singleton

CONFFILE_GLOBAL = "cyborghood.conf"
CONFFILE_BOT = "cyborghood_%s.conf"

def self.load(name = nil)
# load all config parts
config = fetch_config
b28fbd1e Marc Dequenes
unless name.nil?
config_spc = fetch_config(name)
config = merge_configs(config, config_spc) if config_spc
end
d31869c4 Marc Dequenes
# create config object for easier access
@@config = config.to_ostruct.freeze
end

def method_missing(sym, *args, &block)
begin
@@config.send sym, *args, &block
rescue
nil
end
end

private

def self.fetch_config(name = nil)
begin
if name.nil?
filename = CONFFILE_GLOBAL
else
filename = sprintf(CONFFILE_BOT, name)
end
filepath = File.join(File.expand_path(CFG_DIR), filename)
str = File.read(filepath)
YAML.load(str)
rescue
logger.fatal "Problems fetching configuration file '" + filepath + "': " + $!
exit 1
end
end

def self.merge_configs(conf1, conf2)
new_conf = conf1.dup
conf2.each_pair do |k, v|
if conf1.has_key?(k) and conf1[k].is_a?(Hash) and v.is_a?(Hash)
new_conf[k] = merge_configs(conf1[k], v)
else
new_conf[k] = v
end
end
return new_conf
end
end
83a04d84 Marc Dequènes (Duck)
class I18n
include Singleton
a19040bb Marc Dequènes (Duck)
include GetText
83a04d84 Marc Dequènes (Duck)
def initialize
@config = Config.instance
end

def available_languages
a19040bb Marc Dequènes (Duck)
list = ['en'] + Dir.new(Config::L10N_DIR).select{|d| File.directory?(d) and d[0..0] != "." }
83a04d84 Marc Dequènes (Duck)
# local admin can restrict available languages
# (may be useful if l10n is partial due to third party plugins)
list = list & @config.i18n.restricted_language_set if @config.i18n.restricted_language_set
list = ['en'] if list.empty?
list
end

def set_language(lang)
set_locale(lang)
end

def set_default_language
set_language('en')
end

def set_language_for_user(user)
if user.nil?
set_default_language
else
logger.debug "User preference for language: " + user.preferredLanguage

lang = user.prefered_language(self.available_languages)
if lang.nil?
logger.debug "No available langage fits the user preference, using english"
lang = 'en'
else
logger.debug "Language better fitting user preference: " + lang
end

set_language(lang)
end
end
end
d31869c4 Marc Dequenes
end