Skip to content

Commit 2315d81

Browse files
committed
[Watch] Improved watch-and-recompile SASS source file modification system
1 parent 8463a66 commit 2315d81

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

qtsass/qtsass.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
logger = logging.getLogger(__name__)
1111

1212

13-
1413
def rgba(r, g, b, a):
1514
result = "rgba({}, {}, {}, {}%)"
1615
if isinstance(r, sass.SassNumber):
@@ -77,40 +76,48 @@ def compile_to_css_and_save(input_file, dest_file):
7776
if dest_file:
7877
with open(dest_file, 'w') as css_file:
7978
css_file.write(stylesheet)
80-
logger.info("Created CSS file {}".format(args.output))
79+
logger.info("Created CSS file {}".format(dest_file))
8180
else:
8281
print(stylesheet)
8382

84-
class MyEventHandler(FileSystemEventHandler):
83+
84+
class SourceModificationEventHandler(FileSystemEventHandler):
8585
def __init__(self, input_file, dest_file):
86-
super(MyEventHandler, self).__init__()
86+
super(SourceModificationEventHandler, self).__init__()
8787
self._input_file = input_file
8888
self._dest_file = dest_file
8989
self._watched_extension = os.path.split(self._input_file)[1]
9090

9191
def on_modified(self, event):
92-
# TODO: watch a specific kind of file
93-
# but for now, watchdog only returns the directory...
94-
# if os.path.split(event.src_path)[1] == self._watched_extension:
95-
logger.debug("Have to reload : {}".format(event.src_path))
96-
compile_to_css_and_save(self._input_file, self._dest_file)
97-
92+
# On Mac, event will always be a directory.
93+
# 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
98105

99106
if __name__ == "__main__":
100107
parser = argparse.ArgumentParser(prog="QtSASS",
101108
description="Compile a Qt compliant CSS file from a SASS stylesheet.",
102109
)
103110
parser.add_argument('input', type=str, help="The SASS stylesheet file.")
104111
parser.add_argument('-o', '--output', type=str, help="The path of the generated Qt compliant CSS file.")
105-
parser.add_argument('-w', '--watch', help="Whether to watch source file "
106-
"and automatically recompile on file change.")
112+
parser.add_argument('-w', '--watch', action='store_true', help="Whether to watch source file "
113+
"and automatically recompile on file change.")
107114

108115
args = parser.parse_args()
109-
110-
stylesheet = compile_to_css(args.input)
116+
compile_to_css_and_save(args.input, args.output)
111117

112118
if args.watch:
113-
event_handler = MyEventHandler(args.input, args.output)
119+
event_handler = SourceModificationEventHandler(args.input, args.output)
120+
logging.info("qtsass is watching {}...".format(args.input))
114121
observer = Observer()
115122
observer.schedule(event_handler, os.path.dirname(args.input), recursive=False)
116123
observer.start()
@@ -120,5 +127,3 @@ def on_modified(self, event):
120127
except KeyboardInterrupt:
121128
observer.stop()
122129
observer.join()
123-
else:
124-
compile_to_css_and_save(args.input, None)

0 commit comments

Comments
 (0)