diff --git a/AUTHORS b/AUTHORS index 54abbb6..f172f24 100644 --- a/AUTHORS +++ b/AUTHORS @@ -14,4 +14,5 @@ Patches and Suggestions - Morgan Goose - Travis Swicegood - Will Thames -- Greg Haskins \ No newline at end of file +- Greg Haskins +- Miguel Araujo diff --git a/clint/__init__.py b/clint/__init__.py index 4ec78cd..793226b 100644 --- a/clint/__init__.py +++ b/clint/__init__.py @@ -19,8 +19,8 @@ from .pipes import piped_in __title__ = 'clint' -__version__ = '0.2.4' -__build__ = 0x000204 +__version__ = '0.2.5' +__build__ = 0x000205 __author__ = 'Kenneth Reitz' __license__ = 'ISC' __copyright__ = 'Copyright 2011 Kenneth Reitz' diff --git a/clint/arguments.py b/clint/arguments.py index 579a2e0..4b823b6 100644 --- a/clint/arguments.py +++ b/clint/arguments.py @@ -13,33 +13,13 @@ from __future__ import absolute_import import os from sys import argv -from glob import glob from .packages.ordereddict import OrderedDict -from .utils import is_collection - - +from .utils import expand_path, is_collection __all__ = ('Args', ) - -def _expand_path(path): - """Expands directories and globs in given path.""" - - paths = [] - - 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 - - class Args(object): """CLI Argument management.""" @@ -110,20 +90,20 @@ class Args(object): def any_contain(self, x): """Tests if given string is contained in any stored argument.""" - + return bool(self.first_with(x)) def contains(self, x): - """Tests if given object is in arguments list. + """Tests if given object is in arguments list. Accepts strings and lists of strings.""" - + return self.__contains__(x) def first(self, x): """Returns first found index of given value (or list of values)""" - + def _find( x): try: return self.all.index(str(x)) @@ -212,7 +192,7 @@ class Args(object): return False else: return (x in self.all[index]) - + except IndexError: return False @@ -221,7 +201,7 @@ class Args(object): """Returns true if argument exists at given index. Accepts: integer. """ - + try: self.all[x] return True @@ -231,15 +211,15 @@ class Args(object): def value_after(self, x): """Returns value of argument after given found argument (or list thereof).""" - + try: try: i = self.all.index(x) except ValueError: return None - + return self.all[i + 1] - + except IndexError: return None @@ -266,21 +246,21 @@ class Args(object): return collection - + @property def last(self): """Returns last argument.""" - + try: return self.all[-1] except IndexError: return None - + @property def all(self): """Returns all arguments.""" - + return self._args @@ -288,7 +268,7 @@ class Args(object): """Returns all arguments containing given string (or list thereof)""" _args = [] - + for arg in self.all: if is_collection(x): for _x in x: @@ -327,7 +307,7 @@ class Args(object): return self.start_with('-') - @property + @property def not_flags(self): """Returns Arg object excluding flagged arguments.""" @@ -341,7 +321,7 @@ class Args(object): _paths = [] for arg in self.all: - for path in _expand_path(arg): + for path in expand_path(arg): if os.path.exists(path): if absolute: _paths.append(os.path.abspath(path)) @@ -358,7 +338,7 @@ class Args(object): _args = [] for arg in self.all: - if not len(_expand_path(arg)): + if not len(expand_path(arg)): if not os.path.exists(arg): _args.append(arg) diff --git a/clint/textui/progress.py b/clint/textui/progress.py index 960c35b..4ca2b0f 100644 --- a/clint/textui/progress.py +++ b/clint/textui/progress.py @@ -15,20 +15,18 @@ import sys STREAM = sys.stderr BAR_TEMPLATE = '%s[%s%s] %i/%i\r' -BAR_EMPTY_CHAR = '-' -BAR_FILLED_CHAR = '=' DOTS_CHAR = '.' -def bar(it, label='', width=32, hide=False): +def bar(it, label='', width=32, hide=False, empty_char='-', filled_char='='): """Progress iterator. Wrap your iterables with it.""" def _show(_i): x = int(width*_i/count) if not hide: STREAM.write(BAR_TEMPLATE % ( - label, BAR_FILLED_CHAR*x, BAR_EMPTY_CHAR*(width-x), _i, count)) + label, bar_filled_char*x, bar_empty_char*(width-x), _i, count)) STREAM.flush() count = len(it) 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):