Skip to content

Commit 65bb7d2

Browse files
k9ertmoneymanolis
andauthored
Chore: Refactor Swan Extension to cryptoadvance.specterext (#1955)
* Refactor Swan service, delete bitcoinreserve fix tests for services move to specterext and complete * complete refactoring of swan extension * added a test * feedback manolis Co-authored-by: Manolis Mandrapilias <[email protected]>
1 parent 55e2a5f commit 65bb7d2

File tree

29 files changed

+113
-126
lines changed

29 files changed

+113
-126
lines changed

src/cryptoadvance/specter/config.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,8 @@ class BaseConfig(object):
173173

174174
# List of extensions (services) to potentially load
175175
EXTENSION_LIST = [
176-
"cryptoadvance.specter.services.swan.service",
176+
"cryptoadvance.specterext.swan.service",
177177
"cryptoadvance.specterext.liquidissuer.service",
178-
"cryptoadvance.specter.services.bitcoinreserve.service",
179178
"cryptoadvance.specterext.devhelp.service",
180179
"cryptoadvance.specterext.exfund.service",
181180
"cryptoadvance.specterext.faucet.service",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .service_manager import *

src/cryptoadvance/specter/managers/service_manager.py renamed to src/cryptoadvance/specter/managers/service_manager/service_manager.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
from flask import url_for
1818
from flask.blueprints import Blueprint
1919

20-
from ..services.service import Service
21-
from ..services import callbacks, ExtensionException
22-
from ..util.reflection import (
20+
from ...services.service import Service
21+
from ...services import callbacks, ExtensionException
22+
from ...util.reflection import (
2323
_get_module_from_class,
2424
get_classlist_of_type_clazz_from_modulelist,
2525
get_package_dir_for_subclasses_of,
2626
get_subclasses_for_clazz,
2727
get_subclasses_for_clazz_in_cwd,
2828
)
29-
from ..util.reflection_fs import search_dirs_in_path
29+
from ...util.reflection_fs import search_dirs_in_path
3030

3131
logger = logging.getLogger(__name__)
3232

@@ -44,7 +44,7 @@ def __init__(self, specter, devstatus_threshold):
4444
logger.info("----> starting service discovery Static")
4545
# How do we discover services? Two configs are relevant:
4646
# * SERVICES_LOAD_FROM_CWD (boolean, CWD is current working directory)
47-
# * EXTENSION_LIST (array of Fully Qualified module strings like ["cryptoadvance.specter.services.swan.service"])
47+
# * EXTENSION_LIST (array of Fully Qualified module strings like ["cryptoadvance.specterext.swan.service"])
4848
# Ensuring security (especially for the CWD) is NOT done here but
4949
# in the corresponding (Production)Config
5050
logger.debug(f"EXTENSION_LIST = {app.config.get('EXTENSION_LIST')}")

src/cryptoadvance/specter/services/bitcoinreserve/controller.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/cryptoadvance/specter/services/bitcoinreserve/service.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/cryptoadvance/specter/services/bitcoinreserve/static/bitcoinreserve/img/bitcoinreserve_icon.svg

Lines changed: 0 additions & 12 deletions
This file was deleted.
Binary file not shown.

src/cryptoadvance/specter/services/swan/__init__.py

Whitespace-only changes.

src/cryptoadvance/specter/util/reflection.py

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ def get_package_dir_for_subclasses_of(clazz):
6262
).resolve()
6363
)
6464
elif clazz.__name__ == "Service":
65+
# here, we'd like to know the Path of the namespace module cryptoadvance.specterext
66+
# but that fails because you cannot import namespace-modules.
67+
# So we take a module that we know exists and take its parent.
6568
return str(
6669
Path(
67-
import_module("cryptoadvance.specter.services").__file__
68-
).parent.resolve()
70+
import_module("cryptoadvance.specterext.devhelp").__file__
71+
).parent.parent.resolve()
6972
)
7073
# This is mainly for testing purposes for now
7174
elif clazz.__name__ == "Device":
@@ -176,53 +179,44 @@ def get_subclasses_for_clazz(clazz, package_dirs: List[str] = None):
176179
f"Iterating on importer={importer} , module_name={module_name} is_pkg={is_pkg}"
177180
)
178181
if clazz.__name__ == "Service":
182+
# Ignore the stuff lying around in cryptoadvance/specter/services
183+
if importer.path.endswith(
184+
os.path.sep.join(["cryptoadvance", "specter", "services"])
185+
):
186+
continue
179187
try:
180-
181-
module = import_module(
182-
f"cryptoadvance.specter.services.{module_name}.service"
183-
)
184-
logger.debug(
185-
f" Imported cryptoadvance.specter.services.{module_name}.service"
186-
)
187-
except ModuleNotFoundError:
188-
# Ignore the stuff lying around in cryptoadvance/specter/services
189-
if importer.path.endswith(
190-
os.path.sep.join(["cryptoadvance", "specter", "services"])
191-
):
192-
continue
188+
module = import_module(f"{module_name}.service")
189+
logger.debug(f" Imported {module_name}.service")
190+
except ModuleNotFoundError as e:
193191
try:
194-
module = import_module(f"{module_name}.service")
195-
logger.debug(f" Imported {module_name}.service")
192+
# Another style is orgname.specterext.extensionid, for that we have to guess the orgname:
193+
orgname = str(importer).split(os.path.sep)[-2]
194+
logger.debug(f"guessing orgname: {orgname}")
195+
module = import_module(
196+
f"{orgname}.specterext.{module_name}.service"
197+
)
198+
logger.debug(
199+
f" Imported {orgname}.specterext.{module_name}.service"
200+
)
196201
except ModuleNotFoundError as e:
197-
try:
198-
# Another style is orgname.specterext.extensionid, for that we have to guess the orgname:
199-
orgname = str(importer).split(os.path.sep)[-2]
200-
logger.debug(f"guessing orgname: {orgname}")
201-
module = import_module(
202-
f"{orgname}.specterext.{module_name}.service"
203-
)
204-
logger.debug(
205-
f" Imported {orgname}.specterext.{module_name}.service"
202+
if module_name in str(e.name) or orgname in str(e.name):
203+
raise Exception(
204+
f"""
205+
While iterating over {importer} for module {module_name},
206+
a Service implementation could not be found in this places:
207+
* cryptoadvance.specter.services.{module_name}.service
208+
* {module_name}.service
209+
* {orgname}.specterext.{module_name}.service
210+
Maybe you did forget to do this:
211+
$ pip3 install -e .
212+
213+
OR The Module has been found, but had issues finding Modules itself
214+
215+
{e}
216+
"""
206217
)
207-
except ModuleNotFoundError as e:
208-
if module_name in str(e.name) or orgname in str(e.name):
209-
raise Exception(
210-
f"""
211-
While iterating over {importer} for module {module_name},
212-
a Service implementation could not be found in this places:
213-
* cryptoadvance.specter.services.{module_name}.service
214-
* {module_name}.service
215-
* {orgname}.specterext.{module_name}.service
216-
Maybe you did forget to do this:
217-
$ pip3 install -e .
218-
219-
OR The Module has been found, but had issues finding Modules itself
220-
221-
{e}
222-
"""
223-
)
224-
else:
225-
raise e
218+
else:
219+
raise e
226220
elif clazz.__name__ == "SpecterMigration":
227221
module = import_module(
228222
f"cryptoadvance.specter.util.migrations.{module_name}"

0 commit comments

Comments
 (0)