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

Commit dbbcc9f

Browse files
committed
update as per comments
1 parent becb1f1 commit dbbcc9f

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

sass.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def _raise(e):
222222

223223
def compile_dirname(
224224
search_path, output_path, output_style, source_comments, include_paths,
225-
precision, custom_functions, importers
225+
precision, custom_functions, importers, custom_import_extensions
226226
):
227227
fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
228228
for dirpath, _, filenames in os.walk(search_path, onerror=_raise):
@@ -239,7 +239,8 @@ def compile_dirname(
239239
input_filename = input_filename.encode(fs_encoding)
240240
s, v, _ = _sass.compile_filename(
241241
input_filename, output_style, source_comments, include_paths,
242-
precision, None, custom_functions, importers, None, [],
242+
precision, None, custom_functions, importers, None,
243+
custom_import_extensions,
243244
)
244245
if s:
245246
v = v.decode('UTF-8')
@@ -293,9 +294,8 @@ def compile(**kwargs):
293294
:class:`collections.abc.Sequence`,
294295
:class:`collections.abc.Mapping`
295296
:param custom_import_extensions: optional extra file extensions which
296-
allow can be imported, eg. ``'.css'``
297-
:type custom_import_extensions: :class:`list`, :class:`str`,
298-
:class:`tuple`
297+
allow can be imported, eg. ``['.css']``
298+
:type custom_import_extensions: :class:`list`, :class:`tuple`
299299
:param indented: optional declaration that the string is Sass, not SCSS
300300
formatted. :const:`False` by default
301301
:type indented: :class:`bool`
@@ -337,9 +337,8 @@ def compile(**kwargs):
337337
:class:`collections.abc.Sequence`,
338338
:class:`collections.abc.Mapping`
339339
:param custom_import_extensions: optional extra file extensions which
340-
allow can be imported, eg. ``'.css'``
341-
:type custom_import_extensions: :class:`list`, :class:`str`,
342-
:class:`tuple`
340+
allow can be imported, eg. ``['.css']``
341+
:type custom_import_extensions: :class:`list`, :class:`tuple`
343342
:param importers: optional callback functions.
344343
see also below `importer callbacks
345344
<importer-callbacks_>`_ description
@@ -383,9 +382,8 @@ def compile(**kwargs):
383382
:class:`collections.abc.Sequence`,
384383
:class:`collections.abc.Mapping`
385384
:param custom_import_extensions: optional extra file extensions which
386-
allow can be imported, eg. ``'.css'``
387-
:type custom_import_extensions: :class:`list`, :class:`str`,
388-
:class:`tuple`
385+
allow can be imported, eg. ``['.css']``
386+
:type custom_import_extensions: :class:`list`, :class:`tuple`
389387
:raises sass.CompileError: when it fails for any reason
390388
(for example the given Sass has broken syntax)
391389
@@ -597,20 +595,20 @@ def _get_file_arg(key):
597595
)
598596

599597
_custom_exts = kwargs.pop('custom_import_extensions', []) or []
600-
if isinstance(_custom_exts, (text_type, bytes)):
601-
_custom_exts = [_custom_exts]
598+
if not isinstance(_custom_exts, (list, tuple)):
599+
raise TypeError(
600+
'custom_import_extensions must be a list of strings '
601+
'not {}'.format(type(_custom_exts))
602+
)
602603
custom_import_extensions = []
603604
for ext in _custom_exts:
604605
if isinstance(ext, text_type):
605606
custom_import_extensions.append(ext.encode('utf-8'))
606-
elif isinstance(ext, bytes):
607-
custom_import_extensions.append(ext)
608607
else:
609608
raise TypeError(
610609
'custom_import_extensions must be a list of strings '
611-
'or bytes not {}'.format(type(ext))
610+
'not {}'.format(type(ext))
612611
)
613-
614612
importers = _validate_importers(kwargs.pop('importers', None))
615613

616614
if 'string' in modes:
@@ -658,6 +656,7 @@ def _get_file_arg(key):
658656
s, v = compile_dirname(
659657
search_path, output_path, output_style, source_comments,
660658
include_paths, precision, custom_functions, importers,
659+
custom_import_extensions
661660
)
662661
if s:
663662
return

