|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
# ----------------------------------------------------------------------------- |
|
4
|
|
|
# Copyright (c) 2015 Yann Lanthony |
|
5
|
|
|
# Copyright (c) 2017-2018 Spyder Project Contributors |
|
6
|
|
|
# |
|
7
|
|
|
# Licensed under the terms of the MIT License |
|
8
|
|
|
# (See LICENSE.txt for details) |
|
9
|
|
|
# ----------------------------------------------------------------------------- |
|
10
|
|
|
"""qtsass command line interface.""" |
|
11
|
|
|
|
|
12
|
|
|
# Standard library imports |
|
13
|
|
|
from __future__ import absolute_import, print_function |
|
14
|
|
|
import argparse |
|
15
|
|
|
import os |
|
16
|
|
|
import sys |
|
17
|
|
|
import time |
|
18
|
|
|
import logging |
|
19
|
|
|
import signal |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
# Local imports |
|
22
|
|
|
from qtsass.api import compile, compile_filename, compile_dirname, watch |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
logging.basicConfig(level=logging.DEBUG) |
|
26
|
|
|
_log = logging.getLogger(__name__) |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
def create_parser(): |
|
30
|
|
|
"""Create qtsass's cli parser.""" |
|
31
|
|
|
|
|
32
|
|
|
parser = argparse.ArgumentParser( |
|
33
|
|
|
prog='QtSASS', |
|
34
|
|
|
description='Compile a Qt compliant CSS file from a SASS stylesheet.', |
|
35
|
|
|
) |
|
36
|
|
|
parser.add_argument('input', type=str, help='The SASS stylesheet file.') |
|
37
|
|
|
parser.add_argument( |
|
38
|
|
|
'-o', |
|
39
|
|
|
'--output', |
|
40
|
|
|
type=str, |
|
41
|
|
|
help='The path of the generated Qt compliant CSS file.' |
|
42
|
|
|
) |
|
43
|
|
|
parser.add_argument( |
|
44
|
|
|
'-w', |
|
45
|
|
|
'--watch', |
|
46
|
|
|
action='store_true', |
|
47
|
|
|
help='If set, recompile when the source file changes.' |
|
48
|
|
|
) |
|
49
|
|
|
return parser |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
def main(args): |
|
53
|
|
|
"""qtsass's cli entry point.""" |
|
54
|
|
|
|
|
55
|
|
|
args = create_parser().parse_args(args) |
|
56
|
|
|
file_mode = os.path.isfile(args.input) |
|
57
|
|
|
dir_mode = os.path.isdir(args.input) |
|
58
|
|
|
|
|
59
|
|
|
if file_mode and not args.output: |
|
60
|
|
|
css = compile(args.input) |
|
61
|
|
|
print(css) |
|
62
|
|
|
sys.exit(0) |
|
63
|
|
|
|
|
64
|
|
|
elif file_mode: |
|
65
|
|
|
compile_filename(args.input, args.output) |
|
66
|
|
|
|
|
67
|
|
|
elif dir_mode and not args.output: |
|
68
|
|
|
print('Error: missing required option: -o/--output') |
|
69
|
|
|
sys.exit(1) |
|
70
|
|
|
|
|
71
|
|
|
elif dir_mode: |
|
72
|
|
|
compile_dirname(args.input, args.output) |
|
73
|
|
|
|
|
74
|
|
|
else: |
|
75
|
|
|
print('Error: input must be a file or a directory') |
|
76
|
|
|
sys.exit(1) |
|
77
|
|
|
|
|
78
|
|
|
if args.watch: |
|
79
|
|
|
_log.info('qtsass is watching {}...'.format(args.input)) |
|
|
|
|
|
|
80
|
|
|
observer = watch(args.input, args.output) |
|
81
|
|
|
observer.start() |
|
82
|
|
|
try: |
|
83
|
|
|
while True: |
|
84
|
|
|
time.sleep(1) |
|
85
|
|
|
except KeyboardInterrupt: |
|
86
|
|
|
observer.stop() |
|
87
|
|
|
observer.join() |
|
88
|
|
|
sys.exit(0) |
|
89
|
|
|
|