aboutsummaryrefslogtreecommitdiffstats
path: root/src/msec/config.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/msec/config.py')
-rwxr-xr-xsrc/msec/config.py41
1 files changed, 32 insertions, 9 deletions
diff --git a/src/msec/config.py b/src/msec/config.py
index c8e87f6..9f41998 100755
--- a/src/msec/config.py
+++ b/src/msec/config.py
@@ -81,6 +81,9 @@ SETTINGS_PERIODIC = []
# checks that support exceptions - defined by 'audit' plugin
CHECKS_WITH_EXCEPTIONS = []
+# system encoding
+SYS_ENC = sys.getfilesystemencoding()
+
# localized help
try:
from help import HELP
@@ -168,6 +171,26 @@ def merge_with_baselevel(log, config, base_level, load_func, root=''):
config.merge(levelconf)
+def to_utf8(s):
+ """ Returs string after decoding if needed """
+ try:
+ s.decode()
+ return s
+ except:
+ return str(s).decode("utf-8")
+
+def Narg(s):
+ """ Returs string after decoding if needed """
+ try:
+ s.decode()
+ return s
+ except:
+ try:
+ t=str(s)
+ except:
+ return s
+ return t.decode(SYS_ENC)
+
# {{{ MsecConfig
class MsecConfig:
"""Msec configuration parser"""
@@ -208,7 +231,7 @@ class MsecConfig:
try:
fd = open(self.config)
except:
- self.log.error(_("Unable to load configuration file %s: %s") % (self.config, sys.exc_value[1]))
+ self.log.error(_("Unable to load configuration file %s: %s") % (self.config, Narg(sys.exc_value[1])))
return False
for line in fd.readlines():
line = line.strip()
@@ -222,7 +245,7 @@ class MsecConfig:
option, val = line.split("=", 1)
self.options[option] = val
except:
- self.log.warn(_("Bad config option: %s") % line)
+ self.log.warn(_("Bad config option: %s") % Narg(line))
continue
fd.close()
return True
@@ -257,7 +280,7 @@ class MsecConfig:
try:
fd = open(self.config, "w")
except:
- self.log.error(_("Unable to save %s: %s") % (self.config, sys.exc_value))
+ self.log.error(_("Unable to save %s: %s") % (self.config, Narg(sys.exc_value)))
return False
for comment in self.comments:
print >>fd, comment
@@ -316,7 +339,7 @@ class ExceptionConfig:
fd = open(self.config)
except:
# this file is optional, so if it is not found that's not fatal
- self.log.info(_("loading exceptions file %s: %s") % (self.config, sys.exc_value[1]))
+ self.log.info(_("loading exceptions file %s: %s") % (self.config, Narg(sys.exc_value[1])))
self.log.info(_("No exceptions loaded"))
return False
for line in fd.readlines():
@@ -331,7 +354,7 @@ class ExceptionConfig:
option, val = line.split(" ", 1)
self.options.append((option, val))
except:
- self.log.warn(_("Bad config option: %s") % line)
+ self.log.warn(_("Bad config option: %s") % Narg(line))
continue
fd.close()
return True
@@ -370,7 +393,7 @@ class ExceptionConfig:
try:
fd = open(self.config, "w")
except:
- self.log.error(_("Unable to save %s: %s") % (self.config, sys.exc_value))
+ self.log.error(_("Unable to save %s: %s") % (self.config, Narg(sys.exc_value)))
return False
for comment in self.comments:
print >>fd, comment
@@ -420,7 +443,7 @@ class PermConfig(MsecConfig):
try:
fd = open(self.config)
except:
- self.log.error(_("Unable to load configuration file %s: %s") % (self.config, sys.exc_value))
+ self.log.error(_("Unable to load configuration file %s: %s") % (self.config, Narg(sys.exc_value)))
return False
for line in fd.readlines():
line = line.strip()
@@ -439,7 +462,7 @@ class PermConfig(MsecConfig):
self.options_order.append(file)
except:
traceback.print_exc()
- self.log.warn(_("Bad config option: %s") % line)
+ self.log.warn(_("Bad config option: %s") % Narg(line))
continue
fd.close()
return True
@@ -465,7 +488,7 @@ class PermConfig(MsecConfig):
try:
fd = open(self.config, "w")
except:
- self.log.error(_("Unable to save %s: %s") % (self.config, sys.exc_value))
+ self.log.error(_("Unable to save %s: %s") % (self.config, Narg(sys.exc_value)))
return False
for comment in self.comments:
print >>fd, comment