From ed963e49cf250c875c0e2d0fe0f94b6f587bc2a1 Mon Sep 17 00:00:00 2001 From: Adam <123621952+morington@users.noreply.github.com> Date: Mon, 1 Jul 2024 00:37:09 +0300 Subject: [PATCH] Added @exclude decorator to exclude nested configuration models. The system of nested configs has been fixed. --- confhub/__init__.py | 4 ++-- confhub/builder.py | 30 ++++++++++++++++++++++++------ confhub/core/block.py | 2 ++ confhub/core/fields.py | 15 ++++++++++++--- confhub/core/types.py | 2 +- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/confhub/__init__.py b/confhub/__init__.py index cc6dc64..471bdcb 100644 --- a/confhub/__init__.py +++ b/confhub/__init__.py @@ -1,5 +1,5 @@ -from confhub.core.fields import field +from confhub.core.fields import field, exclude from confhub.core.block import BlockCore from confhub.reader import Confhub -__all__ = ["field", "BlockCore", "Confhub"] +__all__ = ["field", "exclude", "BlockCore", "Confhub"] diff --git a/confhub/builder.py b/confhub/builder.py index 0bb2fad..eb63da1 100644 --- a/confhub/builder.py +++ b/confhub/builder.py @@ -1,10 +1,11 @@ from pathlib import Path -from typing import List, Any, Dict +from typing import List, Any, Dict, Optional import yaml import structlog from confhub.core.block import BlockCore +from confhub.core.error import ConfhubError from confhub.core.fields import ConfigurationField from confhub.utils.gitignore import add_to_gitignore @@ -35,19 +36,36 @@ def add_field_to_datafiles(field_name: str, field: ConfigurationField, parent_pa current[part] = {} current = current[part] - current[field_name] = field.get_default_value() + current[field_name] = field.get_default_value() if not field.is_list else [field.get_default_value()] + + def has_configuration_fields(select_class: BlockCore) -> bool: + if any(isinstance(item, ConfigurationField) for item in [ + value for name, value in select_class.__dict__.items() + ]): + return True + else: + if any(isinstance(item, ConfigurationField) for item in [ + value for name, value in select_class.__class__.__dict__.items() + ]): + return False + else: + raise ConfhubError("Cannot find field in model object", select_class=select_class) + + def process_block(_block: BlockCore, parent_path: List[str], parent: bool = False) -> None: + if hasattr(_block, '__exclude__') and _block.__exclude__ and not parent: + return - def process_block(_block: BlockCore, parent_path: List[str]) -> None: current_path = parent_path + [_block.__block__] - for field_name, field in _block.__dict__.items(): + for field_name, field in (_block.__dict__.items() if has_configuration_fields(_block) else _block.__class__.__dict__.items()): if isinstance(field, ConfigurationField): add_field_to_datafiles(field_name, field, current_path) elif isinstance(field, BlockCore): - process_block(field, current_path) + process_block(field, current_path, parent=True) for block in self.blocks: - process_block(block, []) + if block != BlockCore: + process_block(block, []) return _datafiles diff --git a/confhub/core/block.py b/confhub/core/block.py index 2ee1d92..8d0114d 100644 --- a/confhub/core/block.py +++ b/confhub/core/block.py @@ -1,3 +1,5 @@ +from typing import Type + from confhub.core.fields import ConfigurationField from confhub.core.parsing import parsing_value diff --git a/confhub/core/fields.py b/confhub/core/fields.py index fb2b7fe..5127f15 100644 --- a/confhub/core/fields.py +++ b/confhub/core/fields.py @@ -8,11 +8,13 @@ def __init__( self, data_type: Type, secret: bool, - filename: str + filename: str, + is_list: bool, ) -> None: self.data_type = data_type self.secret = secret self.filename = filename + self.is_list = is_list def get_default_value(self) -> str: return f"{self.data_type.__name__}; {DataTypeMapping.get_default_value(self.data_type.__name__)}" @@ -21,10 +23,17 @@ def get_default_value(self) -> str: def field( data_type: Type, secret: bool = False, - filename: str = None + filename: str = None, + is_list: bool = False ) -> ConfigurationField: return ConfigurationField( data_type=data_type, secret=secret, - filename=filename + filename=filename, + is_list=is_list ) + + +def exclude(cls): + cls.__exclude__ = True + return cls diff --git a/confhub/core/types.py b/confhub/core/types.py index b53ce88..dc09915 100644 --- a/confhub/core/types.py +++ b/confhub/core/types.py @@ -5,7 +5,7 @@ "str": (str, "VALUE"), "int": (int, "1234"), "float": (float, "1234.101"), - "bool": (bool, "true"), + "bool": (bool, "true") }