sassc.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
9191
'(output css filename).')
9292
parser.add_option('-I', '--include-path', metavar='DIR',
9393
dest='include_paths', action='append',
94-
help='Path to find "@import"ed (S)CSS source files. '
94+
help='Path to find "@import"ed (S)CSS source files. '
9595
'Can be multiply used.')
9696
parser.add_option(
9797
'-p', '--precision', action='store', type='int', default=5,
@@ -101,6 +101,10 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
101101
'--source-comments', action='store_true', default=False,
102102
help='Include debug info in output',
103103
)
104+
parser.add_option('--import-extensions',
105+
dest='custom_import_extensions', action='append',
106+
help='Extra extensions allowed for sass imports. '
107+
'Can be multiply used.')
104108
options, args = parser.parse_args(argv[1:])
105109
error = functools.partial(print,
106110
parser.get_prog_name() + ': error:',
@@ -130,7 +134,8 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
130134
source_map_filename=source_map_filename,
131135
output_filename_hint=args[1],
132136
include_paths=options.include_paths,
133-
precision=options.precision
137+
precision=options.precision,
138+
custom_import_extensions=options.custom_import_extensions,
134139
)
135140
else:
136141
source_map_filename = None
@@ -140,7 +145,8 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
140145
output_style=options.style,
141146
source_comments=options.source_comments,
142147
include_paths=options.include_paths,
143-
precision=options.precision
148+
precision=options.precision,
149+
custom_import_extensions=options.custom_import_extensions,
144150
)
145151
except (IOError, OSError) as e:
146152
error(e)

sasstests.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,60 +1412,61 @@ def test_imports_from_cwd(tmpdir):
14121412

14131413

14141414
def test_import_no_css(tmpdir):
1415-
tmpdir.join('other.css').write('body {colour: green}')
1415+
tmpdir.join('other.css').write('body {color: green}')
14161416
main_scss = tmpdir.join('main.scss')
14171417
main_scss.write("@import 'other';")
14181418
with pytest.raises(sass.CompileError):
14191419
sass.compile(filename=main_scss.strpath)
14201420

14211421

14221422
@pytest.mark.parametrize('exts', [
1423-
'.css',
14241423
('.css',),
14251424
['.css'],
1426-
b'.css',
1427-
[b'.css'],
14281425
['.foobar', '.css'],
1429-
['.foobar', '.css', b'anything'],
14301426
])
14311427
def test_import_css(exts, tmpdir):
1432-
tmpdir.join('other.css').write('body {colour: green}')
1428+
tmpdir.join('other.css').write('body {color: green}')
14331429
main_scss = tmpdir.join('main.scss')
14341430
main_scss.write("@import 'other';")
14351431
out = sass.compile(
14361432
filename=main_scss.strpath,
14371433
custom_import_extensions=exts,
14381434
)
1439-
assert out == 'body {\n colour: green; }\n'
1440-
1441-
1442-
def test_import_css_string(tmpdir):
1443-
tmpdir.join('other.css').write('body {colour: green}')
1444-
with tmpdir.as_cwd():
1445-
out = sass.compile(
1446-
string="@import 'other';",
1447-
custom_import_extensions='.css',
1448-
)
1449-
assert out == 'body {\n colour: green; }\n'
1435+
assert out == 'body {\n color: green; }\n'
14501436

14511437

1452-
def test_import_css_error(tmpdir):
1453-
tmpdir.join('other.css').write('body {colour: green}')
1438+
@pytest.mark.parametrize('exts', [
1439+
['.css', 3],
1440+
'.css',
1441+
[b'.css'],
1442+
])
1443+
def test_import_css_error(exts, tmpdir):
1444+
tmpdir.join('other.css').write('body {color: green}')
14541445
main_scss = tmpdir.join('main.scss')
14551446
main_scss.write("@import 'other';")
14561447
with pytest.raises(TypeError):
14571448
sass.compile(
14581449
filename=main_scss.strpath,
1459-
custom_import_extensions=['.css', 3],
1450+
custom_import_extensions=exts,
1451+
)
1452+
1453+
1454+
def test_import_css_string(tmpdir):
1455+
tmpdir.join('other.css').write('body {color: green}')
1456+
with tmpdir.as_cwd():
1457+
out = sass.compile(
1458+
string="@import 'other';",
1459+
custom_import_extensions=['.css'],
14601460
)
1461+
assert out == 'body {\n color: green; }\n'
14611462

14621463

14631464
def test_import_ext_other(tmpdir):
1464-
tmpdir.join('other.foobar').write('body {colour: green}')
1465+
tmpdir.join('other.foobar').write('body {color: green}')
14651466
main_scss = tmpdir.join('main.scss')
14661467
main_scss.write("@import 'other';")
14671468
out = sass.compile(
14681469
filename=main_scss.strpath,
1469-
custom_import_extensions='.foobar',
1470+
custom_import_extensions=['.foobar'],
14701471
)
1471-
assert out == 'body {\n colour: green; }\n'
1472+
assert out == 'body {\n color: green; }\n'

0 commit comments

Comments
 (0)