From 0d58eff6a2ec9757a923e1aa548becada2f66349 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 31 Dec 2020 23:45:48 +0100 Subject: [PATCH] Misc encoding fixes --- moulinette/authenticators/ldap.py | 16 ++++++++++++++-- moulinette/interfaces/cli.py | 10 ++-------- moulinette/utils/log.py | 4 ++-- moulinette/utils/process.py | 2 +- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/moulinette/authenticators/ldap.py b/moulinette/authenticators/ldap.py index 2fdc1946..eed25b70 100644 --- a/moulinette/authenticators/ldap.py +++ b/moulinette/authenticators/ldap.py @@ -15,7 +15,6 @@ from moulinette.authenticators import BaseAuthenticator logger = logging.getLogger("moulinette.authenticator.ldap") - # LDAP Class Implementation -------------------------------------------- @@ -60,7 +59,7 @@ class Authenticator(BaseAuthenticator): def __del__(self): """Disconnect and free ressources""" - if self.con: + if hasattr(self, "con") and self.con: self.con.unbind_s() # Implement virtual properties @@ -150,6 +149,19 @@ class Authenticator(BaseAuthenticator): for dn, entry in result: entry["dn"] = [dn] result_list.append(entry) + + def decode(value): + if isinstance(value, bytes): + value = value.decode('utf-8') + return value + + # result_list is for example : + # [{'virtualdomain': [b'test.com']}, {'virtualdomain': [b'yolo.test']}, + for stuff in result_list: + if isinstance(stuff, dict): + for key, values in stuff.items(): + stuff[key] = [decode(v) for v in values] + return result_list def add(self, rdn, attr_dict): diff --git a/moulinette/interfaces/cli.py b/moulinette/interfaces/cli.py index 098568e1..aec63fc8 100644 --- a/moulinette/interfaces/cli.py +++ b/moulinette/interfaces/cli.py @@ -156,15 +156,11 @@ def pretty_print_dict(d, depth=0): elif isinstance(value, dict): pretty_print_dict({key: value}, depth + 1) else: - if isinstance(value, str): - value = value.encode("utf-8") - elif isinstance(v, date): + if isinstance(v, date): v = pretty_date(v) print("{:s}- {}".format(" " * (depth + 1), value)) else: - if isinstance(v, str): - v = v.encode("utf-8") - elif isinstance(v, date): + if isinstance(v, date): v = pretty_date(v) print("{:s}{}: {}".format(" " * depth, k, v)) @@ -532,8 +528,6 @@ class Interface(BaseInterface): Handle the core.MoulinetteSignals.display signal. """ - if isinstance(message, str): - message = message.encode("utf-8") if style == "success": print("{} {}".format(colorize(m18n.g("success"), "green"), message)) elif style == "warning": diff --git a/moulinette/utils/log.py b/moulinette/utils/log.py index e1a902b5..65006d2e 100644 --- a/moulinette/utils/log.py +++ b/moulinette/utils/log.py @@ -101,7 +101,7 @@ class MoulinetteLogger(Logger): if self.isEnabledFor(SUCCESS): self._log(SUCCESS, msg, args, **kwargs) - def findCaller(self): + def findCaller(self, *args): """Override findCaller method to consider this source file.""" f = logging.currentframe() if f is not None: @@ -125,7 +125,7 @@ class MoulinetteLogger(Logger): # FIXME: Get real action_id instead of logger/current one extra["action_id"] = _get_action_id() kwargs["extra"] = extra - return Logger._log(self, *args, **kwargs) + return super()._log(*args, **kwargs) # Action logging ------------------------------------------------------- diff --git a/moulinette/utils/process.py b/moulinette/utils/process.py index ba3fd911..f945cd54 100644 --- a/moulinette/utils/process.py +++ b/moulinette/utils/process.py @@ -28,7 +28,7 @@ def check_output(args, stderr=subprocess.STDOUT, shell=True, **kwargs): and use shell by default before calling subprocess.check_output. """ - return subprocess.check_output(args, stderr=stderr, shell=shell, **kwargs).strip() + return subprocess.check_output(args, stderr=stderr, shell=shell, **kwargs).decode('utf-8').strip() # Call with stream access ----------------------------------------------