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