From 039b1d75ac47872d24135092745cfaf352a28285 Mon Sep 17 00:00:00 2001 From: todd <3578666+tgibson11@users.noreply.github.com> Date: Fri, 9 Jun 2023 06:16:25 -0700 Subject: [PATCH] Raise missingFile exception when unable to read FX config file --- sysbrokers/IB/config/ib_fx_config.py | 22 +++++----------------- sysbrokers/IB/ib_Fx_prices_data.py | 23 ++++++++++++++++++----- syscore/exceptions.py | 2 ++ 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/sysbrokers/IB/config/ib_fx_config.py b/sysbrokers/IB/config/ib_fx_config.py index 798481067e..036a120c26 100644 --- a/sysbrokers/IB/config/ib_fx_config.py +++ b/sysbrokers/IB/config/ib_fx_config.py @@ -2,8 +2,7 @@ import pandas as pd -from syscore.constants import missing_file -from syscore.exceptions import missingInstrument +from syscore.exceptions import missingFile from syscore.fileutils import resolve_path_and_filename_for_package from syslogging.logger import * @@ -18,22 +17,14 @@ def get_ib_config_from_file(log) -> pd.DataFrame: try: config_data = pd.read_csv(IB_CCY_CONFIG_FILE) - except BaseException: + except Exception as e: log.warn("Can't read file %s" % IB_CCY_CONFIG_FILE) - config_data = missing_file + raise missingFile from e return config_data -def config_info_for_code(config_data: pd.DataFrame, currency_code, log) -> ibFXConfig: - new_log = log.setup(**{CURRENCY_CODE_LOG_LABEL: currency_code}) - if config_data is missing_file: - new_log.warn( - "Can't get IB FX config for %s as config file missing" % currency_code - ) - - raise missingInstrument - +def config_info_for_code(config_data: pd.DataFrame, currency_code) -> ibFXConfig: ccy1 = config_data[config_data.CODE == currency_code].CCY1.values[0] ccy2 = config_data[config_data.CODE == currency_code].CCY2.values[0] invert = config_data[config_data.CODE == currency_code].INVERT.values[0] == "YES" @@ -43,10 +34,7 @@ def config_info_for_code(config_data: pd.DataFrame, currency_code, log) -> ibFXC return ib_config_for_code -def get_list_of_codes(log, config_data: pd.DataFrame) -> list: - if config_data is missing_file: - log.warn("Can't get list of fxcodes for IB as config file missing") - return [] +def get_list_of_codes(config_data: pd.DataFrame) -> list: list_of_codes = list(config_data.CODE) diff --git a/sysbrokers/IB/ib_Fx_prices_data.py b/sysbrokers/IB/ib_Fx_prices_data.py index 306636f1fd..1e420163a5 100644 --- a/sysbrokers/IB/ib_Fx_prices_data.py +++ b/sysbrokers/IB/ib_Fx_prices_data.py @@ -8,7 +8,7 @@ ibFXConfig, ) from sysbrokers.broker_fx_prices_data import brokerFxPricesData -from syscore.exceptions import missingData, missingInstrument +from syscore.exceptions import missingData, missingInstrument, missingFile from sysdata.data_blob import dataBlob from sysobjects.spot_fx_prices import fxPrices from syslogging.logger import * @@ -38,8 +38,13 @@ def ib_client(self) -> ibFxClient: return client def get_list_of_fxcodes(self) -> list: - config_data = self._get_ib_fx_config() - list_of_codes = get_list_of_codes(log=list, config_data=config_data) + try: + config_data = self._get_ib_fx_config() + except missingFile: + self.log.warn("Can't get list of fxcodes for IB as config file missing") + return [] + + list_of_codes = get_list_of_codes(config_data=config_data) return list_of_codes @@ -95,9 +100,17 @@ def _get_raw_fx_prices(self, ib_config_for_code: ibFXConfig) -> pd.Series: def _get_config_info_for_code(self, currency_code: str) -> ibFXConfig: - config_data = self._get_ib_fx_config() + try: + config_data = self._get_ib_fx_config() + except missingFile as e: + new_log = self.log.setup(**{CURRENCY_CODE_LOG_LABEL: currency_code}) + new_log.warn( + "Can't get IB FX config for %s as config file missing" % currency_code + ) + raise missingInstrument from e + ib_config_for_code = config_info_for_code( - config_data=config_data, log=self.log, currency_code=currency_code + config_data=config_data, currency_code=currency_code ) return ib_config_for_code diff --git a/syscore/exceptions.py b/syscore/exceptions.py index d86556660b..4657c37835 100644 --- a/syscore/exceptions.py +++ b/syscore/exceptions.py @@ -14,6 +14,8 @@ class missingContract(Exception): class missingData(Exception): pass +class missingFile(Exception): + pass class existingData(Exception): pass