Permalink
Browse files

Setting up dev server to handle file-change monitoring.

  • Loading branch information...
1 parent 3cce022 commit 1b6751079e95a2ef20623d06155d6c94f33cc42a @robatron robatron committed Feb 22, 2012
Showing with 40 additions and 23 deletions.
  1. +35 −21 wok/dev_server.py
  2. +5 −2 wok/engine.py
View
56 wok/dev_server.py
@@ -20,24 +20,38 @@
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
-def run(serv_dir=None, host='', port=8000):
- ''' Run the development server on `host`:`port`, and serve the files in
- `serv_dir`. If `serv_dir` is not provided, it will use the current working
- directory.
- '''
-
- if serv_dir:
- os.chdir(serv_dir)
-
- server = HTTPServer
- handler = SimpleHTTPRequestHandler
- httpd = server((host, port), handler)
- socket_info = httpd.socket.getsockname()
-
- print "Starting dev server on http://%s:%s... (Ctrl-c to stop)"\
- %(socket_info[0], socket_info[1])
- try:
- httpd.serve_forever()
- except KeyboardInterrupt:
- print "\nStopping development server..."
-
+class dev_server:
+
+ def __init__(self, serv_dir=None, host='', port=8000, watch_dirs=None,
+ change_handler=None):
+ ''' Initialize a new development server on `host`:`port`, and serve the
+ files in `serv_dir`. If `serv_dir` is not provided, it will use the
+ current working directory.
+ '''
+ self.serv_dir = serv_dir
+ self.host = host
+ self.port = port
+ self.watch_dirs = watch_dirs
+ self.change_handler = change_handler
+
+ def run(self):
+ if self.serv_dir:
+ os.chdir(self.serv_dir)
+
+ server = HTTPServer
+ req_handler = SimpleHTTPRequestHandler
+ httpd = server((self.host, self.port), req_handler)
+ socket_info = httpd.socket.getsockname()
+
+ print "Starting dev server on http://%s:%s... (Ctrl-c to stop)"\
+ %(socket_info[0], socket_info[1])
+ try:
+ httpd.serve_forever()
+ except KeyboardInterrupt:
+ print "\nStopping development server..."
+
+ def dirs_changed(watch_dirs):
+ ''' Check if directories listed in `watch_dirs` have changed since the
+ last time this was called
+ '''
+ pass
View
7 wok/engine.py
@@ -12,7 +12,7 @@
from wok.page import Page, Author
from wok import renderers
from wok import util
-from wok import devserver
+from wok.dev_server import dev_server
class Engine(object):
@@ -112,7 +112,10 @@ def __init__(self, output_lvl=1):
# Run the dev server after generating pages if the user said to
if cli_options.runserver:
output_dir = os.path.join(self.options['output_dir'])
- devserver.run(output_dir, cli_options.address, cli_options.port)
+ host = '' if cli_options.address is None else cli_options.address
+ port = 8000 if cli_options.port is None else cli_options.port
+ server = dev_server(serv_dir=output_dir, host=host, port=port)
+ server.run()
def read_options(self):
"""Load options from the config file."""

0 comments on commit 1b67510

Please sign in to comment.