mirror of
https://github.com/slgobinath/SafeEyes.git
synced 2025-01-01 00:47:31 +01:00
Initialize thread pool only if there are plugins
This commit is contained in:
parent
5e9f68ff44
commit
a1b3f6e094
@ -35,7 +35,6 @@ class Plugins:
|
|||||||
"""
|
"""
|
||||||
logging.info('Load all the plugins')
|
logging.info('Load all the plugins')
|
||||||
self.__plugins = []
|
self.__plugins = []
|
||||||
self.__thread_pool = ThreadPool(4)
|
|
||||||
|
|
||||||
for plugin in config['plugins']:
|
for plugin in config['plugins']:
|
||||||
if plugin['location'].lower() in ['left', 'right']:
|
if plugin['location'].lower() in ['left', 'right']:
|
||||||
@ -50,28 +49,34 @@ class Plugins:
|
|||||||
else:
|
else:
|
||||||
logging.warning('Ignoring the plugin ' + str(plugin['name']) + ' due to invalid location value: ' + plugin['location'])
|
logging.warning('Ignoring the plugin ' + str(plugin['name']) + ' due to invalid location value: ' + plugin['location'])
|
||||||
|
|
||||||
|
print(min([4, len(self.__plugins)]))
|
||||||
|
if self.__plugins:
|
||||||
|
self.__thread_pool = ThreadPool(min([4, len(self.__plugins)]))
|
||||||
|
|
||||||
|
|
||||||
def start(self, context):
|
def start(self, context):
|
||||||
"""
|
"""
|
||||||
Call the start function of all the plugins in separate thread.
|
Call the start function of all the plugins in separate thread.
|
||||||
"""
|
"""
|
||||||
context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes
|
if self.__plugins:
|
||||||
for plugin in self.__plugins:
|
context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes
|
||||||
try:
|
for plugin in self.__plugins:
|
||||||
self.__thread_pool.apply_async(plugin['module'].start, (context,))
|
try:
|
||||||
except Exception as e:
|
self.__thread_pool.apply_async(plugin['module'].start, (context,))
|
||||||
pass
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
|
||||||
def pre_notification(self, context):
|
def pre_notification(self, context):
|
||||||
"""
|
"""
|
||||||
Call the pre_notification function of all the plugins in separate thread.
|
Call the pre_notification function of all the plugins in separate thread.
|
||||||
"""
|
"""
|
||||||
context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes
|
if self.__plugins:
|
||||||
for plugin in self.__plugins:
|
context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes
|
||||||
try:
|
for plugin in self.__plugins:
|
||||||
self.__thread_pool.apply_async(plugin['module'].pre_notification, (context,))
|
try:
|
||||||
except Exception as e:
|
self.__thread_pool.apply_async(plugin['module'].pre_notification, (context,))
|
||||||
pass
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def pre_break(self, context):
|
def pre_break(self, context):
|
||||||
@ -81,57 +86,60 @@ class Plugins:
|
|||||||
|
|
||||||
Returns: {'left': 'Markup of plugins to be aligned on left', 'right': 'Markup of plugins to be aligned on right' }
|
Returns: {'left': 'Markup of plugins to be aligned on left', 'right': 'Markup of plugins to be aligned on right' }
|
||||||
"""
|
"""
|
||||||
context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes
|
if self.__plugins:
|
||||||
output = {'left': ' \n', 'right': ' \n'}
|
context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes
|
||||||
multiple_results = [self.__thread_pool.apply_async(plugin['module'].pre_break, (context,)) for plugin in self.__plugins]
|
output = {'left': ' \n', 'right': ' \n'}
|
||||||
for i in range(len(multiple_results)):
|
multiple_results = [self.__thread_pool.apply_async(plugin['module'].pre_break, (context,)) for plugin in self.__plugins]
|
||||||
try:
|
for i in range(len(multiple_results)):
|
||||||
result = multiple_results[i].get(timeout=1)
|
try:
|
||||||
if result:
|
result = multiple_results[i].get(timeout=1)
|
||||||
# Limit the line length to 50 characters
|
if result:
|
||||||
large_lines = list(filter(lambda x: len(x) > 50, Utility.html_to_text(result).splitlines()))
|
# Limit the line length to 50 characters
|
||||||
if large_lines:
|
large_lines = list(filter(lambda x: len(x) > 50, Utility.html_to_text(result).splitlines()))
|
||||||
logging.warning('Ignoring lengthy result from the plugin ' + self.__plugins[i]['name'])
|
if large_lines:
|
||||||
continue
|
logging.warning('Ignoring lengthy result from the plugin ' + self.__plugins[i]['name'])
|
||||||
output[self.__plugins[i]['location']] += (result + '\n\n')
|
continue
|
||||||
except Exception:
|
output[self.__plugins[i]['location']] += (result + '\n\n')
|
||||||
# Something went wrong in the plugin
|
except Exception:
|
||||||
logging.warning('Error when executing the plugin ' + self.__plugins[i]['name'])
|
# Something went wrong in the plugin
|
||||||
|
logging.warning('Error when executing the plugin ' + self.__plugins[i]['name'])
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
def post_break(self, context):
|
def post_break(self, context):
|
||||||
"""
|
"""
|
||||||
Call the post_break function of all the plugins in separate thread.
|
Call the post_break function of all the plugins in separate thread.
|
||||||
"""
|
"""
|
||||||
context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes
|
if self.__plugins:
|
||||||
for plugin in self.__plugins:
|
context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes
|
||||||
try:
|
for plugin in self.__plugins:
|
||||||
self.__thread_pool.apply_async(plugin['module'].post_break, (context,))
|
try:
|
||||||
except Exception as e:
|
self.__thread_pool.apply_async(plugin['module'].post_break, (context,))
|
||||||
pass
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
|
||||||
def exit(self, context):
|
def exit(self, context):
|
||||||
"""
|
"""
|
||||||
Call the exit function of all the plugins in separate thread.
|
Call the exit function of all the plugins in separate thread.
|
||||||
"""
|
"""
|
||||||
context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes
|
if self.__plugins:
|
||||||
|
context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes
|
||||||
|
|
||||||
|
# Give maximum 1 sec for all plugins before terminating the thread pool
|
||||||
|
multiple_results = [self.__thread_pool.apply_async(plugin['module'].exit, (context,)) for plugin in self.__plugins]
|
||||||
|
for i in range(len(multiple_results)):
|
||||||
|
try:
|
||||||
|
multiple_results[i].get(timeout=1)
|
||||||
|
except Exception:
|
||||||
|
# Something went wrong in the plugin
|
||||||
|
pass
|
||||||
|
|
||||||
# Give maximum 1 sec for all plugins before terminating the thread pool
|
|
||||||
multiple_results = [self.__thread_pool.apply_async(plugin['module'].exit, (context,)) for plugin in self.__plugins]
|
|
||||||
for i in range(len(multiple_results)):
|
|
||||||
try:
|
try:
|
||||||
multiple_results[i].get(timeout=1)
|
self.__thread_pool.terminate() # Shutdown the pool
|
||||||
except Exception:
|
except Exception as e:
|
||||||
# Something went wrong in the plugin
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
|
||||||
self.__thread_pool.terminate() # Shutdown the pool
|
|
||||||
except Exception as e:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def __has_method(self, module, method_name, no_of_args = 1):
|
def __has_method(self, module, method_name, no_of_args = 1):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user