Skip to content

Commit

Permalink
Added @exclude decorator to exclude nested configuration models. The …
Browse files Browse the repository at this point in the history
…system of nested configs has been fixed.
  • Loading branch information
morington committed Jun 30, 2024
1 parent 602cb7a commit ed963e4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
4 changes: 2 additions & 2 deletions confhub/__init__.py
Original file line number Diff line number Diff line change
@@ -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"]
30 changes: 24 additions & 6 deletions confhub/builder.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions confhub/core/block.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Type

from confhub.core.fields import ConfigurationField
from confhub.core.parsing import parsing_value

Expand Down
15 changes: 12 additions & 3 deletions confhub/core/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)}"
Expand All @@ -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
2 changes: 1 addition & 1 deletion confhub/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"str": (str, "VALUE"),
"int": (int, "1234"),
"float": (float, "1234.101"),
"bool": (bool, "true"),
"bool": (bool, "true")
}


Expand Down

0 comments on commit ed963e4

Please sign in to comment.