From d19650b93cc8c2d0043d41ca341a60b09d87daa8 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sat, 24 Sep 2011 14:33:13 -0400 Subject: [PATCH] add userpath and environ var expansion to expand_path --- clint/utils.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/clint/utils.py b/clint/utils.py index 3f499d0..b215c33 100644 --- a/clint/utils.py +++ b/clint/utils.py @@ -11,9 +11,29 @@ Various Python helpers used within clint. from __future__ import absolute_import from __future__ import with_statement -import sys import errno +import os.path from os import makedirs +from glob import glob + + +def expand_path(path): + """Expands directories and globs in given path.""" + + paths = [] + path = os.path.expanduser(path) + path = os.path.expandvars(path) + + if os.path.isdir(path): + + for (dir, dirs, files) in os.walk(path): + for file in files: + paths.append(os.path.join(dir, file)) + else: + paths.extend(glob(path)) + + return paths + def is_collection(obj): @@ -37,17 +57,17 @@ def mkdir_p(path): def tsplit(string, delimiters): """Behaves str.split but supports tuples of delimiters.""" - + delimiters = tuple(delimiters) stack = [string,] - + for delimiter in delimiters: for i, substring in enumerate(stack): substack = substring.split(delimiter) stack.pop(i) for j, _substring in enumerate(substack): stack.insert(i+j, _substring) - + return stack def schunk(string, size):