Skip to content

Allow partials and other non-function callables as importer callbacks #291

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
Mar 5, 2019
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
25 changes: 13 additions & 12 deletions sass.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from __future__ import absolute_import

import collections
import functools
import inspect
import io
import os
Expand Down Expand Up @@ -195,19 +194,21 @@ def _to_bytes(obj):


def _importer_callback_wrapper(func):
if PY2:
argspec = inspect.getargspec(func)
else:
argspec = inspect.getfullargspec(func)

num_args = len(argspec.args)

@functools.wraps(func)
def inner(path, prev):
if num_args == 2:
ret = func(path.decode('UTF-8'), prev.decode('UTF-8'))
path, prev = path.decode('UTF-8'), prev.decode('UTF-8')
num_args = getattr(inner, '_num_args', None)
if num_args is None:
try:
ret = func(path, prev)
except TypeError:
inner._num_args = 1
ret = func(path)
else:
inner._num_args = 2
elif num_args == 2:
ret = func(path, prev)
else:
ret = func(path.decode('UTF-8'))
ret = func(path)
return _normalize_importer_return_value(ret)
return inner

Expand Down
18 changes: 18 additions & 0 deletions sasstests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import base64
import contextlib
import functools
import glob
import json
import io
Expand Down Expand Up @@ -358,6 +359,23 @@ def importer(path, prev):
)
assert ret == 'a{color:red}\n'

def test_importer_prev_path_partial(self):
def importer(a_css, path, prev):
assert path in ('a', 'b')
if path == 'a':
assert prev == 'stdin'
return ((path, '@import "b";'),)
elif path == 'b':
assert prev == 'a'
return ((path, a_css),)

ret = sass.compile(
string='@import "a";',
importers=((0, functools.partial(importer, 'a { color: red; }')),),
output_style='compressed',
)
assert ret == 'a{color:red}\n'

def test_importer_does_not_handle_returns_None(self):
def importer_one(path):
if path == 'one':
Expand Down