Skip to content

Commit f8fe7e5

Browse files
committedMay 19, 2015
misc fixes
1 parent 977aa62 commit f8fe7e5

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed
 

‎nikola/plugins/command/auto/auto.py

+20-14
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import mimetypes
3131
import os
3232
import subprocess
33-
import sys
3433
try:
3534
from urlparse import urlparse
3635
except ImportError:
@@ -40,10 +39,13 @@
4039

4140
from blinker import signal
4241
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
4749

4850
from nikola.plugin_categories import Command
4951
from nikola.utils import req_missing
@@ -95,11 +97,15 @@ class CommandAuto(Command):
9597
def _execute(self, options, args):
9698
"""Start the watcher."""
9799

100+
if WebSocket is None:
101+
req_missing(['ws4py'], 'use the "auto" command')
102+
return
103+
98104
arguments = ['build']
99105
if self.site.configuration_filename != 'conf.py':
100106
arguments = ['--conf=' + self.site.configuration_filename] + arguments
101107

102-
command_line = 'nikola ' + ' '.join(arguments)
108+
self.command_line = 'nikola ' + ' '.join(arguments)
103109

104110
# Run an initial build so we are up-to-date
105111
subprocess.call(["nikola"] + arguments)
@@ -157,14 +163,15 @@ def _execute(self, options, args):
157163
class Mixed(WebSocketWSGIApplication):
158164
"""A class that supports WS and HTTP protocols in the same port."""
159165
def __call__(self, environ, start_response):
160-
uri = wsgiref.util.request_uri(environ)
161166
if environ.get('HTTP_UPGRADE') is None:
162167
return parent.serve_static(environ, start_response)
163168
return super(Mixed, self).__call__(environ, start_response)
164169

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+
)
168175
ws.initialize_websockets_manager()
169176
print("Serving on port {0}...".format(port))
170177

@@ -173,9 +180,8 @@ def __call__(self, environ, start_response):
173180
except KeyboardInterrupt:
174181
ws.server_close()
175182

176-
177183
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)
179185
if p.wait() != 0:
180186
error_signal.send(error=p.stderr.read())
181187

@@ -202,11 +208,10 @@ def serve_static(self, environ, start_response):
202208
elif p_uri.path == '/livereload.js':
203209
with open(LRJS_PATH) as fd:
204210
start_response(b'200 OK', [(b'Content-type', mimetype)])
205-
return inject_js(mimetype, fd.read())
211+
return self.inject_js(mimetype, fd.read())
206212
start_response(b'404 ERR', [])
207213
return ['404 {0}'.format(uri)]
208214

209-
210215
def inject_js(self, mimetype, data):
211216
"""Inject livereload.js in HTML files."""
212217
if mimetype == 'text/html':
@@ -217,6 +222,7 @@ def inject_js(self, mimetype, data):
217222

218223
pending = []
219224

225+
220226
class LRSocket(WebSocket):
221227
"""Speak Livereload protocol."""
222228

0 commit comments

Comments
 (0)
Please sign in to comment.