From ea7227dc538ff0d8033cb285fb9733d4a13f23e9 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Wed, 26 Apr 2017 07:51:11 +0200 Subject: [PATCH] [core] Convert command output to utf-8 Instead of fixing encoding in every individual module (cmus, gpmdp, ...) perform decoding to utf-8 directly in the core. (hopefully) fixes #74 --- bumblebee/modules/gpmdp.py | 10 ++-------- bumblebee/util.py | 12 ++++++++---- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/bumblebee/modules/gpmdp.py b/bumblebee/modules/gpmdp.py index ef74fbe..36988e8 100644 --- a/bumblebee/modules/gpmdp.py +++ b/bumblebee/modules/gpmdp.py @@ -52,13 +52,7 @@ class Module(bumblebee.engine.Module): status = bumblebee.util.execute("gpmdp-remote status") except RuntimeError: pass - try: - unicode_status = status.decode('utf-8') - unicode_info = info.decode('utf-8') - except AttributeError: - unicode_status = status - unicode_info = info - self._status = unicode_status.split("\n")[0].lower() - self._tags = unicode_info.split("\n")[0] + self._status = status.split("\n")[0].lower() + self._tags = info.split("\n")[0] # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/util.py b/bumblebee/util.py index 0e85184..9efa146 100644 --- a/bumblebee/util.py +++ b/bumblebee/util.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + import shlex import logging import subprocess @@ -18,11 +20,13 @@ def execute(cmd, wait=True): out, _ = proc.communicate() if proc.returncode != 0: raise RuntimeError("{} exited with {}".format(cmd, proc.returncode)) - if type(out) == str: - rv = out - else: + + if hasattr(out, "decode"): rv = out.decode("utf-8") - logging.info("command returned '{}'".format("" if not rv else rv)) + else: + rv = out + + logging.info(u"command returned '{}'".format("" if not rv else rv)) return rv def bytefmt(num):