|
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
|
|
|
# yapf: disable |
|
12
|
|
|
|
|
13
|
|
|
from __future__ import absolute_import |
|
14
|
|
|
|
|
15
|
|
|
# Standard library imports |
|
16
|
|
|
import os |
|
17
|
|
|
|
|
18
|
|
|
# Local imports |
|
19
|
|
|
from qtsass.conformers import scss_conform |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
# yapf: enable |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
def norm_path(*parts): |
|
26
|
|
|
"""Normalize path.""" |
|
27
|
|
|
return os.path.normpath(os.path.join(*parts)).replace('\\', '/') |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
def qss_importer(*include_paths): |
|
31
|
|
|
""" |
|
32
|
|
|
Return function which conforms imported qss files to valid scss. |
|
33
|
|
|
|
|
34
|
|
|
This fucntion is to be used as an importer for sass.compile. |
|
35
|
|
|
|
|
36
|
|
|
:param include_paths: Directorys containing scss, css, and sass files. |
|
37
|
|
|
""" |
|
38
|
|
|
include_paths |
|
39
|
|
|
|
|
40
|
|
|
def find_file(import_file): |
|
41
|
|
|
# Create partial import filename |
|
42
|
|
|
dirname, basename = os.path.split(import_file) |
|
43
|
|
|
if dirname: |
|
44
|
|
|
import_partial_file = '/'.join([dirname, '_' + basename]) |
|
45
|
|
|
else: |
|
46
|
|
|
import_partial_file = '_' + basename |
|
47
|
|
|
|
|
48
|
|
|
# Build potential file paths for @import "import_file" |
|
49
|
|
|
potential_files = [] |
|
50
|
|
|
for ext in ['', '.scss', '.css', '.sass']: |
|
51
|
|
|
full_name = import_file + ext |
|
52
|
|
|
partial_name = import_partial_file + ext |
|
53
|
|
|
potential_files.append(full_name) |
|
54
|
|
|
potential_files.append(partial_name) |
|
55
|
|
|
for path in include_paths: |
|
56
|
|
|
potential_files.append(norm_path(path, full_name)) |
|
57
|
|
|
potential_files.append(norm_path(path, partial_name)) |
|
58
|
|
|
|
|
59
|
|
|
# Return first existing potential file |
|
60
|
|
|
for potential_file in potential_files: |
|
61
|
|
|
if os.path.isfile(potential_file): |
|
62
|
|
|
return potential_file |
|
63
|
|
|
|
|
64
|
|
|
return None |
|
65
|
|
|
|
|
66
|
|
|
def import_and_conform_file(import_file): |
|
67
|
|
|
"""Return base file and conformed scss file.""" |
|
68
|
|
|
real_import_file = find_file(import_file) |
|
69
|
|
|
with open(real_import_file, 'r') as f: |
|
70
|
|
|
import_str = f.read() |
|
71
|
|
|
|
|
72
|
|
|
return [(import_file, scss_conform(import_str))] |
|
73
|
|
|
|
|
74
|
|
|
return import_and_conform_file |
|
75
|
|
|
|