|
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
|
|
|
"""Contains the Qt implementation of the Watcher api.""" |
|
10
|
|
|
|
|
11
|
|
|
# yapf: disable |
|
12
|
|
|
|
|
13
|
|
|
from __future__ import absolute_import |
|
14
|
|
|
|
|
15
|
|
|
# Third party imports |
|
16
|
|
|
from watchdog.events import FileSystemEventHandler |
|
17
|
|
|
from watchdog.observers import Observer |
|
18
|
|
|
|
|
19
|
|
|
# Local imports |
|
20
|
|
|
from qtsass.watchers.api import Watcher |
|
21
|
|
|
|
|
22
|
|
|
# yapf: enable |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class SourceChangedEventHandler(FileSystemEventHandler): |
|
26
|
|
|
"""Source event hanlder.""" |
|
27
|
|
|
|
|
28
|
|
|
def __init__(self, compile, callbacks): |
|
29
|
|
|
"""Source event hanlder.""" |
|
30
|
|
|
super(SourceChangedEventHandler, self).__init__() |
|
31
|
|
|
self._compile = compile |
|
32
|
|
|
self._callbacks = callbacks |
|
33
|
|
|
|
|
34
|
|
|
def on_modified(self, event): |
|
35
|
|
|
"""Override watchdog method to handle on file modification events.""" |
|
36
|
|
|
css = self._compile() |
|
37
|
|
|
for callback in self._callbacks: |
|
38
|
|
|
callback(css) |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
class WatchdogWatcher(Watcher): |
|
42
|
|
|
"""Watches a file or directory for changes and calls the provided compiler |
|
43
|
|
|
whenever a change is detected. |
|
44
|
|
|
""" |
|
45
|
|
|
|
|
46
|
|
|
def setup(self): |
|
47
|
|
|
self._callbacks = set() |
|
48
|
|
|
self._handler = SourceChangedEventHandler( |
|
49
|
|
|
self.compile, |
|
50
|
|
|
self._callbacks |
|
51
|
|
|
) |
|
52
|
|
|
self._observer = Observer() |
|
53
|
|
|
self._observer.schedule(self._handler, self._watch_dir, recursive=True) |
|
54
|
|
|
|
|
55
|
|
|
def connect(self, fn): |
|
56
|
|
|
self._callbacks.add(fn) |
|
57
|
|
|
|
|
58
|
|
|
def disconnect(self, fn): |
|
59
|
|
|
self._callbacks.discard(fn) |
|
60
|
|
|
|
|
61
|
|
|
def start(self): |
|
62
|
|
|
self._observer.start() |
|
63
|
|
|
|
|
64
|
|
|
def stop(self): |
|
65
|
|
|
self._observer.stop() |
|
66
|
|
|
|
|
67
|
|
|
def join(self): |
|
68
|
|
|
self._observer.join() |
|
69
|
|
|
|