diff options
Diffstat (limited to 'compressor/utils')
| -rw-r--r-- | compressor/utils/__init__.py | 18 | ||||
| -rw-r--r-- | compressor/utils/staticfiles.py | 2 | ||||
| -rw-r--r-- | compressor/utils/stringformat.py | 22 |
3 files changed, 24 insertions, 18 deletions
diff --git a/compressor/utils/__init__.py b/compressor/utils/__init__.py index 83a1a2a..1c3479b 100644 --- a/compressor/utils/__init__.py +++ b/compressor/utils/__init__.py @@ -1,6 +1,9 @@ # -*- coding: utf-8 -*- +from __future__ import unicode_literals import os +from django.utils import six + from compressor.exceptions import FilterError @@ -10,15 +13,14 @@ def get_class(class_string, exception=FilterError): """ if not hasattr(class_string, '__bases__'): try: - class_string = class_string.encode('ascii') + class_string = str(class_string) mod_name, class_name = get_mod_func(class_string) - if class_name != '': - cls = getattr(__import__(mod_name, {}, {}, ['']), class_name) + if class_name: + return getattr(__import__(mod_name, {}, {}, [str('')]), class_name) except (ImportError, AttributeError): - pass - else: - return cls - raise exception('Failed to import %s' % class_string) + raise exception('Failed to import %s' % class_string) + + raise exception("Invalid class path '%s'" % class_string) def get_mod_func(callback): @@ -48,7 +50,7 @@ def find_command(cmd, paths=None, pathext=None): """ if paths is None: paths = os.environ.get('PATH', '').split(os.pathsep) - if isinstance(paths, basestring): + if isinstance(paths, six.string_types): paths = [paths] # check if there are funny path extensions for executables, e.g. Windows if pathext is None: diff --git a/compressor/utils/staticfiles.py b/compressor/utils/staticfiles.py index 169d427..28026f2 100644 --- a/compressor/utils/staticfiles.py +++ b/compressor/utils/staticfiles.py @@ -1,4 +1,4 @@ -from __future__ import absolute_import +from __future__ import absolute_import, unicode_literals from django.core.exceptions import ImproperlyConfigured diff --git a/compressor/utils/stringformat.py b/compressor/utils/stringformat.py index 0cfba86..9311e78 100644 --- a/compressor/utils/stringformat.py +++ b/compressor/utils/stringformat.py @@ -6,8 +6,12 @@ An implementation of the advanced string formatting (PEP 3101). Author: Florent Xicluna """ +from __future__ import unicode_literals + import re +from django.utils import six + _format_str_re = re.compile( r'((?<!{)(?:{{)+' # '{{' r'|(?:}})+(?!})' # '}} @@ -128,7 +132,7 @@ def _format_field(value, parts, conv, spec, want_bytes=False): value = value.strftime(str(spec)) else: value = _strformat(value, spec) - if want_bytes and isinstance(value, unicode): + if want_bytes and isinstance(value, six.text_type): return str(value) return value @@ -138,9 +142,9 @@ class FormattableString(object): The method format() behaves like str.format() in python 2.6+. - >>> FormattableString(u'{a:5}').format(a=42) - ... # Same as u'{a:5}'.format(a=42) - u' 42' + >>> FormattableString('{a:5}').format(a=42) + ... # Same as '{a:5}'.format(a=42) + ' 42' """ @@ -244,13 +248,13 @@ def selftest(): import datetime F = FormattableString - assert F(u"{0:{width}.{precision}s}").format('hello world', - width=8, precision=5) == u'hello ' + assert F("{0:{width}.{precision}s}").format('hello world', + width=8, precision=5) == 'hello ' d = datetime.date(2010, 9, 7) - assert F(u"The year is {0.year}").format(d) == u"The year is 2010" - assert F(u"Tested on {0:%Y-%m-%d}").format(d) == u"Tested on 2010-09-07" - print 'Test successful' + assert F("The year is {0.year}").format(d) == "The year is 2010" + assert F("Tested on {0:%Y-%m-%d}").format(d) == "Tested on 2010-09-07" + print('Test successful') if __name__ == '__main__': selftest() |
