summaryrefslogtreecommitdiff
path: root/pecan/commands
diff options
context:
space:
mode:
authorRyan Petrello <lists@ryanpetrello.com>2012-03-19 12:53:10 -0400
committerRyan Petrello <lists@ryanpetrello.com>2012-03-19 12:53:10 -0400
commit2e6e66e50abc615ea96edd7aa93ef6368a18e689 (patch)
tree3c3cf152b6b42bf86c504d263b2d68a1ca898047 /pecan/commands
parentcf5c4e4998c6adc6375e2aa13bc7f353fe3c8d3a (diff)
downloadpecan-2e6e66e50abc615ea96edd7aa93ef6368a18e689.tar.gz
Some more work on `pecan serve --reload`.
Now the app and config directories are monitored (instead of cwd()).
Diffstat (limited to 'pecan/commands')
-rw-r--r--pecan/commands/serve.py52
1 files changed, 39 insertions, 13 deletions
diff --git a/pecan/commands/serve.py b/pecan/commands/serve.py
index 3bd55f4..13b45a1 100644
--- a/pecan/commands/serve.py
+++ b/pecan/commands/serve.py
@@ -38,9 +38,12 @@ class ServeCommand(BaseCommand):
stdout=sys.stdout, stderr=sys.stderr
)
- def watch_and_spawn(self):
+ def watch_and_spawn(self, conf):
from watchdog.observers import Observer
- from watchdog.events import FileSystemEventHandler
+ from watchdog.events import (
+ FileSystemEventHandler, FileSystemMovedEvent, FileModifiedEvent,
+ DirModifiedEvent
+ )
print 'Monitoring for changes...'
self.create_subprocess()
@@ -48,24 +51,31 @@ class ServeCommand(BaseCommand):
parent = self
class AggressiveEventHandler(FileSystemEventHandler):
- def should_reload(self, path):
- extension = os.path.splitext(path)[1]
- if extension in (
- '.py', '.pyc', '.html', '.mak',
- '.mako', '.xml'
+ def should_reload(self, event):
+ for t in (
+ FileSystemMovedEvent, FileModifiedEvent, DirModifiedEvent
):
- return True
+ if isinstance(event, t):
+ return True
return False
def on_modified(self, event):
- if self.should_reload(getattr(event, 'src_path', '')):
+ if self.should_reload(event):
parent.server_process.kill()
parent.create_subprocess()
+ # Determine a list of file paths to monitor
+ paths = self.paths_to_monitor(conf)
+
event_handler = AggressiveEventHandler()
- observer = Observer()
- observer.schedule(event_handler, path=os.getcwd(), recursive=True)
- observer.start()
+ for path, recurse in paths:
+ observer = Observer()
+ observer.schedule(
+ event_handler,
+ path=path,
+ recursive=recurse
+ )
+ observer.start()
try:
while True:
@@ -73,6 +83,22 @@ class ServeCommand(BaseCommand):
except KeyboardInterrupt:
pass
+ def paths_to_monitor(self, conf):
+ paths = []
+
+ for package_name in getattr(conf.app, 'modules', []):
+ module = __import__(package_name, fromlist=['app'])
+ if hasattr(module, 'app') and hasattr(module.app, 'setup_app'):
+ paths.append((
+ os.path.dirname(module.__file__),
+ True
+ ))
+ break
+
+ paths.append((os.path.dirname(conf.__file__), False))
+ return paths
+
+
def _serve(self, app, conf):
from wsgiref.simple_server import make_server
@@ -100,7 +126,7 @@ class ServeCommand(BaseCommand):
if self.args.reload:
try:
- self.watch_and_spawn()
+ self.watch_and_spawn(conf)
except ImportError:
print('The `--reload` option requires `watchdog` to be '
'installed.')