mirror of
https://github.com/Ichinga-Samuel/aiomql.git
synced 2026-08-23 16:58:07 +00:00
Add set_root instance method to the Config class.
Define __setattr__ magic method to ensure instance attributes are also set as class attributes.
This commit is contained in:
@@ -68,11 +68,11 @@ class Config:
|
|||||||
cls._instance._backtest_engine = None
|
cls._instance._backtest_engine = None
|
||||||
cls._instance.bot = None
|
cls._instance.bot = None
|
||||||
cls._instance.backtest_controller = None
|
cls._instance.backtest_controller = None
|
||||||
# cls._instance.load_config(**kwargs)
|
|
||||||
return cls._instance
|
return cls._instance
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
"""Initialize the Config object. The root directory can be set here or in the load_config method."""
|
"""Initialize the Config object. The root directory can be set here or in the load_config method."""
|
||||||
|
|
||||||
root = kwargs.pop("root", None)
|
root = kwargs.pop("root", None)
|
||||||
config_file = kwargs.pop("config_file", None)
|
config_file = kwargs.pop("config_file", None)
|
||||||
if self.root is None or root is not None or config_file is not None:
|
if self.root is None or root is not None or config_file is not None:
|
||||||
@@ -80,6 +80,11 @@ class Config:
|
|||||||
else:
|
else:
|
||||||
self.set_attributes(**kwargs)
|
self.set_attributes(**kwargs)
|
||||||
|
|
||||||
|
def __setattr__(self, name, value):
|
||||||
|
"""Set all instance attributes class as class attributes."""
|
||||||
|
super().__setattr__(name, value)
|
||||||
|
setattr(self.__class__, name, value)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def backtest_engine(self):
|
def backtest_engine(self):
|
||||||
"""Returns the backtest engine object"""
|
"""Returns the backtest engine object"""
|
||||||
@@ -126,6 +131,22 @@ class Config:
|
|||||||
logger.debug(f"Error finding config file: {err}")
|
logger.debug(f"Error finding config file: {err}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def set_root(self, root: str | Path = None):
|
||||||
|
try:
|
||||||
|
if root is not None:
|
||||||
|
root = Path(root).resolve()
|
||||||
|
root.mkdir(parents=True, exist_ok=True) if not root.exists() else ...
|
||||||
|
self.root = root
|
||||||
|
|
||||||
|
elif self.root is None:
|
||||||
|
self.root = Path.cwd()
|
||||||
|
|
||||||
|
elif not isinstance(self.root, Path):
|
||||||
|
self.root = Path(self.root).resolve()
|
||||||
|
except Exception as err:
|
||||||
|
logger.warning("Error finding root path: %s. The current working directory will be used.", err)
|
||||||
|
self.root = Path.cwd()
|
||||||
|
|
||||||
def load_config(self, *, config_file: str | Path = None, filename: str = None, root: str | Path = None, **kwargs) -> Self:
|
def load_config(self, *, config_file: str | Path = None, filename: str = None, root: str | Path = None, **kwargs) -> Self:
|
||||||
"""Load configuration settings from a file and reset the config object.
|
"""Load configuration settings from a file and reset the config object.
|
||||||
|
|
||||||
@@ -135,12 +156,7 @@ class Config:
|
|||||||
root (str): The root directory of the project.
|
root (str): The root directory of the project.
|
||||||
**kwargs: Additional keyword arguments to be set on the config object.
|
**kwargs: Additional keyword arguments to be set on the config object.
|
||||||
"""
|
"""
|
||||||
if root is not None:
|
self.set_root(root=root)
|
||||||
root = Path(root).resolve()
|
|
||||||
root.mkdir(parents=True, exist_ok=True) if not root.exists() else ...
|
|
||||||
self.root = root
|
|
||||||
else:
|
|
||||||
self.root = self.root if isinstance(self.root, Path) else Path.cwd()
|
|
||||||
|
|
||||||
if config_file is not None:
|
if config_file is not None:
|
||||||
config_file = Path(config_file).resolve()
|
config_file = Path(config_file).resolve()
|
||||||
@@ -171,7 +187,6 @@ class Config:
|
|||||||
except Exception as err:
|
except Exception as err:
|
||||||
logger.debug(f"Error setting path: {err}")
|
logger.debug(f"Error setting path: {err}")
|
||||||
self.path = ""
|
self.path = ""
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
Reference in New Issue
Block a user