[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
This commit is contained in:
Tobias Witek 2017-04-26 07:51:11 +02:00
parent 94c72a1e6a
commit ea7227dc53
2 changed files with 10 additions and 12 deletions

View file

@ -52,13 +52,7 @@ class Module(bumblebee.engine.Module):
status = bumblebee.util.execute("gpmdp-remote status") status = bumblebee.util.execute("gpmdp-remote status")
except RuntimeError: except RuntimeError:
pass pass
try: self._status = status.split("\n")[0].lower()
unicode_status = status.decode('utf-8') self._tags = info.split("\n")[0]
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]
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-
import shlex import shlex
import logging import logging
import subprocess import subprocess
@ -18,11 +20,13 @@ def execute(cmd, wait=True):
out, _ = proc.communicate() out, _ = proc.communicate()
if proc.returncode != 0: if proc.returncode != 0:
raise RuntimeError("{} exited with {}".format(cmd, proc.returncode)) raise RuntimeError("{} exited with {}".format(cmd, proc.returncode))
if type(out) == str:
rv = out if hasattr(out, "decode"):
else:
rv = out.decode("utf-8") 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 return rv
def bytefmt(num): def bytefmt(num):