Skip to content

Commit

Permalink
fix(config): separate global configurations into different classes, c…
Browse files Browse the repository at this point in the history
…hange environment variables to project arguments, remove unnecessary comments and return info
  • Loading branch information
entelecheia committed Aug 7, 2023
1 parent 72fa2ae commit 495210a
Showing 1 changed file with 78 additions and 62 deletions.
140 changes: 78 additions & 62 deletions src/hyfi/main/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
field_validator,
)
from hyfi.core import global_hyfi
from hyfi.dotenv import DotEnvConfig
from hyfi.project import ProjectConfig
from hyfi.utils import UTILs

Expand All @@ -27,8 +26,11 @@
__default_workspace_name__ = "workspace"


class GlobalConfig(BaseModel, UTILs):
"""HyFI global config class. This class is used to store the configuration"""
class HyFIConfig(BaseModel, UTILs):
"""HyFI root config class. This class is used to store the configuration"""

_config_name_: str = "config"
_config_group_: str = "/"

debug_mode: bool = False
noop: bool = False
Expand All @@ -39,16 +41,14 @@ class GlobalConfig(BaseModel, UTILs):

hydra: Optional[ConfigType] = None

about: Optional[AboutConfig] = None
about: Optional[ConfigType] = None
copier: Optional[ConfigType] = None
project: Optional[ProjectConfig] = None
project: Optional[ConfigType] = None
pipeline: Optional[ConfigType] = None
task: Optional[ConfigType] = None
workflow: Optional[ConfigType] = None
tasks: Optional[List[str]] = None

_version_: str = PrivateAttr(global_hyfi.version)
_initilized_: bool = PrivateAttr(False)
workflows: Optional[List[str]] = None

