diff options
author | Papoteur <papoteur@mageialinux-online.org> | 2015-07-05 11:54:47 +0200 |
---|---|---|
committer | Thierry Vignaud <thierry.vignaud@gmail.com> | 2015-07-25 17:24:10 +0200 |
commit | 4fac47893db3ede8b27c2d682b1f6d9165f180a8 (patch) | |
tree | b60fcca71959fc6599c2464ae39cf95c43292d91 /src/msec/config.py | |
parent | 5134f51bbc3bcc9834cf8a92e1e9b3ab08d04782 (diff) | |
download | msec-4fac47893db3ede8b27c2d682b1f6d9165f180a8.tar msec-4fac47893db3ede8b27c2d682b1f6d9165f180a8.tar.gz msec-4fac47893db3ede8b27c2d682b1f6d9165f180a8.tar.bz2 msec-4fac47893db3ede8b27c2d682b1f6d9165f180a8.tar.xz msec-4fac47893db3ede8b27c2d682b1f6d9165f180a8.zip |
Add a Narg function to deal the encoding strings. #mga13837
v1: Thierry Vignaud
v2: improved by patpoteur
Diffstat (limited to 'src/msec/config.py')
-rwxr-xr-x | src/msec/config.py | 41 |
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 |