mirror of
https://github.com/solero/houdini.git
synced 2024-11-12 13:48:20 +00:00
Make watchdog file handlers compatible with new handler system
This commit is contained in:
parent
da26cea75b
commit
58d886d09d
@ -1,9 +1,10 @@
|
||||
import sys
|
||||
import importlib
|
||||
import copy
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
|
||||
from Houdini.Handlers import listeners_from_module
|
||||
from Houdini.Events import evaluate_handler_file_event, remove_handlers_by_module
|
||||
from Houdini.Handlers import listeners_from_module, remove_handlers_by_module
|
||||
from Houdini.Events import evaluate_handler_file_event
|
||||
|
||||
|
||||
class HandlerFileEventHandler(FileSystemEventHandler):
|
||||
@ -20,16 +21,16 @@ class HandlerFileEventHandler(FileSystemEventHandler):
|
||||
|
||||
handler_module_path, handler_module = handler_module_details
|
||||
|
||||
if "__init__.py" in handler_module_path:
|
||||
if '__init__.py' in handler_module_path:
|
||||
return
|
||||
|
||||
self.logger.debug("New handler module detected %s", handler_module)
|
||||
self.logger.debug('New handler module detected %s', handler_module)
|
||||
|
||||
try:
|
||||
module = importlib.import_module(handler_module)
|
||||
listeners_from_module(self.server.xt_listeners, self.server.xml_listeners, module)
|
||||
except Exception as import_error:
|
||||
self.logger.error("%s detected in %s, not importing.", import_error.__class__.__name__, handler_module)
|
||||
self.logger.error('%s detected in %s, not importing.', import_error.__class__.__name__, handler_module)
|
||||
|
||||
def on_deleted(self, event):
|
||||
handler_module_details = evaluate_handler_file_event(event)
|
||||
@ -42,7 +43,7 @@ class HandlerFileEventHandler(FileSystemEventHandler):
|
||||
if handler_module not in sys.modules:
|
||||
return
|
||||
|
||||
self.logger.debug("Deleting listeners registered by %s..", handler_module)
|
||||
self.logger.debug('Deleting listeners registered by %s...', handler_module)
|
||||
|
||||
remove_handlers_by_module(self.server.xt_listeners, self.server.xml_listeners, handler_module_path)
|
||||
|
||||
@ -57,10 +58,11 @@ class HandlerFileEventHandler(FileSystemEventHandler):
|
||||
if handler_module not in sys.modules:
|
||||
return False
|
||||
|
||||
self.logger.info("Reloading %s", handler_module)
|
||||
self.logger.info('Reloading %s', handler_module)
|
||||
|
||||
xt_listeners, xml_listeners = remove_handlers_by_module(self.server.xt_listeners, self.server.xml_listeners,
|
||||
handler_module_path)
|
||||
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)
|
||||
|
||||
handler_module_object = sys.modules[handler_module]
|
||||
|
||||
@ -68,12 +70,12 @@ class HandlerFileEventHandler(FileSystemEventHandler):
|
||||
module = importlib.reload(handler_module_object)
|
||||
listeners_from_module(self.server.xt_listeners, self.server.xml_listeners, module)
|
||||
|
||||
self.logger.info("Successfully reloaded %s!", handler_module)
|
||||
self.logger.info('Successfully reloaded %s!', handler_module)
|
||||
except Exception as rebuild_error:
|
||||
self.logger.error("%s detected in %s, not reloading.", rebuild_error.__class__.__name__, handler_module)
|
||||
self.logger.info("Restoring handler references...")
|
||||
self.logger.error('%s detected in %s, not reloading.', rebuild_error.__class__.__name__, handler_module)
|
||||
self.logger.info('Restoring handler references...')
|
||||
|
||||
self.server.xt_listeners = xt_listeners
|
||||
self.server.xml_listeners = xml_listeners
|
||||
|
||||
self.logger.info("Handler references restored. Phew!")
|
||||
self.logger.info('Handler references restored. Phew!')
|
||||
|
86
Houdini/Events/PluginFileEvent.py
Normal file
86
Houdini/Events/PluginFileEvent.py
Normal file
@ -0,0 +1,86 @@
|
||||
import sys
|
||||
import importlib
|
||||
import os.path
|
||||
import copy
|
||||
import asyncio
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
|
||||
from Houdini.Events import evaluate_handler_file_event
|
||||
|
||||
|
||||
class PluginFileEventHandler(FileSystemEventHandler):
|
||||
|
||||
def __init__(self, server):
|
||||
self.logger = server.logger
|
||||
self.server = server
|
||||
|
||||
def on_created(self, event):
|
||||
plugin_module_details = evaluate_handler_file_event(event)
|
||||
|
||||
if not plugin_module_details:
|
||||
return
|
||||
|
||||
plugin_module_path, plugin_module = plugin_module_details
|
||||
|
||||
self.logger.debug('New handler module detected %s', plugin_module)
|
||||
|
||||
try:
|
||||
plugin_module_object = importlib.import_module(plugin_module)
|
||||
plugin_class = plugin_module_object.__name__.split(".")[2]
|
||||
|
||||
asyncio.run(self.server.load_plugin((plugin_module_object, plugin_class)))
|
||||
|
||||
self.logger.info('New plugin \'%s\' has been loaded.' % plugin_class)
|
||||
except Exception as import_error:
|
||||
self.logger.error('%s detected in %s, not importing.', import_error.__class__.__name__, plugin_module)
|
||||
|
||||
def on_deleted(self, event):
|
||||
plugin_module_path = event.src_path[2:]
|
||||
|
||||
plugin_module = plugin_module_path.replace(os.path.pathsep, ".")
|
||||
|
||||
if plugin_module not in sys.modules:
|
||||
return
|
||||
|
||||
self.logger.debug('Deleting listeners registered by %s.', plugin_module)
|
||||
|
||||
plugin_module_object = sys.modules[plugin_module]
|
||||
plugin_class = plugin_module_object.__name__.split(".")[2]
|
||||
|
||||
self.server.unload_plugin((plugin_module_object, plugin_class))
|
||||
|
||||
def on_modified(self, event):
|
||||
plugin_module_details = evaluate_handler_file_event(event)
|
||||
|
||||
if not plugin_module_details:
|
||||
return
|
||||
|
||||
plugin_module_path, plugin_module = plugin_module_details
|
||||
|
||||
if plugin_module not in sys.modules:
|
||||
return
|
||||
|
||||
self.logger.info('Reloading %s', plugin_module)
|
||||
|
||||
plugin_module_object = sys.modules[plugin_module]
|
||||
plugin_class = plugin_module_object.__name__.split(".")[2]
|
||||
|
||||
xt_listeners, xml_listeners = copy.copy(self.server.xt_listeners), copy.copy(self.server.xml_listeners)
|
||||
|
||||
self.server.unload_plugin((plugin_module_object, plugin_class))
|
||||
|
||||
try:
|
||||
new_plugin_module = importlib.reload(plugin_module_object)
|
||||
asyncio.run(self.server.load_plugin((new_plugin_module, plugin_class)))
|
||||
|
||||
self.logger.info('Successfully reloaded %s!', plugin_module)
|
||||
except LookupError as lookup_error:
|
||||
self.logger.warn('Did not reload plugin \'%s\': %s.', plugin_class, lookup_error)
|
||||
except Exception as rebuild_error:
|
||||
self.logger.error('%s detected in %s, not reloading.', rebuild_error.__class__.__name__, plugin_module)
|
||||
self.logger.info('Restoring handler references...')
|
||||
|
||||
self.server.xt_handlers = xt_listeners
|
||||
self.server.xml_handlers = xml_listeners
|
||||
|
||||
self.logger.info('Restored handler references. Phew!')
|
Loading…
Reference in New Issue
Block a user