model_config = ConfigDict(
arbitrary_types_allowed=True,
Expand All @@ -68,7 +68,32 @@ def _validate_logging_level(cls, v, info: FieldValidationInfo):
logger.setLevel(v)
return v

def init_project(

class GlobalConfig(UTILs):
"""HyFI global config class. This class is used to store the global configuration"""

__config__: Optional[HyFIConfig] = None

_about_: Optional[AboutConfig] = None
_project_: Optional[ProjectConfig] = None
_version_: str = PrivateAttr(global_hyfi.version)

# _initilized_: bool = PrivateAttr(False)
def __init__(self, **config_kwargs):
if config_kwargs:
self.__config__ = HyFIConfig(**config_kwargs)

@property
def about(self) -> AboutConfig:
if self._about_ is None:
self._about_ = AboutConfig()
return self._about_

@property
def project(self) -> ProjectConfig:
return self._project_

def inititialize(
self,
project_name: Optional[str] = None,
project_description: Optional[str] = None,
Expand All @@ -77,7 +102,7 @@ def init_project(
global_hyfi_root: Optional[str] = None,
global_workspace_name: Optional[str] = None,
num_workers: Optional[int] = None,
log_level: Optional[str] = None,
logging_level: Optional[str] = None,
reinit: bool = True,
autotime: bool = True,
retina: bool = True,
Expand All @@ -100,52 +125,59 @@ def init_project(
retina: Whether to use retina or not.
verbose: Enables or disables logging
"""
if self._initilized_ and not reinit:
return
if self.about is None:
self.about = AboutConfig()
# Set the log level to the given log level.
# envs = DotEnvConfig(HYFI_VERBOSE=verbose) # type: ignore
if logging_level:
GlobalConfig.setLogger(logging_level)
logger.setLevel(logging_level)

# Load the extentions for the autotime extension.
if autotime:
GlobalConfig.load_extentions(exts=["autotime"])
# Set the retina matplotlib formats.
if retina:
GlobalConfig.set_matplotlib_formats("retina")
# ENVs.load_dotenv()
envs = DotEnvConfig(HYFI_VERBOSE=verbose) # type: ignore
if self.project and not reinit:
return
# Set the project name environment variable HYFI_PROJECT_NAME environment variable if project_name is not set.
if project_name:
envs.HYFI_PROJECT_NAME = GlobalConfig.expand_posix_vars(project_name)
# envs.HYFI_PROJECT_NAME = GlobalConfig.expand_posix_vars(project_name)
project_kwargs["project_name"] = project_name
# Set the project description environment variable HYFI_PROJECT_DESC environment variable.
if project_description:
envs.HYFI_PROJECT_DESC = GlobalConfig.expand_posix_vars(project_description)
# envs.HYFI_PROJECT_DESC = GlobalConfig.expand_posix_vars(project_description)
project_kwargs["project_description"] = project_description
# Set environment variables HYFI_PROJECT_ROOT to the project root if project_root is set to true.
if project_root:
envs.HYFI_PROJECT_ROOT = GlobalConfig.expand_posix_vars(project_root)
# envs.HYFI_PROJECT_ROOT = GlobalConfig.expand_posix_vars(project_root)
project_kwargs["project_root"] = project_root
# Set the project workspace name environment variable HYFI_PROJECT_WORKSPACE_NAME environment variable if project_workspace_name is set to the project workspace name.
if project_workspace_name:
envs.HYFI_PROJECT_WORKSPACE_NAME = GlobalConfig.expand_posix_vars(
project_workspace_name
)
# envs.HYFI_PROJECT_WORKSPACE_NAME = GlobalConfig.expand_posix_vars(
# project_workspace_name
# )
project_kwargs["project_workspace_name"] = project_workspace_name
# Expand the hyfi_root environment variable.
if global_hyfi_root:
envs.HYFI_GLOBAL_ROOT = GlobalConfig.expand_posix_vars(global_hyfi_root)
# envs.HYFI_GLOBAL_ROOT = GlobalConfig.expand_posix_vars(global_hyfi_root)
project_kwargs["global_hyfi_root"] = global_hyfi_root
# Set the global workspace name environment variable HYFI_GLOBAL_WORKSPACE_NAME environment variable.
if global_workspace_name:
envs.HYFI_GLOBAL_WORKSPACE_NAME = GlobalConfig.expand_posix_vars(
global_workspace_name
)
# envs.HYFI_GLOBAL_WORKSPACE_NAME = GlobalConfig.expand_posix_vars(
# global_workspace_name
# )
project_kwargs["global_workspace_name"] = global_workspace_name
# Set the number of workers to use.
if num_workers:
envs.HYFI_NUM_WORKERS = num_workers
# Set the log level to the given log level.
if log_level:
envs.HYFI_LOG_LEVEL = log_level
GlobalConfig.setLogger(log_level)
logger.setLevel(log_level)
# Load the extentions for the autotime extension.
if autotime:
GlobalConfig.load_extentions(exts=["autotime"])
# Set the retina matplotlib formats.
if retina:
GlobalConfig.set_matplotlib_formats("retina")
# envs.HYFI_NUM_WORKERS = num_workers
project_kwargs["num_workers"] = num_workers
# Set the verbose environment variable.
project_kwargs["verbose"] = verbose

self.project = ProjectConfig(**project_kwargs)
logger.info("HyFi project [%s] initialized", self.project.project_name)
self._initilized_ = True
self._project_ = ProjectConfig(**project_kwargs)
logger.info("HyFi project [%s] initialized", self._project_.project_name)
# self._initilized_ = True

def terminate(self) -> None:
"""
Expand All @@ -155,64 +187,48 @@ def terminate(self) -> None:
True if successful False
"""
# If the module is not initialized yet.
if not self._initilized_:
return
# if not self.project:
# return
# Stop the backend if the joblib is running.
if self.project and self.project.joblib:
self.project.joblib.stop_backend()
self._initilized_ = False
# self._initilized_ = False

def __repr__(self):
"""
Returns a string representation of HyFIConfig.
Returns:
The string representation of HyFI
Returns a string representation of GlobalConfig.
"""
return f"HyFIConfig(project={self.project})"
return f"GlobalConfig(project={self.project})"

def __str__(self):
"""
Returns a string representation of the object.
Returns:
The string representation of the
"""
return self.__repr__()

@property
def app_version(self):
"""
Get the version of the application.
Returns:
The version of the application
"""
return global_hyfi.version

@property
def app_name(self):
"""
Get the name of the application.
Returns:
The name of the application
"""
return self.about.name if self.about else global_hyfi.hyfi_name

@property
def package_name(self):
"""
Get the name of the package.
Returns:
The name of the package
"""
return global_hyfi.package_name

def print_about(self, **kwargs):
self.about = AboutConfig(**kwargs)
self._about_ = AboutConfig(**kwargs)
pkg_name = self.package_name
name = self.app_name
print()
Expand Down

0 comments on commit 495210a

Please sign in to comment.