Misc encoding fixes

This commit is contained in:
Alexandre Aubin 2020-12-31 23:45:48 +01:00
parent d3c7d12457
commit 0d58eff6a2
4 changed files with 19 additions and 13 deletions

View file

@ -15,7 +15,6 @@ from moulinette.authenticators import BaseAuthenticator
logger = logging.getLogger("moulinette.authenticator.ldap") logger = logging.getLogger("moulinette.authenticator.ldap")
# LDAP Class Implementation -------------------------------------------- # LDAP Class Implementation --------------------------------------------
@ -60,7 +59,7 @@ class Authenticator(BaseAuthenticator):
def __del__(self): def __del__(self):
"""Disconnect and free ressources""" """Disconnect and free ressources"""
if self.con: if hasattr(self, "con") and self.con:
self.con.unbind_s() self.con.unbind_s()
# Implement virtual properties # Implement virtual properties
@ -150,6 +149,19 @@ class Authenticator(BaseAuthenticator):
for dn, entry in result: for dn, entry in result:
entry["dn"] = [dn] entry["dn"] = [dn]
result_list.append(entry) 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 return result_list
def add(self, rdn, attr_dict): def add(self, rdn, attr_dict):

View file

@ -156,15 +156,11 @@ def pretty_print_dict(d, depth=0):
elif isinstance(value, dict): elif isinstance(value, dict):
pretty_print_dict({key: value}, depth + 1) pretty_print_dict({key: value}, depth + 1)
else: else:
if isinstance(value, str): if isinstance(v, date):
value = value.encode("utf-8")
elif isinstance(v, date):
v = pretty_date(v) v = pretty_date(v)
print("{:s}- {}".format(" " * (depth + 1), value)) print("{:s}- {}".format(" " * (depth + 1), value))
else: else:
if isinstance(v, str): if isinstance(v, date):
v = v.encode("utf-8")
elif isinstance(v, date):
v = pretty_date(v) v = pretty_date(v)
print("{:s}{}: {}".format(" " * depth, k, v)) print("{:s}{}: {}".format(" " * depth, k, v))
@ -532,8 +528,6 @@ class Interface(BaseInterface):
Handle the core.MoulinetteSignals.display signal. Handle the core.MoulinetteSignals.display signal.
""" """
if isinstance(message, str):
message = message.encode("utf-8")
if style == "success": if style == "success":
print("{} {}".format(colorize(m18n.g("success"), "green"), message)) print("{} {}".format(colorize(m18n.g("success"), "green"), message))
elif style == "warning": elif style == "warning":

View file

@ -101,7 +101,7 @@ class MoulinetteLogger(Logger):
if self.isEnabledFor(SUCCESS): if self.isEnabledFor(SUCCESS):
self._log(SUCCESS, msg, args, **kwargs) self._log(SUCCESS, msg, args, **kwargs)
def findCaller(self): def findCaller(self, *args):
"""Override findCaller method to consider this source file.""" """Override findCaller method to consider this source file."""
f = logging.currentframe() f = logging.currentframe()
if f is not None: if f is not None:
@ -125,7 +125,7 @@ class MoulinetteLogger(Logger):
# FIXME: Get real action_id instead of logger/current one # FIXME: Get real action_id instead of logger/current one
extra["action_id"] = _get_action_id() extra["action_id"] = _get_action_id()
kwargs["extra"] = extra kwargs["extra"] = extra
return Logger._log(self, *args, **kwargs) return super()._log(*args, **kwargs)
# Action logging ------------------------------------------------------- # Action logging -------------------------------------------------------

View file

@ -28,7 +28,7 @@ def check_output(args, stderr=subprocess.STDOUT, shell=True, **kwargs):
and use shell by default before calling subprocess.check_output. 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 ---------------------------------------------- # Call with stream access ----------------------------------------------