From 810ffdb1b9f6ff6149247020567be0e3fd06b989 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Tue, 14 Aug 2018 18:41:58 -0700 Subject: [PATCH] Add option to strip extensions for wsgi / distutils integration --- sasstests.py | 23 +++++++++++++++++++++-- sassutils/builder.py | 30 ++++++++++++++++++++++++++++-- sassutils/distutils.py | 14 ++++++++++++++ 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/sasstests.py b/sasstests.py index c87c3c1d..d6f265a5 100644 --- a/sasstests.py +++ b/sasstests.py @@ -553,9 +553,14 @@ def test_normalize_manifests(self): manifests = Manifest.normalize_manifests({ 'package': 'sass/path', 'package.name': ('sass/path', 'css/path'), - 'package.name2': Manifest('sass/path', 'css/path') + 'package.name2': Manifest('sass/path', 'css/path'), + 'package.name3': { + 'sass_path': 'sass/path', + 'css_path': 'css/path', + 'strip_extension': True, + }, }) - assert len(manifests) == 3 + assert len(manifests) == 4 assert isinstance(manifests['package'], Manifest) assert manifests['package'].sass_path == 'sass/path' assert manifests['package'].css_path == 'sass/path' @@ -565,6 +570,10 @@ def test_normalize_manifests(self): assert isinstance(manifests['package.name2'], Manifest) assert manifests['package.name2'].sass_path == 'sass/path' assert manifests['package.name2'].css_path == 'css/path' + assert isinstance(manifests['package.name3'], Manifest) + assert manifests['package.name3'].sass_path == 'sass/path' + assert manifests['package.name3'].css_path == 'css/path' + assert manifests['package.name3'].strip_extension is True def test_build_one(self): with tempdir() as d: @@ -626,6 +635,16 @@ def replace_source_path(s, name): ) +def test_manifest_strip_extension(tmpdir): + src = tmpdir.join('test').ensure_dir() + src.join('a.scss').write('a{b: c;}') + + m = Manifest(sass_path='test', css_path='css', strip_extension=True) + m.build_one(str(tmpdir), 'a.scss') + + assert tmpdir.join('css/a.css').read() == 'a {\n b: c; }\n' + + class WsgiTestCase(BaseTestCase): @staticmethod diff --git a/sassutils/builder.py b/sassutils/builder.py index bcd677ca..52e0f250 100644 --- a/sassutils/builder.py +++ b/sassutils/builder.py @@ -9,6 +9,7 @@ import os import os.path import re +import warnings from six import string_types @@ -86,7 +87,8 @@ class Manifest(object): :param css_path: the path of the directory to store compiled CSS files :type css_path: :class:`str`, :class:`basestring` - + :param strip_extension: whether to remove the original file extension + :type strip_extension: :class:`bool` """ @classmethod @@ -106,6 +108,8 @@ def normalize_manifests(cls, manifests): continue elif isinstance(manifest, tuple): manifest = Manifest(*manifest) + elif isinstance(manifest, collections.Mapping): + manifest = Manifest(**manifest) elif isinstance(manifest, string_types): manifest = Manifest(manifest) else: @@ -117,7 +121,13 @@ def normalize_manifests(cls, manifests): manifests[package_name] = manifest return manifests - def __init__(self, sass_path, css_path=None, wsgi_path=None): + def __init__( + self, + sass_path, + css_path=None, + wsgi_path=None, + strip_extension=None, + ): if not isinstance(sass_path, string_types): raise TypeError('sass_path must be a string, not ' + repr(sass_path)) @@ -131,9 +141,23 @@ def __init__(self, sass_path, css_path=None, wsgi_path=None): elif not isinstance(wsgi_path, string_types): raise TypeError('wsgi_path must be a string, not ' + repr(wsgi_path)) + if strip_extension is None: + warnings.warn( + '`strip_extension` was not specified, defaulting to `False`.\n' + 'In the future, `strip_extension` will default to `True`.', + DeprecationWarning, + ) + strip_extension = False + elif not isinstance(strip_extension, bool): + raise TypeError( + 'strip_extension must be bool not {!r}'.format( + strip_extension, + ), + ) self.sass_path = sass_path self.css_path = css_path self.wsgi_path = wsgi_path + self.strip_extension = strip_extension def resolve_filename(self, package_dir, filename): """Gets a proper full relative path of Sass source and @@ -149,6 +173,8 @@ def resolve_filename(self, package_dir, filename): """ sass_path = os.path.join(package_dir, self.sass_path, filename) + if self.strip_extension: + filename, _ = os.path.splitext(filename) css_filename = filename + '.css' css_path = os.path.join(package_dir, self.css_path, css_filename) return sass_path, css_path diff --git a/sassutils/distutils.py b/sassutils/distutils.py index 9c29ae36..21a2bd0a 100644 --- a/sassutils/distutils.py +++ b/sassutils/distutils.py @@ -49,6 +49,20 @@ 'package.name': ('static/scss', 'static') } +The option can also be a mapping of package names to manifest dictionaries:: + + { + 'package': { + 'sass_path': 'static/sass', + 'css_path': 'static/css', + 'strip_extension': True, + }, + } + +.. versionadded:: 0.15.0 + Added ``strip_extension`` so ``a.scss`` is compiled to ``a.css`` instead + of ``a.scss.css``. This option will default to ``True`` in the future. + .. versionadded:: 0.6.0 Added ``--output-style``/``-s`` option to :class:`build_sass` command.