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
|
|
|
# yapf: disable |
13
|
|
|
|
14
|
|
|
from __future__ import absolute_import, print_function |
15
|
|
|
|
16
|
|
|
# Standard library imports |
17
|
|
|
import argparse |
18
|
|
|
import logging |
19
|
|
|
import os |
20
|
|
|
import sys |
21
|
|
|
import time |
22
|
|
|
|
23
|
|
|
# Local imports |
24
|
|
|
from qtsass.api import ( |
25
|
|
|
compile, |
26
|
|
|
compile_dirname, |
27
|
|
|
compile_filename, |
28
|
|
|
enable_logging, |
29
|
|
|
watch, |
30
|
|
|
) |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
# yapf: enable |
34
|
|
|
|
35
|
|
|
_log = logging.getLogger(__name__) |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
def create_parser(): |
39
|
|
|
"""Create qtsass's cli parser.""" |
40
|
|
|
parser = argparse.ArgumentParser( |
41
|
|
|
prog='QtSASS', |
42
|
|
|
description='Compile a Qt compliant CSS file from a SASS stylesheet.', |
43
|
|
|
) |
44
|
|
|
parser.add_argument( |
45
|
|
|
'input', |
46
|
|
|
type=str, |
47
|
|
|
help='The SASS stylesheet file.', |
48
|
|
|
) |
49
|
|
|
parser.add_argument( |
50
|
|
|
'-o', |
51
|
|
|
'--output', |
52
|
|
|
type=str, |
53
|
|
|
help='The path of the generated Qt compliant CSS file.', |
54
|
|
|
) |
55
|
|
|
parser.add_argument( |
56
|
|
|
'-w', |
57
|
|
|
'--watch', |
58
|
|
|
action='store_true', |
59
|
|
|
help='If set, recompile when the source file changes.', |
60
|
|
|
) |
61
|
|
|
parser.add_argument( |
62
|
|
|
'-d', |
63
|
|
|
'--debug', |
64
|
|
|
action='store_true', |
65
|
|
|
help='Set the logging level to DEBUG.', |
66
|
|
|
) |
67
|
|
|
return parser |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
def main(): |
71
|
|
|
"""CLI entry point.""" |
72
|
|
|
args = create_parser().parse_args() |
73
|
|
|
|
74
|
|
|
# Setup CLI logging |
75
|
|
|
debug = os.environ.get('QTSASS_DEBUG', args.debug) |
76
|
|
|
if debug in ('1', 'true', 'True', 'TRUE', 'on', 'On', 'ON', True): |
77
|
|
|
level = logging.DEBUG |
78
|
|
|
else: |
79
|
|
|
level = logging.INFO |
80
|
|
|
enable_logging(level) |
81
|
|
|
|
82
|
|
|
# Add a StreamHandler |
83
|
|
|
handler = logging.StreamHandler() |
84
|
|
|
if level == logging.DEBUG: |
85
|
|
|
fmt = '%(levelname)-8s: %(name)s> %(message)s' |
86
|
|
|
handler.setFormatter(logging.Formatter(fmt)) |
87
|
|
|
logging.root.addHandler(handler) |
88
|
|
|
logging.root.setLevel(level) |
89
|
|
|
|
90
|
|
|
file_mode = os.path.isfile(args.input) |
91
|
|
|
dir_mode = os.path.isdir(args.input) |
92
|
|
|
|
93
|
|
|
if file_mode and not args.output: |
94
|
|
|
with open(args.input, 'r') as f: |
95
|
|
|
string = f.read() |
96
|
|
|
|
97
|
|
|
css = compile( |
98
|
|
|
string, |
99
|
|
|
include_paths=os.path.abspath(os.path.dirname(args.input)), |
100
|
|
|
) |
101
|
|
|
print(css) |
102
|
|
|
sys.exit(0) |
103
|
|
|
|
104
|
|
|
elif file_mode: |
105
|
|
|
_log.debug('compile_filename({}, {})'.format(args.input, args.output)) |
106
|
|
|
compile_filename(args.input, args.output) |
107
|
|
|
|
108
|
|
|
elif dir_mode and not args.output: |
109
|
|
|
print('Error: missing required option: -o/--output') |
110
|
|
|
sys.exit(1) |
111
|
|
|
|
112
|
|
|
elif dir_mode: |
113
|
|
|
_log.debug('compile_dirname({}, {})'.format(args.input, args.output)) |
114
|
|
|
compile_dirname(args.input, args.output) |
115
|
|
|
|
116
|
|
|
else: |
117
|
|
|
print('Error: input must be a file or a directory') |
118
|
|
|
sys.exit(1) |
119
|
|
|
|
120
|
|
|
if args.watch: |
121
|
|
|
_log.info('qtsass is watching {}...'.format(args.input)) |
122
|
|
|
|
123
|
|
|
watcher = watch(args.input, args.output) |
124
|
|
|
watcher.start() |
125
|
|
|
try: |
126
|
|
|
while True: |
127
|
|
|
time.sleep(0.5) |
128
|
|
|
except KeyboardInterrupt: |
129
|
|
|
watcher.stop() |
130
|
|
|
watcher.join() |
131
|
|
|
sys.exit(0) |
132
|
|
|
|