summaryrefslogtreecommitdiffstats
path: root/utils.py
diff options
context:
space:
mode:
authorJan Pokorný <jpokorny@redhat.com>2014-05-19 19:12:50 +0200
committerJan Pokorný <jpokorny@redhat.com>2014-05-19 19:12:50 +0200
commit0bd38b14b668bff168aeac3cc6c90e2c7ced8c92 (patch)
tree9a391b08e50aea9643ffca5c9503116d0773b856 /utils.py
parent5bf6a22f94302070fdae0335aeb30eee1a8cc495 (diff)
downloadclufter-0bd38b14b668bff168aeac3cc6c90e2c7ced8c92.tar.gz
clufter-0bd38b14b668bff168aeac3cc6c90e2c7ced8c92.tar.xz
clufter-0bd38b14b668bff168aeac3cc6c90e2c7ced8c92.zip
utils_prog: new program-specific unit incl. some moves from utils
Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Diffstat (limited to 'utils.py')
-rw-r--r--utils.py83
1 files changed, 12 insertions, 71 deletions
diff --git a/utils.py b/utils.py
index 4bcd274..fcd0b16 100644
--- a/utils.py
+++ b/utils.py
@@ -2,24 +2,9 @@
# Copyright 2014 Red Hat, Inc.
# Part of clufter project
# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
-"""Various helpers"""
+"""Various little+independent helpers"""
__author__ = "Jan Pokorný <jpokorny @at@ Red Hat .dot. com>"
-import os
-import sys
-from optparse import make_option
-from subprocess import Popen
-
-from .error import ClufterError
-
-
-def selfaware(func):
- """Decorator suitable for recursive staticmethod"""
- def selfaware_inner(*args, **kwargs):
- return func(selfaware(func), *args, **kwargs)
- map(lambda a: setattr(selfaware_inner, a, getattr(func, a)),
- ('__doc__', '__name__'))
- return selfaware_inner
# inspired by http://stackoverflow.com/a/4374075
mutable = lambda x: isinstance(x, (basestring, int, long, bool, float, tuple))
@@ -48,49 +33,10 @@ filtervarspop = \
lambda src, which: dict((x, src.pop(x)) for x in which if x in src)
-def which(name, *where):
- """Mimic `which' UNIX utility"""
- where = tuple(os.path.abspath(i) for i in where)
- if 'PATH' in os.environ:
- path = tuple(i for i in os.environ['PATH'].split(os.pathsep)
- if len(i.strip()))
- else:
- path = ()
- for p in where + path:
- check = os.path.join(p, name)
- if os.path.exists(check):
- return check
- else:
- return None
-
-
#
-# command-line options and function introspection related
+# function introspection related
#
-# dashes recommended by 5 of 4 terminal fanboys
-# NB underscores -> dashes swap is idempotent in regards to the member
-# name of where optparse stores respective option value, but here
-# the list of possible operations to preserve such property stops!
-# + also to preserve reversibility/bijection, original ought to be
-# free of the substituting character
-cli_decor = lambda x: x.replace('_', '-')
-cli_undecor = lambda x: x.replace('-', '_')
-
-# prioritize consonants, deprioritize vowels (except for the first letter
-# overall), which seems to be widely adopted technique for selecting short
-# options based on their long counterparts ;)
-# XXX could eventually also resort to using upper-case chars
-longopt_letters_reprio = \
- lambda longopt: \
- (lambda lo: \
- lo[0] + ''.join(sorted(lo[1:],
- key=lambda x: int(x.lower() in 'aeiouy')))
- )(filter(lambda c: c.isalpha(), longopt))
-
-# extrapolate optparse.make_option to specifically-encoded "plural"
-make_options = lambda opt_decl: [make_option(*a, **kw) for a, kw in opt_decl]
-
def func_defaults_varnames(func, skip=0, fix_generator_tail=True):
"""Using introspection, get arg defaults (dict) + all arg names (tuple)
@@ -119,22 +65,17 @@ def func_defaults_varnames(func, skip=0, fix_generator_tail=True):
return func_defaults, func_varnames
-class OneoffWrappedStdinPopen(object):
- """Singleton to watch for atmost one use of stdin in Popen context"""
- def __init__(self):
- self._used = False
-
- def __call__(self, args, **kwargs):
- if not 'stdin' in kwargs and '-' in args:
- if self._used:
- raise ClufterError(self, 'repeated use detected')
- kwargs['stdin'] = sys.stdin
- # only the first '-' substituted
- args[args.index('-')] = '/dev/stdin'
- self._used |= True
- return Popen(args, **kwargs)
+#
+# decorators
+#
-OneoffWrappedStdinPopen = OneoffWrappedStdinPopen()
+def selfaware(func):
+ """Decorator suitable for recursive staticmethod"""
+ def selfaware_inner(*args, **kwargs):
+ return func(selfaware(func), *args, **kwargs)
+ map(lambda a: setattr(selfaware_inner, a, getattr(func, a)),
+ ('__doc__', '__name__'))
+ return selfaware_inner
# Inspired by http://stackoverflow.com/a/1383402