Skip to content

Commit

Permalink
Testing the generation of a list with nested models. Minor adjustment…
Browse files Browse the repository at this point in the history
…s to generation.
  • Loading branch information
morington committed Jun 30, 2024
1 parent ed963e4 commit 2aadf7b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
1 change: 1 addition & 0 deletions confhub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
from confhub.core.block import BlockCore
from confhub.reader import Confhub


__all__ = ["field", "exclude", "BlockCore", "Confhub"]
39 changes: 28 additions & 11 deletions confhub/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ def __init__(self, *blocks: BlockCore):
def generate_filenames(self) -> Dict[str, Any]:
_datafiles = {'settings': {}, '.secrets': {}}

def add_field_to_datafiles(field_name: str, field: ConfigurationField, parent_path: List[str]) -> None:
def data_typing(field: ConfigurationField) -> Any:
if field.is_list:
return [field.get_default_value()]

return field.get_default_value()

def add_field_to_datafiles(
field_name: str, field: ConfigurationField, parent_path: List[str]
) -> None:
if field.secret:
target = _datafiles['.secrets']
elif field.filename:
Expand All @@ -36,7 +44,7 @@ def add_field_to_datafiles(field_name: str, field: ConfigurationField, parent_pa
current[part] = {}
current = current[part]

current[field_name] = field.get_default_value() if not field.is_list else [field.get_default_value()]
current[field_name] = data_typing(field)

def has_configuration_fields(select_class: BlockCore) -> bool:
if any(isinstance(item, ConfigurationField) for item in [
Expand All @@ -51,17 +59,23 @@ def has_configuration_fields(select_class: BlockCore) -> bool:
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:
def process_block(_block: BlockCore, parent_path: List[str], parent: str = None) -> None:
if hasattr(_block, '__exclude__') and _block.__exclude__ and not parent:
return

current_path = parent_path + [_block.__block__]
current_path = parent_path + [_block.__block__ if not parent else parent]
if parent:
current_path += [_block.__block__]

# field.is_list
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)
if isinstance(field.data_type, BlockCore):
process_block(field.data_type, current_path, parent=field_name)
else:
add_field_to_datafiles(field_name, field, current_path)
elif isinstance(field, BlockCore):
process_block(field, current_path, parent=True)
process_block(field, current_path, parent=field_name)

for block in self.blocks:
if block != BlockCore:
Expand All @@ -77,11 +91,14 @@ def create_files(self, config_path: Path) -> None:
with open(file_path, 'r', encoding='utf-8') as file:
yaml_data = yaml.safe_load(file)

for key in data:
if key in yaml_data:
for inner_key in data[key]:
if inner_key in yaml_data[key]:
data[key][inner_key] = yaml_data[key][inner_key]
if yaml_data:
for key in data:
if key in yaml_data:
if not isinstance(yaml_data[key], Dict):
for inner_key in data[key]:
if inner_key in yaml_data[key]:
if isinstance(data[key][inner_key], type(yaml_data[key][inner_key])):
data[key][inner_key] = yaml_data[key][inner_key]

with open(file_path, 'w', encoding='utf-8') as file:
yaml.dump(data, file, default_flow_style=False)
Expand Down
12 changes: 9 additions & 3 deletions confhub/core/fields.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Type
from typing import Any

from confhub.core.types import DataTypeMapping


class ConfigurationField:
def __init__(
self,
data_type: Type,
data_type: Any,
secret: bool,
filename: str,
is_list: bool,
Expand All @@ -16,12 +16,18 @@ def __init__(
self.filename = filename
self.is_list = is_list

def __str__(self) -> str:
return f"ConfigurationField: [{self.data_type}]"

def __repr__(self) -> str:
return f"ConfigurationField: [{self.data_type}; secret={self.secret}; filename={self.filename}; is_list={self.is_list}]"

def get_default_value(self) -> str:
return f"{self.data_type.__name__}; {DataTypeMapping.get_default_value(self.data_type.__name__)}"


def field(
data_type: Type,
data_type: Any,
secret: bool = False,
filename: str = None,
is_list: bool = False
Expand Down
1 change: 1 addition & 0 deletions confhub/core/parsing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from typing import Union, Any, Dict, List

import yaml
Expand Down
7 changes: 6 additions & 1 deletion confhub/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ def __load(self, *models: BlockCore, files: List[str | Path]) -> Type[dataclasse

__fields_from_dataclass.append((block.__block__, type(block), dataclasses.field(default=value)))

return dataclasses.make_dataclass('Data', __fields_from_dataclass)
return dataclasses.make_dataclass('Data', __fields_from_dataclass)


if __name__ == '__main__':
data = Confhub().models
print()

0 comments on commit 2aadf7b

Please sign in to comment.