/hyde/util.py

http://github.com/hyde/hyde · Python · 59 lines · 31 code · 15 blank · 13 comment · 4 complexity · 8c518d16dbf9983adbd012efe53fe535 MD5 · raw file

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