wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

commit 26db14166bb490db97437bc6c48702e6951b4f33
parent dd72b1baf7541a0c9b04087ae8ca34b4cf9cfbf9
Author: Kris Maglione <jg@suckless.org>
Date:   Sat, 17 Oct 2009 04:34:17 -0400

Small python wmiirc changes.

Diffstat:
alternative_wmiircs/python/pygmi/event.py | 8++++----
alternative_wmiircs/python/pygmi/menu.py | 8+++++---
alternative_wmiircs/python/pygmi/monitor.py | 64+++++++++++++++++++++++++++++++++++++++++++++-------------------
alternative_wmiircs/python/wmiirc.py | 12++++++------
4 files changed, 60 insertions(+), 32 deletions(-)

diff --git a/alternative_wmiircs/python/pygmi/event.py b/alternative_wmiircs/python/pygmi/event.py @@ -163,7 +163,7 @@ class Keys(object): """ self.modes = {} self.modelist = [] - self.mode = 'main' + self._set_mode('main', False) self.defs = {} events.bind(Key=self.dispatch) @@ -178,14 +178,14 @@ class Keys(object): } self.modelist.append(mode) - def _set_mode(self, mode): + def _set_mode(self, mode, execute=True): self._add_mode(mode) self._mode = mode self._keys = dict((k % self.defs, v) for k, v in self.modes[mode]['keys'].items() + self.modes[mode]['import'].items()); - - client.write('/keys', '\n'.join(self._keys.keys()) + '\n') + if execute: + client.write('/keys', '\n'.join(self._keys.keys()) + '\n') mode = property(lambda self: self._mode, _set_mode, doc="The current mode for which to dispatch keys") diff --git a/alternative_wmiircs/python/pygmi/menu.py b/alternative_wmiircs/python/pygmi/menu.py @@ -1,4 +1,5 @@ from pygmi.util import call +from threading import Thread __all__ = 'Menu', 'ClickMenu' @@ -10,7 +11,6 @@ def inthread(fn, action): return res if not action: return run() - from threading import Thread t = Thread(target=run) t.daemon = True t.start() @@ -23,7 +23,7 @@ class Menu(object): self.histfile = histfile self.nhist = nhist - def call(self, choices=None): + def __call__(self, choices=None): if choices is None: choices = self.choices if callable(choices): @@ -36,6 +36,7 @@ class Menu(object): args += ['-n', self.nhist] return call(*map(str, args), input='\n'.join(choices)) return inthread(act, self.action) + call = __call__ class ClickMenu(object): def __init__(self, choices=(), action=None, @@ -44,7 +45,7 @@ class ClickMenu(object): self.action = action self.prev = None - def call(self, choices=None): + def __call__(self, choices=None): if choices is None: choices = self.choices if callable(choices): @@ -56,5 +57,6 @@ class ClickMenu(object): args += ['--'] + list(choices) return call(*map(str, args)).replace('\n', '') return inthread(act, self.action) + call = __call__ # vim:se sts=4 sw=4 et: diff --git a/alternative_wmiircs/python/pygmi/monitor.py b/alternative_wmiircs/python/pygmi/monitor.py @@ -8,6 +8,16 @@ __all__ = 'monitors', 'defmonitor', 'Monitor' monitors = {} def defmonitor(*args, **kwargs): + """ + Defines a new monitor to appear in wmii's bar based on + the wrapped function. Creates a new Monitor object, + initialized with *args and **kwargs. The wrapped function + is assigned to the 'action' keyword argument for the + Monitor, its name is assigned to the 'name' argument. + + The new monitor is added to the 'monitors' dict in this + module. + """ def monitor(fn): kwargs['action'] = fn if not args and 'name' not in kwargs: @@ -21,25 +31,35 @@ def defmonitor(*args, **kwargs): return monitor(fn) return monitor -class MonitorBase(type): - def __new__(cls, name, bases, attrs): - new_cls = super(MonitorBase, cls).__new__(cls, name, bases, attrs) - if name not in attrs: - new_cls.name = new_cls.__name__.lower() - try: - Monitor - if new_cls.name not in monitors: - monitors[new_cls.name] = new_cls() - except Exception, e: - pass - return new_cls - class Monitor(object): + """ + A class to manage status monitors for wmii's bar. The bar item + is updated on a fixed interval based on the values returned + by the 'action' method. + + Property active: When true, the monitor is updated at regular + intervals. When false, monitor is hidden. + Property name: The name of the monitor, which acts as the name + of the bar in wmii's filesystem. + Property interval: The update interval, in seconds. + Property side: The side of the bar on which to place the monitor. + Property action: A function of no arguments which returns the + value of the monitor. Called at each update interval. + May return a string, a tuple of (Color, string), or None + to hide the monitor for one iteration. + """ side = 'right' interval = 1.0 def __init__(self, name=None, interval=None, side=None, action=None, colors=None, label=None): + """ + Initializes the new monitor. For parameter values, see the + corresponding property values in the class's docstring. + + Param color: The initial colors for the monitor. + Param label: The initial label for the monitor. + """ if side: self.side = side if name: @@ -54,6 +74,10 @@ class Monitor(object): self.tick() def tick(self): + """ + Called internally at the interval defined by #interval. + Calls #action and updates the monitor based on the result. + """ mon = monitors.get(self.name, None) if self.timer and mon is not self: return @@ -71,12 +95,14 @@ class Monitor(object): self.timer.start() def getlabel(self): - if self.action: - try: - return self.action(self) - except Exception: - pass - return None + """ + Calls #action and returns the result, ignoring any + exceptions. + """ + try: + return self.action(self) + except Exception: + return None _active = True def _set_active(self, val): diff --git a/alternative_wmiircs/python/wmiirc.py b/alternative_wmiircs/python/wmiirc.py @@ -138,8 +138,8 @@ tag_menu = Menu(histfile='%s/history.tags' % confpath[0], nhist=100, def clickmenu(choices, args): ClickMenu(choices=(k for k, v in choices), - action=lambda choice: dict(choices).get(choice, identity)(*args)) \ - .call() + action=lambda choice: dict(choices).get(choice, identity)(*args) + ).call() class Notice(Button): def __init__(self): @@ -211,18 +211,18 @@ keys.bind('main', ( "Running programs", ('%(mod)s-a', "Open wmii actions menu", - lambda k: action_menu.call()), + lambda k: action_menu()), ('%(mod)s-p', "Open program menu", - lambda k: program_menu.call()), + lambda k: program_menu()), ('%(mod)s-Return', "Launch a terminal", lambda k: call(*terminal, background=True)), "Tag actions", ('%(mod)s-t', "Change to another tag", - lambda k: tags.select(tag_menu.call())), + lambda k: tags.select(tag_menu())), ('%(mod)s-Shift-t', "Retag the selected client", - lambda k: setattr(Client('sel'), 'tags', tag_menu.call())), + lambda k: setattr(Client('sel'), 'tags', tag_menu())), ('%(mod)s-n', "Move to the view to the left", lambda k: tags.select(tags.next())),