[core] Minor refactoring

Use a small helper function from util, tidy up some parts of the
output.
This commit is contained in:
Tobi-wan Kenobi 2016-12-02 17:52:05 +01:00
parent a93fa4aa5c
commit f306366629
4 changed files with 18 additions and 23 deletions

View file

@ -1,8 +1,7 @@
import os import os
import shlex
import inspect import inspect
import threading import threading
import subprocess import bumblebee.util
def output(args): def output(args):
import bumblebee.outputs.i3 import bumblebee.outputs.i3
@ -57,8 +56,7 @@ class Command(object):
cmd(self._event, self._widget) cmd(self._event, self._widget)
else: else:
c = cmd.format(*args, **kwargs) c = cmd.format(*args, **kwargs)
DEVNULL = open(os.devnull, 'wb') bumblebee.util.execute(c, False)
subprocess.Popen(shlex.split(c), stdout=DEVNULL, stderr=DEVNULL)
class Output(object): class Output(object):
def __init__(self, config): def __init__(self, config):
@ -103,14 +101,14 @@ class Output(object):
def wait(self): def wait(self):
self._wait.wait(self._config.parameter("interval", 1)) self._wait.wait(self._config.parameter("interval", 1))
def start(self):
pass
def draw(self, widgets, theme): def draw(self, widgets, theme):
if not type(widgets) is list: if not type(widgets) is list:
widgets = [ widgets ] widgets = [ widgets ]
self._draw(widgets, theme) self._draw(widgets, theme)
def start(self):
pass
def _draw(self, widgets, theme): def _draw(self, widgets, theme):
pass pass

View file

@ -51,10 +51,6 @@ class Output(bumblebee.output.Output):
"separator_block_width": 0, "separator_block_width": 0,
}) })
sep = theme.default_separators(widget)
sep = sep if sep else False
width = theme.separator_block_width(widget)
width = width if width else 0
self._data.append({ self._data.append({
u"full_text": " {} {} {}".format( u"full_text": " {} {} {}".format(
theme.prefix(widget), theme.prefix(widget),
@ -65,8 +61,8 @@ class Output(bumblebee.output.Output):
"background": theme.background(widget), "background": theme.background(widget),
"name": widget.module(), "name": widget.module(),
"instance": widget.instance(), "instance": widget.instance(),
"separator": sep, "separator": theme.default_separators(widget, False),
"separator_block_width": width, "separator_block_width": theme.separator_block_width(widget, 0),
}) })
theme.next_widget() theme.next_widget()

View file

@ -89,8 +89,8 @@ class Theme:
def separator(self, widget): def separator(self, widget):
return self._get(widget, "separator") return self._get(widget, "separator")
def default_separators(self, widget): def default_separators(self, widget, default):
return self._get(widget, "default-separators") return self._get(widget, "default-separators", default)
def separator_color(self, widget): def separator_color(self, widget):
return self.background(widget) return self.background(widget)
@ -98,8 +98,8 @@ class Theme:
def separator_background(self, widget): def separator_background(self, widget):
return self._background[1] return self._background[1]
def separator_block_width(self, widget): def separator_block_width(self, widget, default):
return self._get(widget, "separator-block-width") return self._get(widget, "separator-block-width", default)
def _get(self, widget, name, default = None): def _get(self, widget, name, default = None):
module = widget.module() module = widget.module()

View file

@ -21,12 +21,13 @@ def durationfmt(duration):
return res return res
def execute(cmd): def execute(cmd, wait=True):
args = shlex.split(cmd) args = shlex.split(cmd)
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, err = p.communicate()
if p.returncode != 0: if wait:
raise RuntimeError("{} exited with {}".format(cmd, p.returncode)) out, err = p.communicate()
if p.returncode != 0:
return out.decode("utf-8") raise RuntimeError("{} exited with {}".format(cmd, p.returncode))
return out.decode("utf-8")
return None