2019-03-01 19:41:40 +00:00
|
|
|
import sys
|
|
|
|
import importlib
|
2019-04-10 23:52:59 +00:00
|
|
|
import copy
|
2019-03-01 19:41:40 +00:00
|
|
|
from watchdog.events import FileSystemEventHandler
|
|
|
|
|
2019-04-10 23:52:59 +00:00
|
|
|
from Houdini.Handlers import listeners_from_module, remove_handlers_by_module
|
|
|
|
from Houdini.Events import evaluate_handler_file_event
|
2019-03-01 19:41:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HandlerFileEventHandler(FileSystemEventHandler):
|
|
|
|
|
|
|
|
def __init__(self, server):
|
|
|
|
self.logger = server.logger
|
|
|
|
self.server = server
|
|
|
|
|
|
|
|
def on_created(self, event):
|
|
|
|
handler_module_details = evaluate_handler_file_event(event)
|
|
|
|
|
|
|
|
if not handler_module_details:
|
|
|
|
return
|
|
|
|
|
|
|
|
handler_module_path, handler_module = handler_module_details
|
|
|
|
|
2019-04-10 23:52:59 +00:00
|
|
|
if '__init__.py' in handler_module_path:
|
2019-03-01 19:41:40 +00:00
|
|
|
return
|
|
|
|
|
2019-04-10 23:52:59 +00:00
|
|
|
self.logger.debug('New handler module detected %s', handler_module)
|
2019-03-01 19:41:40 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
module = importlib.import_module(handler_module)
|
|
|
|
listeners_from_module(self.server.xt_listeners, self.server.xml_listeners, module)
|
|
|
|
except Exception as import_error:
|
2019-04-10 23:52:59 +00:00
|
|
|
self.logger.error('%s detected in %s, not importing.', import_error.__class__.__name__, handler_module)
|
2019-03-01 19:41:40 +00:00
|
|
|
|
|
|
|
def on_deleted(self, event):
|
|
|
|
handler_module_details = evaluate_handler_file_event(event)
|
|
|
|
|
|
|
|
if not handler_module_details:
|
|
|
|
return
|
|
|
|
|
|
|
|
handler_module_path, handler_module = handler_module_details
|
|
|
|
|
|
|
|
if handler_module not in sys.modules:
|
|
|
|
return
|
|
|
|
|
2019-04-10 23:52:59 +00:00
|
|
|
self.logger.debug('Deleting listeners registered by %s...', handler_module)
|
2019-03-01 19:41:40 +00:00
|
|
|
|
|
|
|
remove_handlers_by_module(self.server.xt_listeners, self.server.xml_listeners, handler_module_path)
|
|
|
|
|
|
|
|
def on_modified(self, event):
|
|
|
|
handler_module_details = evaluate_handler_file_event(event)
|
|
|
|
|
|
|
|
if not handler_module_details:
|
|
|
|
return
|
|
|
|
|
|
|
|
handler_module_path, handler_module = handler_module_details
|
|
|
|
|
|
|
|
if handler_module not in sys.modules:
|
|
|
|
return False
|
|
|
|
|
2019-04-10 23:52:59 +00:00
|
|
|
self.logger.info('Reloading %s', handler_module)
|
2019-03-01 19:41:40 +00:00
|
|
|
|
2019-04-10 23:52:59 +00:00
|
|
|
xt_listeners, xml_listeners = copy.copy(self.server.xt_listeners), copy.copy(self.server.xml_listeners)
|
|
|
|
|
|
|
|
remove_handlers_by_module(self.server.xt_listeners, self.server.xml_listeners, handler_module_path)
|
2019-03-01 19:41:40 +00:00
|
|
|
|
|
|
|
handler_module_object = sys.modules[handler_module]
|
|
|
|
|
|
|
|
try:
|
|
|
|
module = importlib.reload(handler_module_object)
|
|
|
|
listeners_from_module(self.server.xt_listeners, self.server.xml_listeners, module)
|
|
|
|
|
2019-04-10 23:52:59 +00:00
|
|
|
self.logger.info('Successfully reloaded %s!', handler_module)
|
2019-03-01 19:41:40 +00:00
|
|
|
except Exception as rebuild_error:
|
2019-04-10 23:52:59 +00:00
|
|
|
self.logger.error('%s detected in %s, not reloading.', rebuild_error.__class__.__name__, handler_module)
|
|
|
|
self.logger.info('Restoring handler references...')
|
2019-03-01 19:41:40 +00:00
|
|
|
|
|
|
|
self.server.xt_listeners = xt_listeners
|
|
|
|
self.server.xml_listeners = xml_listeners
|
|
|
|
|
2019-04-10 23:52:59 +00:00
|
|
|
self.logger.info('Handler references restored. Phew!')
|