changeset: 2524:a1498ca4aa13
user: Kris Maglione <jg_AT_suckless.org>
date: Fri Oct 09 20:34:23 2009 -0400
files: alternative_wmiircs/python/pygmi/__init__.py alternative_wmiircs/python/pygmi/event.py alternative_wmiircs/python/pygmi/events.py alternative_wmiircs/python/wmiirc.py
description:
Minor changes to pygmi.events API.
diff -r 7f5efccf6aee -r a1498ca4aa13 alternative_wmiircs/python/pygmi/__init__.py
--- a/alternative_wmiircs/python/pygmi/__init__.py Fri Oct 09 16:40:33 2009 -0400
+++ b/alternative_wmiircs/python/pygmi/__init__.py Fri Oct 09 20:34:23 2009 -0400
@@ -14,13 +14,13 @@
sys.path += confpath
from pygmi.util import *
-from pygmi.events import *
+from pygmi.event import *
from pygmi.fs import *
from pygmi.menu import *
from pygmi.monitor import *
-from pygmi import util, events, fs, menu, monitor
+from pygmi import util, event, fs, menu, monitor
-__all__ = (fs.__all__ + monitor.__all__ + events.__all__ +
+__all__ = (fs.__all__ + monitor.__all__ + event.__all__ +
menu.__all__ + util.__all__ +
('client', 'confpath', 'shell'))
diff -r 7f5efccf6aee -r a1498ca4aa13 alternative_wmiircs/python/pygmi/event.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/alternative_wmiircs/python/pygmi/event.py Fri Oct 09 20:34:23 2009 -0400
@@ -0,0 +1,181 @@
+import os
+import re
+import sys
+import traceback
+
+import pygmi
+from pygmi import monitor, client, curry, call, program_list, _
+
+__all__ = ('keys', 'events', 'Match')
+
+class Match(object):
+ def __init__(self, *args):
+ self.args = args
+ self.matchers = []
+ for a in args:
+ if a is _:
+ a = lambda k: True
+ elif isinstance(a, basestring):
+ a = a.__eq__
+ elif isinstance(a, (list, tuple, set)):
+ a = curry(lambda ary, k: k in ary, a)
+ elif hasattr(a, 'search'):
+ a = a.search
+ else:
+ a = str(a).__eq__
+ self.matchers.append(a)
+
+ def match(self, string):
+ ary = string.split(' ', len(self.matchers))
+ if all(m(a) for m, a in zip(self.matchers, ary)):
+ return ary
+
+def flatten(items):
+ for k, v in items:
+ if not isinstance(k, (list, tuple)):
+ k = k,
+ for key in k:
+ yield key, v
+
+class Events():
+ def __init__(self):
+ self.events = {}
+ self.eventmatchers = {}
+ self.alive = True
+
+ def dispatch(self, event, args=''):
+ try:
+ if event in self.events:
+ self.events[event](args)
+ for matcher, action in self.eventmatchers.iteritems():
+ ary = matcher.match(' '.join((event, args)))
+ if ary is not None:
+ action(*ary)
+ except Exception, e:
+ traceback.print_exc(sys.stderr)
+
+ def loop(self):
+ keys.mode = 'main'
+ for line in client.readlines('/event'):
+ if not self.alive:
+ break
+ self.dispatch(*line.split(' ', 1))
+ self.alive = False
+
+ def bind(self, items={}, **kwargs):
+ kwargs.update(items)
+ for k, v in flatten(kwargs.iteritems()):
+ if hasattr(k, 'match'):
+ self.eventmatchers[k] = v
+ else:
+ self.events[k] = v
+
+ def event(self, fn):
+ self.bind({fn.__name__: fn})
+events = Events()
+
+class Keys(object):
+ def __init__(self):
+ self.modes = {}
+ self.modelist = []
+ self.mode = 'main'
+ self.defs = {}
+ events.bind(Key=self.dispatch)
+
+ def _add_mode(self, mode):
+ if mode not in self.modes:
+ self.modes[mode] = {
+ 'name': mode,
+ 'desc': {},
+ 'groups': [],
+ 'keys': {},
+ 'import': {},
+ }
+ self.modelist.append(mode)
+
+ def _set_mode(self, mode):
+ 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')
+ mode = property(lambda self: self._mode, _set_mode)
+
+ @property
+ def help(self):
+ return '\n\n'.join(
+ ('Mode %s\n' % mode['name']) +
+ '\n\n'.join((' %s\n' % str(group or '')) +
+ '\n'.join(' %- 20s %s' % (key % self.defs,
+ mode['keys'][key].__doc__)
+ for key in mode['desc'][group])
+ for group in mode['groups'])
+ for mode in (self.modes[name]
+ for name in self.modelist))
+
+ def bind(self, mode='main', keys=(), import_={}):
+ self._add_mode(mode)
+ mode = self.modes[mode]
+ group = None
+ def add_desc(key, desc):
+ if group not in mode['desc']:
+ mode['desc'][group] = []
+ mode['groups'].append(group)
+ if key not in mode['desc'][group]:
+ mode['desc'][group].append(key);
+
+ if isinstance(keys, dict):
+ keys = keys.iteritems()
+ for obj in keys:
+ if isinstance(obj, tuple) and len(obj) in (2, 3):
+ if len(obj) == 2:
+ key, val = obj
+ desc = ''
+ elif len(obj) == 3:
+ key, desc, val = obj
+ mode['keys'][key] = val
+ add_desc(key, desc)
+ val.__doc__ = str(desc)
+ else:
+ group = obj
+
+ def wrap_import(mode, key):
+ return lambda k: self.modes[mode]['keys'][key](k)
+ for k, v in flatten((v, k) for k, v in import_.iteritems()):
+ mode['import'][k % self.defs] = wrap_import(v, k)
+
+ def dispatch(self, key):
+ mode = self.modes[self.mode]
+ if key in self._keys:
+ return self._keys[key](key)
+keys = Keys()
+
+class Actions(object):
+ def __getattr__(self, name):
+ if name.startswith('_') or name.endswith('_'):
+ raise AttributeError()
+ if hasattr(self, name + '_'):
+ return getattr(self, name + '_')
+ def action(args=''):
+ cmd = pygmi.find_script(name)
+ if cmd:
+ call(pygmi.shell, '-c', '$* %s' % args, '--', cmd,
+ background=True)
+ return action
+
+ def _call(self, args):
+ a = args.split(' ', 1)
+ if a:
+ getattr(self, a[0])(*a[1:])
+
+ @property
+ def _choices(self):
+ return sorted(
+ program_list(pygmi.confpath) +
+ [re.sub('_$', '', k) for k in dir(self)
+ if not re.match('^_', k) and callable(getattr(self, k))])
+
+
+# vim:se sts=4 sw=4 et:
diff -r 7f5efccf6aee -r a1498ca4aa13 alternative_wmiircs/python/pygmi/events.py
--- a/alternative_wmiircs/python/pygmi/events.py Fri Oct 09 16:40:33 2009 -0400
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,180 +0,0 @@
-import os
-import re
-import sys
-import traceback
-
-import pygmi
-from pygmi import monitor, client, curry, call, program_list, _
-
-__all__ = ('keys', 'bind_events', 'event_loop', 'event', 'Match')
-
-keydefs = {}
-events = {}
-eventmatchers = {}
-
-alive = True
-
-class Match(object):
- def __init__(self, *args):
- self.args = args
- self.matchers = []
- for a in args:
- if a is _:
- a = lambda k: True
- elif isinstance(a, basestring):
- a = a.__eq__
- elif isinstance(a, (list, tuple)):
- a = curry(lambda ary, k: k in ary, a)
- elif hasattr(a, 'search'):
- a = a.search
- else:
- a = str(a).__eq__
- self.matchers.append(a)
-
- def match(self, string):
- ary = string.split(' ', len(self.matchers))
- if all(m(a) for m, a in zip(self.matchers, ary)):
- return ary
-
-def flatten(items):
- for k, v in items:
- if not isinstance(k, (list, tuple)):
- k = k,
- for key in k:
- yield key, v
-
-def bind_events(items={}, **kwargs):
- kwargs.update(items)
- for k, v in flatten(kwargs.iteritems()):
- if isinstance(k, Match):
- eventmatchers[k] = v
- else:
- events[k] = v
-
-def event(fn):
- bind_events({fn.__name__: fn})
-
-class Keys(object):
- def __init__(self):
- self.modes = {}
- self.modelist = []
- self.mode = 'main'
- bind_events(Key=self.dispatch)
-
- def _add_mode(self, mode):
- if mode not in self.modes:
- self.modes[mode] = {
- 'name': mode,
- 'desc': {},
- 'groups': [],
- 'keys': {},
- 'import': {},
- }
- self.modelist.append(mode)
-
- def _set_mode(self, mode):
- self._add_mode(mode)
- self._mode = mode
- self._keys = dict((k % keydefs, v) for k, v in
- self.modes[mode]['keys'].items() +
- self.modes[mode]['import'].items());
-
- client.write('/keys', '\n'.join(self._keys.keys()) + '\n')
- mode = property(lambda self: self._mode, _set_mode)
-
- @property
- def help(self):
- return '\n\n'.join(
- ('Mode %s\n' % mode['name']) +
- '\n\n'.join((' %s\n' % str(group or '')) +
- '\n'.join(' %- 20s %s' % (key % keydefs,
- mode['keys'][key].__doc__)
- for key in mode['desc'][group])
- for group in mode['groups'])
- for mode in (self.modes[name]
- for name in self.modelist))
-
- def bind(self, mode='main', keys=(), import_={}):
- self._add_mode(mode)
- mode = self.modes[mode]
- group = None
- def add_desc(key, desc):
- if group not in mode['desc']:
- mode['desc'][group] = []
- mode['groups'].append(group)
- if key not in mode['desc'][group]:
- mode['desc'][group].append(key);
-
- if isinstance(keys, dict):
- keys = keys.iteritems()
- for obj in keys:
- if isinstance(obj, tuple) and len(obj) in (2, 3):
- if len(obj) == 2:
- key, val = obj
- desc = ''
- elif len(obj) == 3:
- key, desc, val = obj
- mode['keys'][key] = val
- add_desc(key, desc)
- val.__doc__ = str(desc)
- else:
- group = obj
-
- def wrap_import(mode, key):
- return lambda k: self.modes[mode]['keys'][key](k)
- for k, v in flatten((v, k) for k, v in import_.iteritems()):
- mode['import'][k % keydefs] = wrap_import(v, k)
-
- def dispatch(self, key):
- mode = self.modes[self.mode]
- if key in self._keys:
- return self._keys[key](key)
-keys = Keys()
-
-def dispatch(event, args=''):
- try:
- if event in events:
- events[event](args)
- for matcher, action in eventmatchers.iteritems():
- ary = matcher.match(' '.join((event, args)))
- if ary is not None:
- action(*ary)
- except Exception, e:
- traceback.print_exc(sys.stderr)
-
-def event_loop():
- from pygmi import events
- keys.mode = 'main'
- for line in client.readlines('/event'):
- if not events.alive:
- break
- dispatch(*line.split(' ', 1))
- events.alive = False
-
-class Actions(object):
- def __getattr__(self, name):
- if name.startswith('_') or name.endswith('_'):
- raise AttributeError()
- if hasattr(self, name + '_'):
- return getattr(self, name + '_')
- def action(args=''):
- cmd = pygmi.find_script(name)
- if cmd:
- call(pygmi.shell, '-c', '$* %s' % args, '--', cmd,
- background=True)
- return action
-
- def _call(self, args):
- a = args.split(' ', 1)
- if a:
- getattr(self, a[0])(*a[1:])
-
- @property
- def _choices(self):
- return sorted(
- program_list(pygmi.confpath) +
- [re.sub('_$', '', k) for k in dir(self)
- if not re.match('^_', k) and callable(getattr(self, k))])
-
-
-# vim:se sts=4 sw=4 et:
diff -r 7f5efccf6aee -r a1498ca4aa13 alternative_wmiircs/python/wmiirc.py
--- a/alternative_wmiircs/python/wmiirc.py Fri Oct 09 16:40:33 2009 -0400
+++ b/alternative_wmiircs/python/wmiirc.py Fri Oct 09 20:34:23 2009 -0400
@@ -6,7 +6,7 @@
import pygmi
from pygmi import *
-from pygmi import events
+from pygmi import event
identity = lambda k: k
@@ -18,7 +18,7 @@
# wmiirc or any other modules it needs.
# Keys
-events.keydefs = dict(
+keys.defs = dict(
mod='Mod4',
left='h',
down='j',
@@ -36,7 +36,7 @@
wmii['font'] = 'drift,-*-fixed-*-*-*-*-9-*-*-*-*-*-*-*'
wmii['normcolors'] = '#000000', '#c1c48b', '#81654f'
wmii['focuscolors'] = '#000000', '#81654f', '#000000'
-wmii['grabmod'] = events.keydefs['mod']
+wmii['grabmod'] = keys.defs['mod']
wmii['border'] = 2
def setbackground(color):
@@ -76,7 +76,7 @@
client.awrite('/event', 'Start wmiirc')
tags = Tags()
-bind_events({
+events.bind({
('Quit', Match('Start', 'wmiirc')): lambda *a: sys.exit(),
'CreateTag': tags.add,
'DestroyTag': tags.delete,
@@ -112,7 +112,7 @@
})
@apply
-class Actions(events.Actions):
+class Actions(event.Actions):
def rehash(self, args=''):
program_menu.choices = program_list(os.environ['PATH'].split(':'))
def showkeys(self, args=''):
@@ -303,6 +303,6 @@
except Exception, e:
traceback.print_exc(sys.stdout)
-event_loop()
+events.loop()
# vim:se sts=4 sw=4 et:
Received on Sun Oct 11 2009 - 09:36:09 UTC
This archive was generated by hypermail 2.2.0 : Sun Oct 11 2009 - 09:48:16 UTC