Skip to content

Add option to strip extensions for wsgi / distutils integration #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions sasstests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
30 changes: 28 additions & 2 deletions sassutils/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import os.path
import re
import warnings

from six import string_types

Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions sassutils/distutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down