Skip to content

Commit 933e104

Browse files
committed
[Watch] Recover from sass compile error + fix watch files with same extension as input_file (split was used instead of splitext)
1 parent 6afa43f commit 933e104

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

qtsass/qtsass.py

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,19 @@ def qt_conform(input_str):
6161

6262

6363
def compile_to_css(input_file):
64-
return qt_conform(sass.compile(string=css_conform(input_file),
65-
source_comments=False,
66-
custom_functions={
67-
'qlineargradient': qlineargradient,
68-
'rgba': rgba
69-
}
70-
)
71-
)
72-
64+
logger.debug("Compiling {}...".format(input_file))
65+
try:
66+
return qt_conform(sass.compile(string=css_conform(input_file),
67+
source_comments=False,
68+
custom_functions={
69+
'qlineargradient': qlineargradient,
70+
'rgba': rgba
71+
}
72+
)
73+
)
74+
except sass.CompileError as e:
75+
logging.error("Failed to compile {}:\n{}".format(input_file, e))
76+
return ""
7377

7478
def compile_to_css_and_save(input_file, dest_file):
7579
stylesheet = compile_to_css(input_file)
@@ -86,22 +90,29 @@ def __init__(self, input_file, dest_file):
8690
super(SourceModificationEventHandler, self).__init__()
8791
self._input_file = input_file
8892
self._dest_file = dest_file
89-
self._watched_extension = os.path.split(self._input_file)[1]
93+
self._watched_extension = os.path.splitext(self._input_file)[1]
94+
95+
def _recompile(self):
96+
i = 0
97+
success = False
98+
while i < 10 and not success:
99+
try:
100+
time.sleep(0.2)
101+
compile_to_css_and_save(self._input_file, self._dest_file)
102+
success = True
103+
except FileNotFoundError:
104+
i += 1
90105

91106
def on_modified(self, event):
92107
# On Mac, event will always be a directory.
93108
# On Windows, only recompile if event's file has the same extension as the input file
94-
if event.is_directory or os.path.split(event.src_path)[1] == self._watched_extension:
95-
logger.debug("Recompiling {}...".format(event.src_path))
96-
i = 0
97-
success = False
98-
while i < 10 and not success:
99-
try:
100-
time.sleep(0.2)
101-
compile_to_css_and_save(self._input_file, self._dest_file)
102-
success = True
103-
except FileNotFoundError:
104-
i += 1
109+
if event.is_directory or os.path.splitext(event.src_path)[1] == self._watched_extension:
110+
self._recompile()
111+
112+
def on_created(self, event):
113+
if os.path.splitext(event.src_path)[1] == self._watched_extension:
114+
self._recompile()
115+
105116

106117
if __name__ == "__main__":
107118
parser = argparse.ArgumentParser(prog="QtSASS",

0 commit comments

Comments
 (0)