1import os
2import subprocess
3
4import pygmi
5
6__all__ = 'call', 'message', 'program_list', 'curry', 'find_script', '_', 'prop'
7
8def _():
9    pass
10
11def call(*args, **kwargs):
12    background = kwargs.pop('background', False)
13    input = kwargs.pop('input', None)
14    p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
15                         stderr=subprocess.PIPE, cwd=os.environ['HOME'],
16                         close_fds=True, **kwargs)
17    if not background:
18        return p.communicate(input)[0].rstrip('\n')
19
20def message(message):
21    args = ['xmessage', '-file', '-'];
22    font = pygmi.wmii['font']
23    if not font.startswith('xft:'):
24        args += ['-fn', font.split(',')[0]]
25    call(*args, input=message)
26
27def program_list(path):
28    names = []
29    for d in path:
30        try:
31            for f in os.listdir(d):
32                p = '%s/%s' % (d, f)
33                if f not in names and os.access(p, os.X_OK) and (
34                    os.path.isfile(p) or os.path.islink(p)):
35                    names.append(f)
36        except Exception:
37            pass
38    return sorted(names)
39
40def curry(func, *args, **kwargs):
41    if _ in args:
42        blank = [i for i in range(0, len(args)) if args[i] is _]
43        def curried(*newargs, **newkwargs):
44            ary = list(args)
45            for k, v in zip(blank, newargs):
46                ary[k] = v
47            ary = tuple(ary) + newargs[len(blank):]
48            return func(*ary, **dict(kwargs, **newkwargs))
49    else:
50        def curried(*newargs, **newkwargs):
51            return func(*(args + newargs), **dict(kwargs, **newkwargs))
52    curried.__name__ = func.__name__ + '__curried__'
53    return curried
54
55def find_script(name):
56    for path in pygmi.confpath:
57        if os.access('%s/%s' % (path, name), os.X_OK):
58            return '%s/%s' % (path, name)
59
60def prop(**kwargs):
61    def prop_(wrapped):
62        kwargs['fget'] = wrapped
63        return property(**kwargs)
64    return prop_
65
66# vim:se sts=4 sw=4 et:
67