-
-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathhandlers.py
More file actions
232 lines (184 loc) · 6.54 KB
/
handlers.py
File metadata and controls
232 lines (184 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# -*- coding: utf-8 -*-
"""
livereload.handlers
~~~~~~~~~~~~~~~~~~~
HTTP and WebSocket handlers for livereload.
:copyright: (c) 2013 by Hsiaoming Yang
:license: BSD, see LICENSE for more details.
"""
import datetime
import hashlib
import os
import stat
import time
import logging
from tornado import web
from tornado import ioloop
from tornado import escape
from tornado.log import gen_log
from tornado.websocket import WebSocketHandler
from tornado.util import ObjectDict
logger = logging.getLogger('livereload')
class LiveReloadHandler(WebSocketHandler):
waiters = set()
watcher = None
live_css = None
_last_reload_time = None
def allow_draft76(self):
return True
def check_origin(self, origin):
return True
def on_close(self):
if self in LiveReloadHandler.waiters:
LiveReloadHandler.waiters.remove(self)
def send_message(self, message):
if isinstance(message, dict):
message = escape.json_encode(message)
try:
self.write_message(message)
except:
logger.error('Error sending message', exc_info=True)
@classmethod
def start_tasks(cls):
if cls._last_reload_time:
return
if not cls.watcher._tasks:
logger.info('Watch current working directory')
cls.watcher.watch(os.getcwd())
cls._last_reload_time = time.time()
logger.info('Start watching changes')
if not cls.watcher.start(cls.poll_tasks):
logger.info('Start detecting changes')
ioloop.PeriodicCallback(cls.poll_tasks, 800).start()
@classmethod
def poll_tasks(cls):
filepath, delay = cls.watcher.examine()
if not filepath or delay == 'forever' or not cls.waiters:
return
reload_time = 3
if delay:
reload_time = max(delay, 3)
if filepath == '__livereload__':
reload_time = 0
if time.time() - cls._last_reload_time < reload_time:
# if you changed lot of files in one time
# it will refresh too many times
logger.info('Ignore: %s', filepath)
return
if delay:
loop = ioloop.IOLoop.current()
loop.call_later(delay, cls.reload_waiters)
else:
cls.reload_waiters()
@classmethod
def reload_waiters(cls, path=None):
logger.info(
'Reload %s waiters: %s',
len(cls.waiters),
cls.watcher.filepath,
)
if path is None:
path = cls.watcher.filepath or '*'
msg = {
'command': 'reload',
'path': path,
'liveCSS': cls.live_css,
'liveImg': True,
}
cls._last_reload_time = time.time()
for waiter in cls.waiters.copy():
try:
waiter.write_message(msg)
except:
logger.error('Error sending message', exc_info=True)
cls.waiters.remove(waiter)
def on_message(self, message):
"""Handshake with livereload.js
1. client send 'hello'
2. server reply 'hello'
3. client send 'info'
"""
message = ObjectDict(escape.json_decode(message))
if message.command == 'hello':
handshake = {
'command': 'hello',
'protocols': [
'http://livereload.com/protocols/official-7',
],
'serverName': 'livereload-tornado',
}
self.send_message(handshake)
if message.command == 'info' and 'url' in message:
logger.info('Browser Connected: %s' % message.url)
LiveReloadHandler.waiters.add(self)
class MtimeStaticFileHandler(web.StaticFileHandler):
_static_mtimes = {} # type: typing.Dict
@classmethod
def get_content_modified_time(cls, abspath):
"""Returns the time that ``abspath`` was last modified.
May be overridden in subclasses. Should return a `~datetime.datetime`
object or None.
"""
stat_result = os.stat(abspath)
modified = datetime.datetime.utcfromtimestamp(
stat_result[stat.ST_MTIME])
return modified
@classmethod
def get_content_version(cls, abspath):
"""Returns a version string for the resource at the given path.
This class method may be overridden by subclasses. The
default implementation is a hash of the file's contents.
.. versionadded:: 3.1
"""
data = cls.get_content(abspath)
hasher = hashlib.md5()
mtime_data = format(cls.get_content_modified_time(abspath), "%Y-%m-%d %H:%M:%S")
hasher.update(mtime_data.encode())
if isinstance(data, bytes):
hasher.update(data)
else:
for chunk in data:
hasher.update(chunk)
return hasher.hexdigest()
@classmethod
def _get_cached_version(cls, abs_path):
def _load_version(abs_path):
try:
hsh = cls.get_content_version(abs_path)
mtm = cls.get_content_modified_time(abs_path)
return mtm, hsh
except Exception:
gen_log.error("Could not open static file %r", abs_path)
return None, None
with cls._lock:
hashes = cls._static_hashes
mtimes = cls._static_mtimes
if abs_path not in hashes:
mtm, hsh = _load_version(abs_path)
hashes[abs_path] = mtm
mtimes[abs_path] = hsh
else:
hsh = hashes.get(abs_path)
mtm = mtimes.get(abs_path)
if mtm != cls.get_content_modified_time(abs_path):
mtm, hsh = _load_version(abs_path)
hashes[abs_path] = mtm
mtimes[abs_path] = hsh
if hsh:
return hsh
return None
class LiveReloadJSHandler(web.RequestHandler):
def get(self):
self.set_header('Content-Type', 'application/javascript')
root = os.path.abspath(os.path.dirname(__file__))
js_file = os.path.join(root, 'vendors/livereload.js')
with open(js_file, 'rb') as f:
self.write(f.read())
class ForceReloadHandler(web.RequestHandler):
def get(self):
path = self.get_argument('path', default=None) or '*'
LiveReloadHandler.reload_waiters(path)
self.write('ok')
class StaticFileHandler(MtimeStaticFileHandler):
def should_return_304(self):
return False