[hackers] [wmii] Small python wmiirc changes. || Kris Maglione

From: <hg_AT_suckless.org>
Date: Sat, 17 Oct 2009 23:49:03 +0000 (UTC)

changeset: 2553:aa72269e70f8
user: Kris Maglione <jg_AT_suckless.org>
date: Sat Oct 17 04:34:17 2009 -0400
files: alternative_wmiircs/python/pygmi/event.py alternative_wmiircs/python/pygmi/menu.py alternative_wmiircs/python/pygmi/monitor.py alternative_wmiircs/python/wmiirc.py
description:
Small python wmiirc changes.

diff -r 4d85770063e3 -r aa72269e70f8 alternative_wmiircs/python/pygmi/event.py
--- a/alternative_wmiircs/python/pygmi/event.py Sat Oct 17 03:10:37 2009 -0400
+++ b/alternative_wmiircs/python/pygmi/event.py Sat Oct 17 04:34:17 2009 -0400
@@ -163,7 +163,7 @@
         """
         self.modes = {}
         self.modelist = []
- self.mode = 'main'
+ self._set_mode('main', False)
         self.defs = {}
         events.bind(Key=self.dispatch)
 
@@ -178,14 +178,14 @@
             }
             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 -r 4d85770063e3 -r aa72269e70f8 alternative_wmiircs/python/pygmi/menu.py
--- a/alternative_wmiircs/python/pygmi/menu.py Sat Oct 17 03:10:37 2009 -0400
+++ b/alternative_wmiircs/python/pygmi/menu.py Sat Oct 17 04:34:17 2009 -0400
@@ -1,4 +1,5 @@
 from pygmi.util import call
+from threading import Thread
 
 __all__ = 'Menu', 'ClickMenu'
 
@@ -10,7 +11,6 @@
         return res
     if not action:
         return run()
- from threading import Thread
     t = Thread(target=run)
     t.daemon = True
     t.start()
@@ -23,7 +23,7 @@
         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 @@
                 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 @@
         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 @@
             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 -r 4d85770063e3 -r aa72269e70f8 alternative_wmiircs/python/pygmi/monitor.py
--- a/alternative_wmiircs/python/pygmi/monitor.py Sat Oct 17 03:10:37 2009 -0400
+++ b/alternative_wmiircs/python/pygmi/monitor.py Sat Oct 17 04:34:17 2009 -0400
@@ -8,6 +8,16 @@
 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 @@
         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.
 
-class Monitor(object):
+ 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 @@
         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 @@
             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 -r 4d85770063e3 -r aa72269e70f8 alternative_wmiircs/python/wmiirc.py
--- a/alternative_wmiircs/python/wmiirc.py Sat Oct 17 03:10:37 2009 -0400
+++ b/alternative_wmiircs/python/wmiirc.py Sat Oct 17 04:34:17 2009 -0400
@@ -138,8 +138,8 @@
 
 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 @@
 
     "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())),
Received on Sat Oct 17 2009 - 23:49:03 UTC

This archive was generated by hypermail 2.2.0 : Sun Oct 18 2009 - 00:00:14 UTC