30
30
import mimetypes
31
31
import os
32
32
import subprocess
33
- import sys
34
33
try :
35
34
from urlparse import urlparse
36
35
except ImportError :
40
39
41
40
from blinker import signal
42
41
import pyinotify
43
- from ws4py .websocket import WebSocket
44
- from ws4py .server .wsgirefserver import WSGIServer , WebSocketWSGIRequestHandler
45
- from ws4py .server .wsgiutils import WebSocketWSGIApplication
46
- from ws4py .messaging import TextMessage
42
+ try :
43
+ from ws4py .websocket import WebSocket
44
+ from ws4py .server .wsgirefserver import WSGIServer , WebSocketWSGIRequestHandler
45
+ from ws4py .server .wsgiutils import WebSocketWSGIApplication
46
+ from ws4py .messaging import TextMessage
47
+ except ImportError :
48
+ WebSocket = None
47
49
48
50
from nikola .plugin_categories import Command
49
51
from nikola .utils import req_missing
@@ -95,11 +97,15 @@ class CommandAuto(Command):
95
97
def _execute (self , options , args ):
96
98
"""Start the watcher."""
97
99
100
+ if WebSocket is None :
101
+ req_missing (['ws4py' ], 'use the "auto" command' )
102
+ return
103
+
98
104
arguments = ['build' ]
99
105
if self .site .configuration_filename != 'conf.py' :
100
106
arguments = ['--conf=' + self .site .configuration_filename ] + arguments
101
107
102
- command_line = 'nikola ' + ' ' .join (arguments )
108
+ self . command_line = 'nikola ' + ' ' .join (arguments )
103
109
104
110
# Run an initial build so we are up-to-date
105
111
subprocess .call (["nikola" ] + arguments )
@@ -157,14 +163,15 @@ def _execute(self, options, args):
157
163
class Mixed (WebSocketWSGIApplication ):
158
164
"""A class that supports WS and HTTP protocols in the same port."""
159
165
def __call__ (self , environ , start_response ):
160
- uri = wsgiref .util .request_uri (environ )
161
166
if environ .get ('HTTP_UPGRADE' ) is None :
162
167
return parent .serve_static (environ , start_response )
163
168
return super (Mixed , self ).__call__ (environ , start_response )
164
169
165
- ws = make_server ('' , port , server_class = WSGIServer ,
166
- handler_class = WebSocketWSGIRequestHandler ,
167
- app = Mixed (handler_cls = LRSocket ))
170
+ ws = make_server (
171
+ host , port , server_class = WSGIServer ,
172
+ handler_class = WebSocketWSGIRequestHandler ,
173
+ app = Mixed (handler_cls = LRSocket )
174
+ )
168
175
ws .initialize_websockets_manager ()
169
176
print ("Serving on port {0}..." .format (port ))
170
177
@@ -173,9 +180,8 @@ def __call__(self, environ, start_response):
173
180
except KeyboardInterrupt :
174
181
ws .server_close ()
175
182
176
-
177
183
def do_rebuild (self , event ):
178
- p = subprocess .Popen ('nikola build' , shell = True , stderr = subprocess .PIPE )
184
+ p = subprocess .Popen (self . command_line , shell = True , stderr = subprocess .PIPE )
179
185
if p .wait () != 0 :
180
186
error_signal .send (error = p .stderr .read ())
181
187
@@ -202,11 +208,10 @@ def serve_static(self, environ, start_response):
202
208
elif p_uri .path == '/livereload.js' :
203
209
with open (LRJS_PATH ) as fd :
204
210
start_response (b'200 OK' , [(b'Content-type' , mimetype )])
205
- return inject_js (mimetype , fd .read ())
211
+ return self . inject_js (mimetype , fd .read ())
206
212
start_response (b'404 ERR' , [])
207
213
return ['404 {0}' .format (uri )]
208
214
209
-
210
215
def inject_js (self , mimetype , data ):
211
216
"""Inject livereload.js in HTML files."""
212
217
if mimetype == 'text/html' :
@@ -217,6 +222,7 @@ def inject_js(self, mimetype, data):
217
222
218
223
pending = []
219
224
225
+
220
226
class LRSocket (WebSocket ):
221
227
"""Speak Livereload protocol."""
222
228
0 commit comments