1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# ----------------------------------------------------------------------------- |
3
|
|
|
# Copyright (c) 2015 Yann Lanthony |
4
|
|
|
# Copyright (c) 2017-2018 Spyder Project Contributors |
5
|
|
|
# |
6
|
|
|
# Licensed under the terms of the MIT License |
7
|
|
|
# (See LICENSE.txt for details) |
8
|
|
|
# ----------------------------------------------------------------------------- |
9
|
|
|
"""Libsass importers.""" |
10
|
|
|
|
11
|
|
|
# Standard library imports |
12
|
|
|
from __future__ import absolute_import |
13
|
|
|
import os |
14
|
|
|
|
15
|
|
|
# Local imports |
16
|
|
|
from qtsass.conformers import scss_conform |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
def norm_path(*parts): |
|
|
|
|
20
|
|
|
return os.path.normpath(os.path.join(*parts)) |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def qss_importer(where): |
24
|
|
|
""" |
25
|
|
|
Returns a function which conforms imported qss files to valid scss to be |
26
|
|
|
used as an importer for sass.compile. |
27
|
|
|
|
28
|
|
|
:param where: Directory containing scss, css, and sass files |
29
|
|
|
""" |
30
|
|
|
|
31
|
|
|
def find_file(import_file): |
|
|
|
|
32
|
|
|
|
33
|
|
|
# Create partial import filename |
34
|
|
|
dirname, basename = os.path.split(import_file) |
35
|
|
|
if dirname: |
36
|
|
|
import_partial_file = '/'.join([dirname, '_' + basename]) |
37
|
|
|
else: |
38
|
|
|
import_partial_file = '_' + basename |
39
|
|
|
|
40
|
|
|
# Build potential file paths for @import "import_file" |
41
|
|
|
potential_files = [] |
42
|
|
|
for ext in ['', '.scss', '.css', '.sass']: |
43
|
|
|
full_name = import_file + ext |
44
|
|
|
partial_name = import_partial_file + ext |
45
|
|
|
potential_files.append(full_name) |
46
|
|
|
potential_files.append(partial_name) |
47
|
|
|
potential_files.append(norm_path(where, full_name)) |
48
|
|
|
potential_files.append(norm_path(where, partial_name)) |
49
|
|
|
|
50
|
|
|
# Return first existing potential file |
51
|
|
|
for potential_file in potential_files: |
52
|
|
|
if os.path.isfile(potential_file): |
53
|
|
|
return potential_file |
54
|
|
|
|
55
|
|
|
return None |
56
|
|
|
|
57
|
|
|
def import_and_conform_file(import_file): |
|
|
|
|
58
|
|
|
|
59
|
|
|
real_import_file = find_file(import_file) |
60
|
|
|
with open(real_import_file, 'r') as f: |
|
|
|
|
61
|
|
|
import_str = f.read() |
62
|
|
|
|
63
|
|
|
return [(import_file, scss_conform(import_str))] |
64
|
|
|
|
65
|
|
|
return import_and_conform_file |
66
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.