Skip to content

Commit cca5f11

Browse files
authored
Merge pull request #258 from asottile/add_strip_extension_option
Add option to strip extensions for wsgi / distutils integration
2 parents f05a9b4 + 810ffdb commit cca5f11

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

sasstests.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,14 @@ def test_normalize_manifests(self):
553553
manifests = Manifest.normalize_manifests({
554554
'package': 'sass/path',
555555
'package.name': ('sass/path', 'css/path'),
556-
'package.name2': Manifest('sass/path', 'css/path')
556+
'package.name2': Manifest('sass/path', 'css/path'),
557+
'package.name3': {
558+
'sass_path': 'sass/path',
559+
'css_path': 'css/path',
560+
'strip_extension': True,
561+
},
557562
})
558-
assert len(manifests) == 3
563+
assert len(manifests) == 4
559564
assert isinstance(manifests['package'], Manifest)
560565
assert manifests['package'].sass_path == 'sass/path'
561566
assert manifests['package'].css_path == 'sass/path'
@@ -565,6 +570,10 @@ def test_normalize_manifests(self):
565570
assert isinstance(manifests['package.name2'], Manifest)
566571
assert manifests['package.name2'].sass_path == 'sass/path'
567572
assert manifests['package.name2'].css_path == 'css/path'
573+
assert isinstance(manifests['package.name3'], Manifest)
574+
assert manifests['package.name3'].sass_path == 'sass/path'
575+
assert manifests['package.name3'].css_path == 'css/path'
576+
assert manifests['package.name3'].strip_extension is True
568577

569578
def test_build_one(self):
570579
with tempdir() as d:
@@ -626,6 +635,16 @@ def replace_source_path(s, name):
626635
)
627636

628637

638+
def test_manifest_strip_extension(tmpdir):
639+
src = tmpdir.join('test').ensure_dir()
640+
src.join('a.scss').write('a{b: c;}')
641+
642+
m = Manifest(sass_path='test', css_path='css', strip_extension=True)
643+
m.build_one(str(tmpdir), 'a.scss')
644+
645+
assert tmpdir.join('css/a.css').read() == 'a {\n b: c; }\n'
646+
647+
629648
class WsgiTestCase(BaseTestCase):
630649

631650
@staticmethod

sassutils/builder.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import os.path
1111
import re
12+
import warnings
1213

1314
from six import string_types
1415

@@ -86,7 +87,8 @@ class Manifest(object):
8687
:param css_path: the path of the directory to store compiled CSS
8788
files
8889
:type css_path: :class:`str`, :class:`basestring`
89-
90+
:param strip_extension: whether to remove the original file extension
91+
:type strip_extension: :class:`bool`
9092
"""
9193

9294
@classmethod
@@ -106,6 +108,8 @@ def normalize_manifests(cls, manifests):
106108
continue
107109
elif isinstance(manifest, tuple):
108110
manifest = Manifest(*manifest)
111+
elif isinstance(manifest, collections.Mapping):
112+
manifest = Manifest(**manifest)
109113
elif isinstance(manifest, string_types):
110114
manifest = Manifest(manifest)
111115
else:
@@ -117,7 +121,13 @@ def normalize_manifests(cls, manifests):
117121
manifests[package_name] = manifest
118122
return manifests
119123

120-
def __init__(self, sass_path, css_path=None, wsgi_path=None):
124+
def __init__(
125+
self,
126+
sass_path,
127+
css_path=None,
128+
wsgi_path=None,
129+
strip_extension=None,
130+
):
121131
if not isinstance(sass_path, string_types):
122132
raise TypeError('sass_path must be a string, not ' +
123133
repr(sass_path))
@@ -131,9 +141,23 @@ def __init__(self, sass_path, css_path=None, wsgi_path=None):
131141
elif not isinstance(wsgi_path, string_types):
132142
raise TypeError('wsgi_path must be a string, not ' +
133143
repr(wsgi_path))
144+
if strip_extension is None:
145+
warnings.warn(
146+
'`strip_extension` was not specified, defaulting to `False`.\n'
147+
'In the future, `strip_extension` will default to `True`.',
148+
DeprecationWarning,
149+
)
150+
strip_extension = False
151+
elif not isinstance(strip_extension, bool):
152+
raise TypeError(
153+
'strip_extension must be bool not {!r}'.format(
154+
strip_extension,
155+
),
156+
)
134157
self.sass_path = sass_path
135158
self.css_path = css_path
136159
self.wsgi_path = wsgi_path
160+
self.strip_extension = strip_extension
137161

138162
def resolve_filename(self, package_dir, filename):
139163
"""Gets a proper full relative path of Sass source and
@@ -149,6 +173,8 @@ def resolve_filename(self, package_dir, filename):
149173
150174
"""
151175
sass_path = os.path.join(package_dir, self.sass_path, filename)
176+
if self.strip_extension:
177+
filename, _ = os.path.splitext(filename)
152178
css_filename = filename + '.css'
153179
css_path = os.path.join(package_dir, self.css_path, css_filename)
154180
return sass_path, css_path

sassutils/distutils.py

+14
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@
4949
'package.name': ('static/scss', 'static')
5050
}
5151
52+
The option can also be a mapping of package names to manifest dictionaries::
53+
54+
{
55+
'package': {
56+
'sass_path': 'static/sass',
57+
'css_path': 'static/css',
58+
'strip_extension': True,
59+
},
60+
}
61+
62+
.. versionadded:: 0.15.0
63+
Added ``strip_extension`` so ``a.scss`` is compiled to ``a.css`` instead
64+
of ``a.scss.css``. This option will default to ``True`` in the future.
65+
5266
.. versionadded:: 0.6.0
5367
Added ``--output-style``/``-s`` option to :class:`build_sass` command.
5468

0 commit comments

Comments
 (0)