Skip to content

Commit

Permalink
refactor service_manager entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
k9ert committed Feb 8, 2023
1 parent fe229a9 commit 2390040
Show file tree
Hide file tree
Showing 32 changed files with 97 additions and 95 deletions.
6 changes: 3 additions & 3 deletions docs/extensions/callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Checkout the `cryptoadvance.specter.services.callbacks` file for all the specifi

Your Extension is also able to specify your own callback-methods. What does that mean? Let's assume you have specified a callback `my_callback`. So anywhere in your code, once or as many times you like, you can call:
```
app.specter.service_manager.execute_ext_callbacks(my_callback, "any", params, you="like")
app.specter.ext_manager.execute_ext_callbacks(my_callback, "any", params, you="like")
```
So the `service_manager` will then take care that all extensions are called which register that function. What exactly is called for each funtion and what's returned, depends on the `return_style` (see below). This is the same for all callbacks, no matter whether one which is called by core or by an extension.
So the `ext_manager` will then take care that all extensions are called which register that function. What exactly is called for each funtion and what's returned, depends on the `return_style` (see below). This is the same for all callbacks, no matter whether one which is called by core or by an extension.

Some important one is the `after_serverpy_init_app` which passes a `Scheduler` class which can be used to setup regular tasks. A list of currently implemented callback-methods along with their descriptions are available in [`/src/cryptoadvance/specter/services/callbacks.py`](https://github.com/cryptoadvance/specter-desktop/blob/master/src/cryptoadvance/specter/services/callbacks.py).

Expand All @@ -26,7 +26,7 @@ class MyExtension(Service):
callbacks = ["mynym.specterext.myextension.callbacks"]

def some_method(self):
returnvalues = app.specter.service_manager.execute_ext_callbacks(my_callback, "any", params, you="like")
returnvalues = app.specter.ext_manager.execute_ext_callbacks(my_callback, "any", params, you="like")
# in callbacks.py
class my_callback(Callback)
id = "my_callback"
Expand Down
4 changes: 2 additions & 2 deletions docs/extensions/frontend-aspects.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ In this case, this would add a tab called "myexttitle" and you're now supposed t
def myext_something():
return render_template(
"myext/some_settingspage.jinja",
ext_settingstabs = app.specter.service_manager.execute_ext_callbacks(
ext_settingstabs = app.specter.ext_manager.execute_ext_callbacks(
callbacks.add_settingstabs
)
)
Expand All @@ -105,7 +105,7 @@ def myext_mywalletdetails(wallet_alias):
wallet_alias=wallet_alias,
wallet=wallet,
specter=app.specter,
ext_wallettabs = app.specter.service_manager.execute_ext_callbacks(
ext_wallettabs = app.specter.ext_manager.execute_ext_callbacks(
callbacks.add_wallettabs
)
)
Expand Down
2 changes: 1 addition & 1 deletion pyinstaller/hooks/hook-cryptoadvance.specter.services.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from cryptoadvance.specter.managers.service_manager import ExtensionManager
from cryptoadvance.specter.managers import ExtensionManager

logger = logging.getLogger(__name__)

Expand Down
1 change: 1 addition & 0 deletions src/cryptoadvance/specter/managers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .extension_manager import ExtensionManager
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .extension_manager import ExtensionManager
10 changes: 5 additions & 5 deletions src/cryptoadvance/specter/managers/node_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ..node import Node, NonExistingNode
from ..internal_node import InternalNode
from ..services import callbacks
from ..managers.service_manager.service_manager import ExtensionManager
from ..managers import ExtensionManager
from ..util.bitcoind_setup_tasks import setup_bitcoind_thread

logger = logging.getLogger(__name__)
Expand All @@ -26,7 +26,7 @@ def __init__(
bitcoind_path="",
internal_bitcoind_version="",
data_folder="",
service_manager=None,
ext_manager=None,
):
self.nodes = {}
# Dict is sth. like: {'nigiri_regtest': <Node name=Nigiri regtest fullpath=...>, 'default': <Node name=Bitcoin Core fullpath=...>}
Expand All @@ -36,7 +36,7 @@ def __init__(
self.only_tor = only_tor
self.bitcoind_path = bitcoind_path
self.internal_bitcoind_version = internal_bitcoind_version
self.service_manager: ExtensionManager = service_manager
self.ext_manager: ExtensionManager = ext_manager
self.load_from_disk(data_folder)
internal_nodes = [
node for node in self.nodes.values() if not node.external_node
Expand Down Expand Up @@ -66,8 +66,8 @@ def load_from_disk(self, data_folder=None):
if (
node.__class__.__module__.split(".")[1] == "specterext"
): # e.g. cryptoadvance.specterext.spectrum
if self.service_manager:
if not self.service_manager.is_class_from_loaded_extension(
if self.ext_manager:
if not self.ext_manager.is_class_from_loaded_extension(
node.__class__
):
logger.warning(
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
from .service_manager import *
""" This package/module is deprecated and only existing for backwards compatibility
please migrate to the extension_manager
"""

from ..extension_manager import extension_manager as service_manager
2 changes: 1 addition & 1 deletion src/cryptoadvance/specter/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def storage_callback(mode="write", path=None):
# First, call extensions which want to get informed.
# This is working synchronously!
try:
app.specter.service_manager.execute_ext_callbacks(
app.specter.ext_manager.execute_ext_callbacks(
specter_persistence_callback, path=path, mode=mode
)
except AttributeError as e:
Expand Down
20 changes: 10 additions & 10 deletions src/cryptoadvance/specter/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from cryptoadvance.specter.hwi_rpc import HWIBridge
from cryptoadvance.specter.liquid.rpc import LiquidRPC
from cryptoadvance.specter.managers.service_manager import ExtensionManager
from cryptoadvance.specter.managers import ExtensionManager
from cryptoadvance.specter.rpc import BitcoinRPC
from cryptoadvance.specter.services import callbacks
from cryptoadvance.specter.util.reflection import get_template_static_folder
Expand Down Expand Up @@ -157,17 +157,17 @@ def init_app(app: SpecterFlask, hwibridge=False, specter=None):
# ExtensionManager will instantiate and register blueprints for extensions
# It's an attribute to the specter but specter is not aware of it.
# However some managers are aware of it and so we need to split
# instantiation from initializing and in between attach the service_manager
specter.service_manager = ExtensionManager(
# instantiation from initializing and in between attach the ext_manager
specter.ext_manager = ExtensionManager(
specter=specter, devstatus_threshold=app.config["SERVICES_DEVSTATUS_THRESHOLD"]
)

def service_manager_cleanup_on_exit(signum, frame):
return specter.service_manager.execute_ext_callbacks(
def ext_manager_cleanup_on_exit(signum, frame):
return specter.ext_manager.execute_ext_callbacks(
callbacks.cleanup_on_exit, signum, frame
)

specter.call_functions_at_cleanup_on_exit.append(service_manager_cleanup_on_exit)
specter.call_functions_at_cleanup_on_exit.append(ext_manager_cleanup_on_exit)

specter.initialize()

Expand Down Expand Up @@ -198,7 +198,7 @@ def login(id, password: str = None):
app.specter = specter
# Executing callback specter_added_to_flask_app
app.logger.info("Executing callback specter_added_to_flask_app ...")
specter.service_manager.execute_ext_callbacks(specter_added_to_flask_app)
specter.ext_manager.execute_ext_callbacks(specter_added_to_flask_app)
if specter.config["auth"].get("method") == "none":
app.logger.info("Login disabled")
app.config["LOGIN_DISABLED"] = True
Expand Down Expand Up @@ -282,7 +282,7 @@ def set_language_code():
def every5seconds():
ctx = app.app_context()
ctx.push()
app.specter.service_manager.execute_ext_callbacks(callbacks.every5seconds)
app.specter.ext_manager.execute_ext_callbacks(callbacks.every5seconds)
ctx.pop()

# initialize scheduler
Expand All @@ -292,9 +292,9 @@ def every5seconds():

scheduler.init_app(app)
scheduler.start()
specter.service_manager.add_required_services_to_users(specter.user_manager.users)
specter.ext_manager.add_required_services_to_users(specter.user_manager.users)
logger.info("----> starting service callback_after_serverpy_init_app ")
specter.service_manager.execute_ext_callbacks(
specter.ext_manager.execute_ext_callbacks(
after_serverpy_init_app, scheduler=scheduler
)
return app
Expand Down
4 changes: 2 additions & 2 deletions src/cryptoadvance/specter/server_endpoints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
def flash(*args, **kwargs):
"""An indirection in order to potentially handle a flash differently
This function could be placed in util but as it might
use the service_manager, we place it here for now.
use the ext_manager, we place it here for now.
"""

return_values = app.specter.service_manager.execute_ext_callbacks(
return_values = app.specter.ext_manager.execute_ext_callbacks(
callbacks.flash, *args, **kwargs
)

Expand Down
4 changes: 2 additions & 2 deletions src/cryptoadvance/specter/server_endpoints/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def register():
plaintext_password=password,
config=config,
)
app.specter.service_manager.add_required_services_to_users([user])
app.specter.ext_manager.add_required_services_to_users([user])

flash(
_(
Expand Down Expand Up @@ -242,7 +242,7 @@ def redirect_login(request):

for service_id in app.specter.user_manager.get_user().services:
try:
service_cls = app.specter.service_manager.get_service(service_id)
service_cls = app.specter.ext_manager.get_service(service_id)
service_cls.on_user_login()
except ExtensionException as ee:
if not str(ee).startswith("No such plugin"):
Expand Down
4 changes: 2 additions & 2 deletions src/cryptoadvance/specter/server_endpoints/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ def slow_request_detection_start():


@app.before_request
def execute_service_manager_hook():
def execute_ext_manager_hook():
"""inform extensions about the request"""
app.specter.service_manager.execute_ext_callbacks(flask_before_request, request)
app.specter.ext_manager.execute_ext_callbacks(flask_before_request, request)


@app.after_request
Expand Down
18 changes: 9 additions & 9 deletions src/cryptoadvance/specter/server_endpoints/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def inject_common_stuff():
"""Can be used in all jinja2 templates of this Blueprint
Injects the additional settings_tabs via extentions
"""
ext_settingstabs = app.specter.service_manager.execute_ext_callbacks(
ext_settingstabs = app.specter.ext_manager.execute_ext_callbacks(
callbacks.add_settingstabs
)
return dict(ext_settingstabs=ext_settingstabs)
Expand All @@ -70,7 +70,7 @@ def general():
fee_estimator_custom_url = app.specter.config.get("fee_estimator_custom_url", "")
loglevel = get_loglevel(app)
unit = app.specter.unit
services = app.specter.service_manager.services
services = app.specter.ext_manager.services
if request.method == "POST":
action = request.form["action"]

Expand Down Expand Up @@ -131,7 +131,7 @@ def general():
validate_bool=validate_merkleproof_bool
)
if current_user.is_admin:
app.specter.service_manager.set_active_services(active_services)
app.specter.ext_manager.set_active_services(active_services)
app.specter.update_fee_estimator(
fee_estimator=fee_estimator,
custom_url=fee_estimator_custom_url,
Expand Down Expand Up @@ -450,7 +450,7 @@ def auth():
specter=app.specter,
current_version=current_version,
rand=rand,
has_service_encrypted_storage=app.specter.service_manager.user_has_encrypted_storage(
has_service_encrypted_storage=app.specter.ext_manager.user_has_encrypted_storage(
current_user
),
)
Expand All @@ -473,7 +473,7 @@ def auth():
specter=app.specter,
current_version=current_version,
rand=rand,
has_service_encrypted_storage=app.specter.service_manager.user_has_encrypted_storage(
has_service_encrypted_storage=app.specter.ext_manager.user_has_encrypted_storage(
current_user
),
)
Expand Down Expand Up @@ -502,7 +502,7 @@ def auth():
specter=app.specter,
current_version=current_version,
rand=rand,
has_service_encrypted_storage=app.specter.service_manager.user_has_encrypted_storage(
has_service_encrypted_storage=app.specter.ext_manager.user_has_encrypted_storage(
current_user
),
)
Expand Down Expand Up @@ -532,7 +532,7 @@ def auth():
specter=app.specter,
current_version=current_version,
rand=rand,
has_service_encrypted_storage=app.specter.service_manager.user_has_encrypted_storage(
has_service_encrypted_storage=app.specter.ext_manager.user_has_encrypted_storage(
current_user
),
)
Expand Down Expand Up @@ -560,7 +560,7 @@ def auth():

# if there is no password, we have to delete the previously encrypted data from services
for user in app.specter.user_manager.users:
app.specter.service_manager.delete_services_with_encrypted_storage(
app.specter.ext_manager.delete_services_with_encrypted_storage(
user
)

Expand Down Expand Up @@ -621,7 +621,7 @@ def auth():
specter=app.specter,
current_version=current_version,
rand=rand,
has_service_encrypted_storage=app.specter.service_manager.user_has_encrypted_storage(
has_service_encrypted_storage=app.specter.ext_manager.user_has_encrypted_storage(
current_user
),
next=request.args.get("next", ""),
Expand Down
10 changes: 5 additions & 5 deletions src/cryptoadvance/specter/server_endpoints/wallets/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def inject_common_stuff():
"""Can be used in all jinja2 templates of this Blueprint
Injects the additional wallet_tabs via extentions
"""
ext_wallettabs = app.specter.service_manager.execute_ext_callbacks(
ext_wallettabs = app.specter.ext_manager.execute_ext_callbacks(
callbacks.add_wallettabs
)
return dict(ext_wallettabs=ext_wallettabs)
Expand All @@ -74,7 +74,7 @@ def wallets_overview():
# that's why we need so many lines for just expressing:
# "Here is a ViewModel, adjust it if you want"
# We need to change that method to enable "middleware"
wallets_overview_vm = app.specter.service_manager.execute_ext_callbacks(
wallets_overview_vm = app.specter.ext_manager.execute_ext_callbacks(
adjust_view_model, WalletsOverviewVm()
)
if wallets_overview_vm.wallets_overview_redirect != None:
Expand All @@ -90,7 +90,7 @@ def wallets_overview():
"wallet/overview/wallets_overview.jinja",
specter=app.specter,
rand=rand,
services=app.specter.service_manager.services,
services=app.specter.ext_manager.services,
wallets_overview_vm=wallets_overview_vm,
)

Expand Down Expand Up @@ -434,7 +434,7 @@ def history(wallet_alias):
tx_list_type=tx_list_type,
specter=app.specter,
rand=rand,
services=app.specter.service_manager.services,
services=app.specter.ext_manager.services,
)


Expand Down Expand Up @@ -775,7 +775,7 @@ def addresses(wallet_alias):
wallet=wallet,
specter=app.specter,
rand=rand,
services=app.specter.service_manager.services,
services=app.specter.ext_manager.services,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def about():
# that's why we need so many lines for just expressing:
# "Here is a ViewModel, adjust it if you want"
# We need to change that method to enable "middleware"
welcome_vm = app.specter.service_manager.execute_ext_callbacks(
welcome_vm = app.specter.ext_manager.execute_ext_callbacks(
adjust_view_model, WelcomeVm()
)
if welcome_vm.about_redirect != None:
Expand Down
10 changes: 5 additions & 5 deletions src/cryptoadvance/specter/services/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
)

# All blueprint from Services are no longer loaded statically but dynamically when the service-class in initialized
# check cryptoadvance.specter.services.service_manager.Service for doing that and
# check cryptoadvance.specter.managers.extension_manager.ExtensionManager for doing that and
# check cryptoadvance.specter.services/**/manifest for instances of Service-classes and
# check cryptoadvance.specter.services.service_manager.ExtensionManager.services for initialisation of ServiceClasses
# check cryptoadvance.specter.services.extension_manager.ExtensionManager.services for initialisation of ServiceClasses


def user_secret_decrypted_required(func):
Expand Down Expand Up @@ -51,7 +51,7 @@ def choose():
"services/choose.jinja",
is_login_disabled=app.config["LOGIN_DISABLED"],
specter=app.specter,
services=app.specter.service_manager.services_sorted,
services=app.specter.ext_manager.services_sorted,
)


Expand All @@ -66,7 +66,7 @@ def associate_addr(wallet_alias, address):
if request.method == "POST":
service_id = request.form["service_id"]
wallet = app.specter.wallet_manager.get_by_alias(wallet_alias)
service_cls = app.specter.service_manager.get_service(service_id)
service_cls = app.specter.ext_manager.get_service(service_id)
service_cls.reserve_address(wallet=wallet, address=address)
return redirect(
url_for("wallets_endpoint.addresses", wallet_alias=wallet_alias)
Expand All @@ -78,7 +78,7 @@ def associate_addr(wallet_alias, address):
services = []
for service_id in current_user.services:
try:
services.append(app.specter.service_manager.get_service(service_id))
services.append(app.specter.ext_manager.get_service(service_id))
except ExtensionException:
pass

Expand Down
Loading

0 comments on commit 2390040

Please sign in to comment.