Skip to content

Commit

Permalink
refactor callback exec to callback executor
Browse files Browse the repository at this point in the history
  • Loading branch information
k9ert committed Oct 26, 2022
1 parent 352cb8c commit f9cd800
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from ...services import callbacks, ExtensionException


class CallbackExecutor:
"""encapsulating the complexities of the extension callbacks"""

def __init__(self, services):
self.services = services

def execute_ext_callbacks(self, callback_id, *args, **kwargs):
"""will execute the callback function for each extension which has defined that method
the callback_id needs to be passed and specify why the callback has been called.
It needs to be one of the constants defined in cryptoadvance.specter.services.callbacks
"""
if callback_id not in dir(callbacks):
raise Exception(f"Non existing callback_id: {callback_id}")
# No debug statement here possible as this is called for every request and would flood the logs
# logger.debug(f"Executing callback {callback_id}")
return_values = {}
for ext in self.services.values():
if hasattr(ext, f"callback_{callback_id}"):
return_values[ext.id] = getattr(ext, f"callback_{callback_id}")(
*args, **kwargs
)
elif hasattr(ext, "callback"):
return_values[ext.id] = ext.callback(callback_id, *args, **kwargs)
# Filtering out all None return values
return_values = {k: v for k, v in return_values.items() if v is not None}
# logger.debug(f"return_values for callback {callback_id} {return_values}")
return return_values
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

from cryptoadvance.specter.config import ProductionConfig
from cryptoadvance.specter.device import Device
from cryptoadvance.specter.managers.service_manager.callback_executor import (
CallbackExecutor,
)
from cryptoadvance.specter.specter_error import SpecterError
from cryptoadvance.specter.user import User
from cryptoadvance.specter.util.reflection import get_template_static_folder
Expand Down Expand Up @@ -80,6 +83,7 @@ def __init__(self, specter, devstatus_threshold):
f"Service {clazz.__name__} not activated due to devstatus ( {self.devstatus_threshold} > {clazz.devstatus} )"
)
logger.info("----> finished service processing")
self.callback_executor = CallbackExecutor(self.services)
self.execute_ext_callbacks("afterExtensionManagerInit")

@classmethod
Expand Down Expand Up @@ -276,22 +280,9 @@ def execute_ext_callbacks(self, callback_id, *args, **kwargs):
the callback_id needs to be passed and specify why the callback has been called.
It needs to be one of the constants defined in cryptoadvance.specter.services.callbacks
"""
if callback_id not in dir(callbacks):
raise Exception(f"Non existing callback_id: {callback_id}")
# No debug statement here possible as this is called for every request and would flood the logs
# logger.debug(f"Executing callback {callback_id}")
return_values = {}
for ext in self.services.values():
if hasattr(ext, f"callback_{callback_id}"):
return_values[ext.id] = getattr(ext, f"callback_{callback_id}")(
*args, **kwargs
)
elif hasattr(ext, "callback"):
return_values[ext.id] = ext.callback(callback_id, *args, **kwargs)
# Filtering out all None return values
return_values = {k: v for k, v in return_values.items() if v is not None}
# logger.debug(f"return_values for callback {callback_id} {return_values}")
return return_values
return self.callback_executor.execute_ext_callbacks(
callback_id, *args, **kwargs
)

@property
def services(self) -> Dict[str, Service]:
Expand Down

0 comments on commit f9cd800

Please sign in to comment.