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
|
|
|
"""The filesystem watcher api.""" |
10
|
|
|
|
11
|
|
|
# yapf: disable |
12
|
|
|
|
13
|
|
|
from __future__ import absolute_import |
14
|
|
|
import logging |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
# yapf: enable |
18
|
|
|
|
19
|
|
|
_log = logging.getLogger(__name__) |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class Watcher(object): |
23
|
|
|
"""Watcher base class. |
24
|
|
|
|
25
|
|
|
Watchers monitor a file or directory and call the on_change method when a |
26
|
|
|
change occurs. The on_change method should trigger the compiler function |
27
|
|
|
passed in during construction and dispatch the result to all connected |
28
|
|
|
callbacks. |
29
|
|
|
|
30
|
|
|
Watcher implementations must inherit from this base class. Subclasses |
31
|
|
|
should perform any setup required in the setup method, rather than |
32
|
|
|
overriding __init__. |
33
|
|
|
""" |
34
|
|
|
|
35
|
|
|
def __init__(self, watch_dir, compiler, args=None, kwargs=None): |
36
|
|
|
"""Store initialization values and call Watcher.setup.""" |
37
|
|
|
self._watch_dir = watch_dir |
38
|
|
|
self._compiler = compiler |
39
|
|
|
self._args = args or () |
40
|
|
|
self._kwargs = kwargs or {} |
41
|
|
|
self._callbacks = set() |
42
|
|
|
self._log = _log |
43
|
|
|
self.setup() |
44
|
|
|
|
45
|
|
|
def setup(self): |
46
|
|
|
"""Perform any setup required here. |
47
|
|
|
|
48
|
|
|
Rather than implement __init__, subclasses can perform any setup in |
49
|
|
|
this method. |
50
|
|
|
""" |
51
|
|
|
return NotImplemented |
52
|
|
|
|
53
|
|
|
def start(self): |
54
|
|
|
"""Start this Watcher.""" |
55
|
|
|
return NotImplemented |
56
|
|
|
|
57
|
|
|
def stop(self): |
58
|
|
|
"""Stop this Watcher.""" |
59
|
|
|
return NotImplemented |
60
|
|
|
|
61
|
|
|
def join(self): |
62
|
|
|
"""Wait for this Watcher to finish.""" |
63
|
|
|
return NotImplemented |
64
|
|
|
|
65
|
|
|
def compile(self): |
66
|
|
|
"""Call the Watcher's compiler.""" |
67
|
|
|
self._log.debug( |
68
|
|
|
'Compiling sass...%s(*%s, **%s)', |
69
|
|
|
self._compiler, |
70
|
|
|
self._args, |
71
|
|
|
self._kwargs, |
72
|
|
|
) |
73
|
|
|
return self._compiler(*self._args, **self._kwargs) |
74
|
|
|
|
75
|
|
|
def compile_and_dispatch(self): |
76
|
|
|
"""Compile and dispatch the resulting css to connected callbacks.""" |
77
|
|
|
self._log.debug('Compiling and dispatching....') |
78
|
|
|
|
79
|
|
|
try: |
80
|
|
|
css = self.compile() |
81
|
|
|
except Exception: |
82
|
|
|
self._log.exception('Failed to compile...') |
83
|
|
|
return |
84
|
|
|
|
85
|
|
|
self.dispatch(css) |
86
|
|
|
|
87
|
|
|
def dispatch(self, css): |
88
|
|
|
"""Dispatch css to connected callbacks.""" |
89
|
|
|
self._log.debug('Dispatching callbacks...') |
90
|
|
|
for callback in self._callbacks: |
91
|
|
|
callback(css) |
92
|
|
|
|
93
|
|
|
def on_change(self): |
94
|
|
|
"""Call when a change is detected. |
95
|
|
|
|
96
|
|
|
Subclasses must call this method when they detect a change. Subclasses |
97
|
|
|
may also override this method in order to manually compile and dispatch |
98
|
|
|
callbacks. For example, a Qt implementation may use signals and slots |
99
|
|
|
to ensure that compiling and executing callbacks happens in the main |
100
|
|
|
GUI thread. |
101
|
|
|
""" |
102
|
|
|
self._log.debug('Change detected...') |
103
|
|
|
self.compile_and_dispatch() |
104
|
|
|
|
105
|
|
|
def connect(self, fn): |
106
|
|
|
"""Connect a callback to this Watcher. |
107
|
|
|
|
108
|
|
|
All callbacks are called when a change is detected. Callbacks are |
109
|
|
|
passed the compiled css. |
110
|
|
|
""" |
111
|
|
|
self._log.debug('Connecting callback: %s', fn) |
112
|
|
|
self._callbacks.add(fn) |
113
|
|
|
|
114
|
|
|
def disconnect(self, fn): |
115
|
|
|
"""Disconnect a callback from this Watcher.""" |
116
|
|
|
self._log.debug('Disconnecting callback: %s', fn) |
117
|
|
|
self._callbacks.discard(fn) |
118
|
|
|
|