Small python wmiirc changes.

This commit is contained in:
Kris Maglione 2009-10-17 04:34:17 -04:00
parent dd72b1baf7
commit 26db14166b
4 changed files with 60 additions and 32 deletions

View File

@ -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")

View File

@ -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:

View File

@ -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):

View File

@ -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())),