Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit feeb346

Browse files
committed
Python 2.5 compatibility
- Made sassutils.utils module for compatibility layer - is_mapping(): alternative to collections.Mapping - relpath(): alternative to os.path.relpath()
1 parent f7137d7 commit feeb346

File tree

5 files changed

+64
-7
lines changed

5 files changed

+64
-7
lines changed

docs/sassutils.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66

77
sassutils/builder
88
sassutils/distutils
9+
sassutils/utils
910
sassutils/wsgi

docs/sassutils/utils.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
.. automodule:: sassutils.utils
3+
:members:

sassutils/builder.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
"""
55
from __future__ import with_statement
66

7-
import collections
87
import os
98
import os.path
109
import re
1110

1211
from sass import compile
12+
from .utils import is_mapping, relpath
1313

1414
__all__ = 'SUFFIXES', 'SUFFIX_PATTERN', 'Manifest', 'build_directory'
1515

@@ -49,8 +49,8 @@ def build_directory(sass_path, css_path, _root_sass=None, _root_css=None):
4949
css = compile(filename=sass_fullname, include_paths=[_root_sass])
5050
with open(css_fullname, 'w') as css_file:
5151
css_file.write(css)
52-
result[os.path.relpath(sass_fullname, _root_sass)] = \
53-
os.path.relpath(css_fullname, _root_css)
52+
result[relpath(sass_fullname, _root_sass)] = \
53+
relpath(css_fullname, _root_css)
5454
elif os.path.isdir(sass_fullname):
5555
css_fullname = os.path.join(css_path, name)
5656
subresult = build_directory(sass_fullname, css_fullname,
@@ -75,7 +75,7 @@ class Manifest(object):
7575
def normalize_manifests(cls, manifests):
7676
if manifests is None:
7777
manifests = {}
78-
elif isinstance(manifests, collections.Mapping):
78+
elif is_mapping(manifests):
7979
manifests = dict(manifests)
8080
else:
8181
raise TypeError('manifests must be a mapping object, not ' +

sassutils/utils.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
""":mod:`sassutils.utils` --- Utilities for internal use
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
"""
5+
import collections
6+
import functools
7+
import os.path
8+
9+
10+
__all__ = 'is_mapping', 'relpath'
11+
12+
13+
def is_mapping(value):
14+
"""The predicate method equivalent to::
15+
16+
isinstance(value, collections.Mapping)
17+
18+
This function works on Python 2.5 as well.
19+
20+
:param value: a value to test its type
21+
:returns: ``True`` only if ``value`` is a mapping object
22+
:rtype: :class:`bool`
23+
24+
"""
25+
return isinstance(value, collections.Mapping)
26+
27+
28+
if not hasattr(collections, 'Mapping'):
29+
@functools.wraps(is_mapping)
30+
def is_mapping(value):
31+
return (callable(getattr(value, 'keys', None)) and
32+
callable(getattr(value, 'values', None)) and
33+
callable(getattr(value, 'items', None)) and
34+
callable(getattr(value, '__getitem__', None)))
35+
36+
37+
def relpath(path, start=os.path.curdir):
38+
"""Equivalent to :func:`os.path.relpath()` except it's for
39+
Python 2.5.
40+
41+
"""
42+
start_list = os.path.abspath(start).split(os.path.sep)
43+
path_list = os.path.abspath(path).split(os.path.sep)
44+
# Work out how much of the filepath is shared by start and path.
45+
i = len(os.path.commonprefix([start_list, path_list]))
46+
rel_list = [os.path.pardir] * (len(start_list)-i) + path_list[i:]
47+
if not rel_list:
48+
return os.path.curdir
49+
return os.path.join(*rel_list)
50+
51+
52+
if hasattr(os.path, 'relpath'):
53+
relpath = os.path.relpath

sassutils/wsgi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"""
55
from __future__ import absolute_import, with_statement
66

7-
import collections
87
import os
98
import os.path
109

1110
import pkg_resources
1211

1312
from sass import CompileError
1413
from .builder import Manifest
14+
from .utils import is_mapping
1515

1616
__all__ = 'SassMiddleware',
1717

@@ -41,7 +41,7 @@ def __init__(self, app, manifests, package_dir={},
4141
'not ' + repr(app))
4242
self.app = app
4343
self.manifests = Manifest.normalize_manifests(manifests)
44-
if not isinstance(package_dir, collections.Mapping):
44+
if not is_mapping(package_dir):
4545
raise TypeError('package_dir must be a mapping object, not ' +
4646
repr(package_dir))
4747
self.error_status = error_status
@@ -73,7 +73,7 @@ def __call__(self, environ, start_response):
7373
result = manifest.build_one(package_dir, sass_filename)
7474
except (IOError, OSError):
7575
break
76-
except CompileError as e:
76+
except CompileError, e:
7777
start_response(self.error_status,
7878
[('Content-Type', 'text/css')])
7979
return [

0 commit comments

Comments
 (0)