Refactor watchdog event evaluation functions

This commit is contained in:
Ben 2019-05-29 23:00:29 +01:00
parent 1127cedb21
commit 065d949ec7

View File

@ -1,21 +1,20 @@
import os import os
import copy
def evaluate_handler_file_event(handler_file_event): def evaluate_listener_file_event(listener_file_event):
# Ignore all directory events # Ignore all directory events
if handler_file_event.is_directory: if listener_file_event.is_directory:
return False return False
handler_module_path = handler_file_event.src_path[2:] listener_module_path = listener_file_event.src_path[2:]
# Ignore non-Python files # Ignore non-Python files
if handler_module_path[-3:] != ".py": if listener_module_path[-3:] != ".py":
return False return False
handler_module = handler_module_path.replace(os.path.sep, ".")[:-3] listener_module = listener_module_path.replace(os.path.sep, ".")[:-3]
return handler_module_path, handler_module return listener_module_path, listener_module
def evaluate_plugin_file_event(plugin_file_event): def evaluate_plugin_file_event(plugin_file_event):
@ -23,16 +22,16 @@ def evaluate_plugin_file_event(plugin_file_event):
if plugin_file_event.is_directory: if plugin_file_event.is_directory:
return False return False
handler_module_path = plugin_file_event.src_path[2:] plugin_module_path = plugin_file_event.src_path[2:]
# Ignore non-Python files # Ignore non-Python files
if handler_module_path[-3:] != ".py": if plugin_module_path[-3:] != ".py":
return False return False
# Remove file extension and replace path separator with dots. Then make like a banana.. and split. # Remove file extension and replace path separator with dots. Then make like a banana.. and split.
handler_module_tokens = handler_module_path.replace(os.path.sep, ".")[:-3].split(".") plugin_module_tokens = plugin_module_path.replace(os.path.sep, ".")[:-3].split(".")
if handler_module_tokens.pop() == "__init__": if plugin_module_tokens.pop() == "__init__":
return handler_module_path, ".".join(handler_module_tokens) return plugin_module_path, ".".join(plugin_module_tokens)
return False return False