Add base event handler, utils function

This commit is contained in:
Eli Uriegas 2017-02-19 14:15:18 -06:00
parent 9c306899ba
commit 3528ff9501
2 changed files with 60 additions and 0 deletions

48
sanic/_live_reload.py Normal file
View File

@ -0,0 +1,48 @@
import os
import signal
import time
from watchdog.events import RegexMatchingEventHandler
from watchdog.observers import Observer
class SanicEventHandler(RegexMatchingEventHandler):
def __init__(self, interval=1):
super().__init__(regexes=['.*\.py', '.*\.pyc', '.*\.pyo'])
self.interval = interval
self.last_reload = self.current_time
def on_created(self, event):
self.trigger_reload(event.src_path)
def on_modified(self, event):
self.trigger_reload(event.src_path)
def on_moved(self, event):
self.trigger_reload(event.src_path)
def on_deleted(self, event):
self.trigger_reload(event.src_path)
@property
def current_time(self):
return time.time()
def trigger_reload(self, src_path):
if self.current_time - self.last_reload > self.interval:
os.kill(os.getpid(), signal.SIGHUP)
# TODO: Add a log for the src_path and reload mechanisms
self.last_reload = self.current_time
if __name__ == '__main__':
import sys
handler = SanicEventHandler()
observer = Observer()
observer.schedule(event_handler=handler, path=sys.argv[1], recursive=True)
observer.start()
try:
while observer.isAlive():
observer.join(1)
except KeyboardInterrupt:
observer.stop()
observer.join()

View File

@ -1,3 +1,5 @@
import os
import inspect
import warnings import warnings
from sanic.testing import TestClient from sanic.testing import TestClient
@ -15,3 +17,13 @@ def sanic_endpoint_test(app, method='get', uri='/', gather_request=True,
return test_client._sanic_endpoint_test( return test_client._sanic_endpoint_test(
method, uri, gather_request, debug, server_kwargs, method, uri, gather_request, debug, server_kwargs,
*request_args, **request_kwargs) *request_args, **request_kwargs)
def get_original_caller_directory():
"""Gets the directory of the original caller for any function
To be primarily used to identify where to watch for live reload situations
:returns: The absolute path to the directory of the original caller
"""
return os.path.split(os.path.abspath(inspect.stack()[-1]))[0]