Skip to content

Commit

Permalink
put decorators into utils, added "register_if_installed"
Browse files Browse the repository at this point in the history
  • Loading branch information
timo committed Feb 20, 2010
1 parent ec2f5f9 commit a8dd862
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
26 changes: 4 additions & 22 deletions dwmstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,9 @@

from utils import *

PREVIOUS = object()

_statuses = []
_transitions = []

# A decorator to add a function to our statuses list.
def status_func(func):
global _statuses
_statuses.append(func)
return func

# A decorator to add a function to our transitions list.
def transition_func(func):
global _transitions
_transitions.append(func)
return func
STATUSDELAY = 30

# This function allows us to set the text of the status line.
def dwm_set_status(text):
Popen(["xsetroot", "-name", text])
PREVIOUS = object()

def animate(delay, text):
previous = yield PREVIOUS
Expand All @@ -36,7 +19,6 @@ def wait(delay, text):
yield text
sleep(delay)


# install a few status functions

# Status functions call one of these helper functions and yield the result:
Expand Down Expand Up @@ -69,7 +51,7 @@ def memory_free():
yield animate(5, "Used Swap Space")
yield animate(5, pretty_progressbar(used_swap, 80))

@status_func
@register_if_installed("mpc")
def mpd_np():
try:
status_output = Popen(["mpc", "status"], stdout=PIPE).communicate()[0]
Expand All @@ -87,7 +69,7 @@ def mpd_np():
yield animate(5, result)
yield animate(5, lines[1].split()[2])

@status_func
@register_if_installed("acpi")
def battery():
try:
status_output = Popen(["acpi"], stdout=PIPE).communicate()[0]
Expand Down
25 changes: 25 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding: utf-8
# The dwmstatusline utility module.
# Written by Timo Paulssen
from subprocess import Popen, call

def pretty_progressbar(fraction, charsize):
"""Generate a progressbar with accompanying percentage value.
Expand All @@ -24,3 +25,27 @@ def pretty_progressbar(fraction, charsize):
"O",
" " * bar2size,
fraction * 100)

_statuses = []
_transitions = []

# A decorator to add a function to our statuses list.
def status_func(func):
global _statuses
_statuses.append(func)
return func

def register_if_installed(func, command):
if call(["which", command]) == 0:
func = transition_func(func)
return func

# A decorator to add a function to our transitions list.
def transition_func(func):
global _transitions
_transitions.append(func)
return func

# This function allows us to set the text of the status line.
def dwm_set_status(text):
Popen(["xsetroot", "-name", text])

0 comments on commit a8dd862

Please sign in to comment.