Tidy up watchdog event handlers

This commit is contained in:
Ben 2019-06-03 19:12:14 +01:00
parent fb7901db4d
commit cc4a394e77
3 changed files with 32 additions and 28 deletions

View File

@ -12,6 +12,10 @@ def evaluate_listener_file_event(listener_file_event):
if listener_module_path[-3:] != ".py":
return False
# Ignore package index files
if '__init__.py' in listener_module_path:
return False
listener_module = listener_module_path.replace(os.path.sep, ".")[:-3]
return listener_module_path, listener_module

View File

@ -20,17 +20,17 @@ class ListenerFileEventHandler(FileSystemEventHandler):
listener_module_path, listener_module = listener_module_details
if '__init__.py' in listener_module_path:
return
self.logger.debug('New handler module detected %s', listener_module)
self.logger.debug('New listener module detected {}'.format(listener_module))
try:
module = importlib.import_module(listener_module)
self.server.xt_listeners.load(module)
self.server.xml_listeners.load(module)
self.logger.info('New listener module loaded {}'.format(listener_module))
except Exception as import_error:
self.logger.error('%s detected in %s, not importing.', import_error.__class__.__name__, listener_module)
self.logger.error('{} detected in {}, not importing'.format(
import_error.__class__.__name__, listener_module))
def on_deleted(self, event):
listener_module_details = evaluate_listener_file_event(event)
@ -45,10 +45,13 @@ class ListenerFileEventHandler(FileSystemEventHandler):
listener_module_object = sys.modules[listener_module]
self.logger.debug('Deleting listeners registered by %s...', listener_module)
self.logger.debug('Deleting listener module {}'.format(listener_module))
self.server.xt_listeners.remove(listener_module_object)
self.server.xml_listeners.remove(listener_module_object)
del sys.modules[listener_module]
self.logger.info('Deleted listener module {}'.format(listener_module))
def on_modified(self, event):
listener_module_details = evaluate_listener_file_event(event)
@ -61,11 +64,10 @@ class ListenerFileEventHandler(FileSystemEventHandler):
if listener_module not in sys.modules:
return False
self.logger.info('Reloading %s', listener_module)
self.logger.info('Reloading listener module {}'.format(listener_module))
self.server.xt_listeners.backup()
self.server.xml_listeners.backup()
self.server.commands.backup()
listener_module_object = sys.modules[listener_module]
@ -77,13 +79,10 @@ class ListenerFileEventHandler(FileSystemEventHandler):
self.server.xt_listeners.load(module)
self.server.xml_listeners.load(module)
self.logger.info('Successfully reloaded %s!', listener_module)
self.logger.info('Successfully reloaded listener module {}!'.format(listener_module))
except Exception as rebuild_error:
self.logger.error('%s detected in %s, not reloading.', rebuild_error.__class__.__name__, listener_module)
self.logger.info('Restoring listeners...')
self.logger.error('{} detected in {}, not reloading.'.format(
rebuild_error.__class__.__name__, listener_module))
self.server.xt_listeners.restore()
self.server.xml_listeners.restore()
self.server.commands.restore()
self.logger.info('Listeners restored. Phew!')

View File

@ -2,7 +2,6 @@ import sys
import importlib
import os.path
import logging
import asyncio
from watchdog.events import FileSystemEventHandler
from houdini.events import evaluate_plugin_file_event
@ -22,30 +21,34 @@ class PluginFileEventHandler(FileSystemEventHandler):
plugin_module_path, plugin_module = plugin_module_details
self.logger.debug('New plugin detected %s', plugin_module)
self.logger.debug('New plugin detected {}'.format(plugin_module))
try:
plugin_module_object = importlib.import_module(plugin_module)
self.server.plugin.load(plugin_module_object)
self.server.plugins.load(plugin_module_object)
self.logger.info('New plugin \'%s\' has been loaded.' % plugin_module)
self.logger.info('New plugin {} has been loaded'.format(plugin_module))
except Exception as import_error:
self.logger.error('%s detected in %s, not importing.', import_error.__class__.__name__, plugin_module)
self.logger.error('{} detected in {}, not importing'.format(
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, ".")
plugin_module = plugin_module_path.replace(os.path.sep, ".")
if plugin_module not in sys.modules:
return
self.logger.debug('Deleting listeners registered by %s.', plugin_module)
self.logger.debug('Deleting plugin {}'.format(plugin_module))
plugin_module_object = sys.modules[plugin_module]
self.server.plugins.remove(plugin_module_object)
del sys.modules[plugin_module]
self.logger.info('Plugin {} has been removed'.format(plugin_module))
def on_modified(self, event):
plugin_module_details = evaluate_plugin_file_event(event)
@ -57,7 +60,7 @@ class PluginFileEventHandler(FileSystemEventHandler):
if plugin_module not in sys.modules:
return
self.logger.info('Reloading %s', plugin_module)
self.logger.info('Reloading plugin {}'.format(plugin_module))
plugin_module_object = sys.modules[plugin_module]
@ -72,15 +75,13 @@ class PluginFileEventHandler(FileSystemEventHandler):
self.server.plugins.load(new_plugin_module)
self.logger.info('Successfully reloaded %s!', plugin_module)
self.logger.info('Successfully reloaded plugin {}'.format(plugin_module))
except LookupError as lookup_error:
self.logger.warning('Did not reload plugin \'%s\': %s.', plugin_module, lookup_error)
self.logger.warning('Did not reload plugin \'{}\': {}'.format(plugin_module, 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.logger.error('{} detected in {}, not reloading'.format(
rebuild_error.__class__.__name__, plugin_module))
self.server.xt_listeners.restore()
self.server.xml_listeners.restore()
self.server.commands.restore()
self.logger.info('Restored handler references. Phew!')