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
|
|
|
"""qtsass - Compile SCSS files to valid Qt stylesheets.""" |
10
|
|
|
|
11
|
|
|
# Standard library imports |
12
|
|
|
from __future__ import absolute_import, print_function |
13
|
|
|
import logging |
14
|
|
|
import os |
15
|
|
|
|
16
|
|
|
# Third party imports |
17
|
|
|
import sass |
18
|
|
|
from watchdog.observers import Observer |
19
|
|
|
|
20
|
|
|
# Local imports |
21
|
|
|
from qtsass.conformers import scss_conform, qt_conform |
22
|
|
|
from qtsass.functions import qlineargradient, rgba |
23
|
|
|
from qtsass.importers import qss_importer |
24
|
|
|
from qtsass.events import SourceEventHandler |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
logging.basicConfig(level=logging.DEBUG) |
28
|
|
|
_log = logging.getLogger(__name__) |
|
|
|
|
29
|
|
|
|
30
|
|
|
|
31
|
|
|
def compile(input_file): |
|
|
|
|
32
|
|
|
"""Compile QtSASS to CSS.""" |
33
|
|
|
|
34
|
|
|
_log.debug('Compiling {}...'.format(input_file)) |
|
|
|
|
35
|
|
|
|
36
|
|
|
with open(input_file, 'r') as f: |
|
|
|
|
37
|
|
|
input_str = f.read() |
38
|
|
|
|
39
|
|
|
try: |
40
|
|
|
importer_root = os.path.dirname(os.path.abspath(input_file)) |
41
|
|
|
return qt_conform( |
42
|
|
|
sass.compile( |
43
|
|
|
string=scss_conform(input_str), |
44
|
|
|
source_comments=False, |
45
|
|
|
custom_functions={ |
46
|
|
|
'qlineargradient': qlineargradient, |
47
|
|
|
'rgba': rgba |
48
|
|
|
}, |
49
|
|
|
importers=[(0, qss_importer(importer_root))] |
50
|
|
|
) |
51
|
|
|
) |
52
|
|
|
except sass.CompileError as e: |
|
|
|
|
53
|
|
|
_log.error('Failed to compile {}:\n{}'.format(input_file, e)) |
|
|
|
|
54
|
|
|
return "" |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def compile_filename(input_file, dest_file): |
58
|
|
|
"""Compile QtSASS to CSS and save.""" |
59
|
|
|
|
60
|
|
|
css = compile(input_file) |
61
|
|
|
with open(dest_file, 'w') as css_file: |
62
|
|
|
css_file.write(css) |
63
|
|
|
_log.info('Created CSS file {}'.format(dest_file)) |
|
|
|
|
64
|
|
|
|
65
|
|
|
|
66
|
|
|
def compile_dirname(input_dir, output_dir): |
67
|
|
|
"""Compiles QtSASS files in a directory including subdirectories.""" |
68
|
|
|
|
69
|
|
|
def is_valid(file): |
|
|
|
|
70
|
|
|
return not file.startswith('_') and file.endswith('.scss') |
71
|
|
|
|
72
|
|
|
for root, subdirs, files in os.walk(input_dir): |
|
|
|
|
73
|
|
|
relative_root = os.path.relpath(root, input_dir) |
74
|
|
|
output_root = os.path.join(output_dir, relative_root) |
75
|
|
|
|
76
|
|
|
for file in [f for f in files if is_valid(f)]: |
77
|
|
|
scss_path = os.path.join(root, file) |
78
|
|
|
css_file = os.path.splitext(file)[0] + '.css' |
79
|
|
|
css_path = os.path.join(output_root, css_file) |
80
|
|
|
if not os.path.isdir(output_root): |
81
|
|
|
os.makedirs(output_root) |
82
|
|
|
compile_filename(scss_path, css_path) |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
def watch(source, destination, compiler=None, recursive=True): |
86
|
|
|
""" |
87
|
|
|
Watches a source file or directory, compiling QtSass files when modified. |
88
|
|
|
|
89
|
|
|
The compiler function defaults to compile_filename when source is a file |
90
|
|
|
and compile_dirname when source is a directory. |
91
|
|
|
|
92
|
|
|
:param source: Path to source QtSass file or directory. |
93
|
|
|
:param destination: Path to output css file or directory. |
94
|
|
|
:param compiler: Compile function (optional) |
95
|
|
|
:param recursive: If True, watch subdirectories (default: True). |
96
|
|
|
:returns: watchdog.Observer |
97
|
|
|
""" |
98
|
|
|
|
99
|
|
|
if os.path.isfile(source): |
100
|
|
|
watch_dir = os.path.dirname(source) |
101
|
|
|
compiler = compiler or compile_filename |
102
|
|
|
else: |
103
|
|
|
watch_dir = source |
104
|
|
|
compiler = compiler or compile_dirname |
105
|
|
|
|
106
|
|
|
event_handler = SourceEventHandler(source, destination, compiler) |
107
|
|
|
|
108
|
|
|
observer = Observer() |
109
|
|
|
observer.schedule(event_handler, watch_dir, recursive=recursive) |
110
|
|
|
return observer |
111
|
|
|
|
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.