Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add support for passing previous file to importer callback #287

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
Feb 14, 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
7 changes: 6 additions & 1 deletion pysass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,14 @@ static Sass_Import_List _call_py_importer_f(
PyObject* pyfunc = (PyObject*)sass_importer_get_cookie(cb);
PyObject* py_result = NULL;
Sass_Import_List sass_imports = NULL;
struct Sass_Import* previous;
const char* prev_path;
Py_ssize_t i;

py_result = PyObject_CallFunction(pyfunc, PySass_IF_PY3("y", "s"), path);
previous = sass_compiler_get_last_import(comp);
prev_path = sass_import_get_abs_path(previous);

py_result = PyObject_CallFunction(pyfunc, PySass_IF_PY3("yy", "ss"), path, prev_path);

/* Handle importer throwing an exception */
if (!py_result) goto done;
Expand Down
26 changes: 21 additions & 5 deletions sass.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,19 @@ 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):
ret = func(path.decode('UTF-8'))
def inner(path, prev):
if num_args == 2:
ret = func(path.decode('UTF-8'), prev.decode('UTF-8'))
else:
ret = func(path.decode('UTF-8'))
return _normalize_importer_return_value(ret)
return inner

Expand Down Expand Up @@ -477,8 +487,10 @@ def func_name(a, b):
A priority of zero is acceptable; priority determines the order callbacks
are attempted.

These callbacks must accept a single string argument representing the path
passed to the ``@import`` directive, and either return ``None`` to
These callbacks can accept one or two string arguments. The first argument
is the path that was passed to the ``@import`` directive; the second
(optional) argument is the previous resolved path, where the ``@import``
directive was found. The callbacks must either return ``None`` to
indicate the path wasn't handled by that callback (to continue with others
or fall back on internal ``libsass`` filesystem behaviour) or a list of
one or more tuples, each in one of three forms:
Expand All @@ -494,7 +506,7 @@ def func_name(a, b):

.. code-block:: python

def my_importer(path):
def my_importer(path, prev):
return [(path, '#' + path + ' { color: red; }')]

sass.compile(
Expand Down Expand Up @@ -530,6 +542,10 @@ def my_importer(path):
Added ``source_map_contents``, ``source_map_embed``,
``omit_source_map_url``, and ``source_map_root`` parameters.

.. versionadded:: 0.18.0
The importer callbacks can now take a second argument, the previously-
resolved path, so that importers can do relative path resolution.

"""
modes = set()
for mode_name in MODES:
Expand Down
17 changes: 17 additions & 0 deletions sasstests.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,23 @@ def importer_returning_one_argument(path):
)
assert ret == 'b i{font-size:20px}.foo-one-arg{color:blue}\n'

def test_importer_prev_path(self):
def importer(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 { color: red; }'),)

ret = sass.compile(
string='@import "a";',
importers=((0, importer),),
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