/ruffus/re_glob.py

https://code.google.com/p/ruffus/ · Python · 79 lines · 56 code · 11 blank · 12 comment · 23 complexity · 7e45e0addd8da51fdc20fffca36bb40b MD5 · raw file

  1. """Filename globbing utility."""
  2. import sys
  3. import os
  4. import re
  5. __all__ = ["re_glob", "ire_glob"]
  6. def re_glob(pathname):
  7. """Return a list of paths matching a pathname pattern using regular expresions
  8. """
  9. return list(ire_glob(pathname))
  10. def ire_glob(pathname):
  11. """Return an iterator which yields the paths matching a pathname pattern using regular expresions
  12. """
  13. if not has_magic(pathname):
  14. if os.path.lexists(pathname):
  15. yield pathname
  16. return
  17. dirname, basename = os.path.split(pathname)
  18. if not dirname:
  19. for name in re_glob1(os.curdir, basename):
  20. yield name
  21. return
  22. if has_magic(dirname):
  23. dirs = ire_glob(dirname)
  24. else:
  25. dirs = [dirname]
  26. if has_magic(basename):
  27. re_glob_in_dir = re_glob1
  28. else:
  29. re_glob_in_dir = re_glob0
  30. for dirname in dirs:
  31. for name in re_glob_in_dir(dirname, basename):
  32. yield os.path.join(dirname, name)
  33. # These 2 helper functions non-recursively re_glob inside a literal directory.
  34. # They return a list of basenames. `re_glob1` accepts a pattern while `re_glob0`
  35. # takes a literal basename (so it only has to check for its existence).
  36. def re_glob1(dirname, pattern):
  37. if not dirname:
  38. dirname = os.curdir
  39. if isinstance(pattern, unicode) and not isinstance(dirname, unicode):
  40. dirname = unicode(dirname, sys.getfilesystemencoding() or
  41. sys.getdefaultencoding())
  42. try:
  43. names = os.listdir(dirname)
  44. except os.error:
  45. return []
  46. if pattern[0] != '.':
  47. names = filter(lambda x: x[0] != '.', names)
  48. # only where entire name is specified by regular expression
  49. matching = []
  50. for n in names:
  51. m = re.match(pattern, n)
  52. if m and m.end() == len(n):
  53. matching.append(n)
  54. return matching
  55. def re_glob0(dirname, basename):
  56. if basename == '':
  57. # `os.path.split()` returns an empty basename for paths ending with a
  58. # directory separator. 'q*x/' should match only directories.
  59. if os.path.isdir(dirname):
  60. return [basename]
  61. else:
  62. if os.path.lexists(os.path.join(dirname, basename)):
  63. return [basename]
  64. return []
  65. magic_check = re.compile('[.*\\\^$?(){}[\]]')
  66. def has_magic(s):
  67. return magic_check.search(s) is not None