From e5ba8f371fcf23e17ebefac15416cbdac160b471 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Fri, 2 Aug 2024 07:44:21 +0200 Subject: [PATCH] nordnet_client is now gated behind soft import --- fin_depo/__init__.py | 25 ++++++++++++++++++------- fin_depo/defi_kucoin.py | 14 +++++++++++--- fin_depo/investbank_nordnet.py | 6 +++++- test/test_static.py | 3 ++- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/fin_depo/__init__.py b/fin_depo/__init__.py index 0cc2d24..01427e6 100644 --- a/fin_depo/__init__.py +++ b/fin_depo/__init__.py @@ -55,20 +55,31 @@ __all__ = [ 'static', ] +import importlib import logging from . import ( data, - defi_kraken, - defi_kucoin, - defi_partisia_blockchain, static, ) from ._version import __version__ logger = logging.getLogger(__name__) -try: - from . import investbank_nordnet -except ImportError: - logger.exception('Could not import %s. Are module requirements installed?', 'fin_depo.investbank_nordnet') + +def load_backend(name: str) -> object | None: + try: + return importlib.import_module(f'{__name__}.{name}') + except Exception: + logger.exception( + 'Backend %s could not be imported? Are all module dependencies installed?', + name, + ) + return None + +# Import modules + +defi_kraken = load_backend('defi_kraken') +defi_kucoin = load_backend('defi_kucoin') +investbank_nordnet = load_backend('investbank_nordnet') +defi_partisia_blockchain = load_backend('defi_partisia_blockchain') diff --git a/fin_depo/defi_kucoin.py b/fin_depo/defi_kucoin.py index 1e1f915..864917f 100644 --- a/fin_depo/defi_kucoin.py +++ b/fin_depo/defi_kucoin.py @@ -139,7 +139,10 @@ class KucoinDepoFetcher(DepoFetcher): return self._get_order_details(response['orderId'], input_asset, output_asset) def _get_order_details( - self, order_id: str, input_asset: fin_defs.Asset, output_asset: fin_defs.Asset, + self, + order_id: str, + input_asset: fin_defs.Asset, + output_asset: fin_defs.Asset, ) -> TradeOrderDetails: """Determine the order details for the order with the given id. @@ -164,14 +167,19 @@ class KucoinDepoFetcher(DepoFetcher): fee_asset=fin_defs.WELL_KNOWN_SYMBOLS[order_details['feeCurrency']], fee_amount=Decimal(order_details['fee']), executed_time=datetime.datetime.fromtimestamp( - order_details['createdAt'] / 1000, tz=datetime.UTC, + order_details['createdAt'] / 1000, + tz=datetime.UTC, ), order_id=order_id, raw_order_details=order_details, ) def _get_order_with_retries( - self, order_id: str, *, num_retries: int, sleep_between_tries: float = 1.0, + self, + order_id: str, + *, + num_retries: int, + sleep_between_tries: float = 1.0, ) -> dict: """Get the order details from KuCoin backend. diff --git a/fin_depo/investbank_nordnet.py b/fin_depo/investbank_nordnet.py index 52b4c13..222519f 100644 --- a/fin_depo/investbank_nordnet.py +++ b/fin_depo/investbank_nordnet.py @@ -30,7 +30,11 @@ def asset_from_instrument(instrument: Instrument) -> fin_defs.Asset | None: return None symbol = instrument.symbol exchange_id = instrument.tradables[0].mic - return fin_defs.Stock(symbol, fin_defs.EXCHANGES_BY_IDS[exchange_id], nordnet_id = instrument.instrument_id) + return fin_defs.Stock( + symbol, + fin_defs.EXCHANGES_BY_IDS[exchange_id], + nordnet_id=instrument.instrument_id, + ) EMPTY_DICT: Mapping[str, str | int] = frozendict() diff --git a/test/test_static.py b/test/test_static.py index 8fc0f0c..06abdef 100644 --- a/test/test_static.py +++ b/test/test_static.py @@ -7,7 +7,8 @@ import fin_depo def test_get_depo(): fetcher = fin_depo.static.StaticDepoFetcher( - 'Test', {fin_defs.BTC: Decimal(1000), fin_defs.USD: Decimal(2000)}, + 'Test', + {fin_defs.BTC: Decimal(1000), fin_defs.USD: Decimal(2000)}, ) depo = fetcher.get_depo()