PageRenderTime 29ms CodeModel.GetById 21ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/hyde/util.py

http://github.com/hyde/hyde
Python | 59 lines | 31 code | 15 blank | 13 comment | 6 complexity | 8c518d16dbf9983adbd012efe53fe535 MD5 | raw file
 1"""
 2Module for python 2.6 compatibility.
 3"""
 4import os
 5from functools import partial
 6from itertools import tee
 7
 8from hyde._compat import str, zip
 9
10
11def make_method(method_name, method_):
12    def method__(*args, **kwargs):
13        return method_(*args, **kwargs)
14    method__.__name__ = method_name
15    return method__
16
17
18def add_property(obj, method_name, method_, *args, **kwargs):
19    m = make_method(method_name, partial(method_, *args, **kwargs))
20    setattr(obj, method_name, property(m))
21
22
23def add_method(obj, method_name, method_, *args, **kwargs):
24    m = make_method(method_name, partial(method_, *args, **kwargs))
25    setattr(obj, method_name, m)
26
27
28def pairwalk(iterable):
29    a, b = tee(iterable)
30    next(b, None)
31    return zip(a, b)
32
33
34def first_match(predicate, iterable):
35    """
36    Gets the first element matched by the predicate
37    in the iterable.
38    """
39    for item in iterable:
40        if predicate(item):
41            return item
42    return None
43
44
45def discover_executable(name, sitepath):
46    """
47    Finds an executable in the given sitepath or in the
48    path list provided by the PATH environment variable.
49    """
50
51    # Check if an executable can be found in the site path first.
52    # If not check the os $PATH for its presence.
53
54    paths = [str(sitepath)] + os.environ['PATH'].split(os.pathsep)
55    for path in paths:
56        full_name = os.path.join(path, name)
57        if os.path.exists(full_name):
58            return full_name
59    return None