|
| 1 | +import os |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import Dict |
| 4 | + |
| 5 | +import yaml |
| 6 | + |
| 7 | +from mage_ai.data_preparation.models.project.models import ProjectDataClass |
| 8 | +from mage_ai.presenters.design.constants import CUSTOM_DESIGN_FILENAME |
| 9 | +from mage_ai.settings.platform import ( |
| 10 | + build_repo_path_for_all_projects, |
| 11 | + project_platform_activated, |
| 12 | +) |
| 13 | +from mage_ai.settings.repo import get_repo_path |
| 14 | +from mage_ai.shared.io import safe_write |
| 15 | +from mage_ai.shared.models import BaseDataClass |
| 16 | + |
| 17 | + |
| 18 | +@dataclass |
| 19 | +class DesignComponentConfigurations(BaseDataClass): |
| 20 | + logo: Dict = None |
| 21 | + |
| 22 | + |
| 23 | +@dataclass |
| 24 | +class DesignComponents(BaseDataClass): |
| 25 | + header: DesignComponentConfigurations = None |
| 26 | + |
| 27 | + def __post_init__(self): |
| 28 | + self.serialize_attribute_class('header', DesignComponentConfigurations) |
| 29 | + |
| 30 | + |
| 31 | +@dataclass |
| 32 | +class DesignPageConfigurations(BaseDataClass): |
| 33 | + edit: Dict = None |
| 34 | + |
| 35 | + |
| 36 | +@dataclass |
| 37 | +class DesignPages(BaseDataClass): |
| 38 | + pipelines: DesignPageConfigurations = None |
| 39 | + |
| 40 | + def __post_init__(self): |
| 41 | + self.serialize_attribute_class('pipelines', DesignPageConfigurations) |
| 42 | + |
| 43 | + |
| 44 | +@dataclass |
| 45 | +class CustomDesign(BaseDataClass): |
| 46 | + components: DesignComponents = None |
| 47 | + custom_designs: Dict = None |
| 48 | + pages: DesignPages = None |
| 49 | + project: ProjectDataClass = None |
| 50 | + |
| 51 | + def __post_init__(self): |
| 52 | + self.serialize_attribute_class('components', DesignComponents) |
| 53 | + self.serialize_attribute_class('pages', DesignPages) |
| 54 | + self.serialize_attribute_class('project', ProjectDataClass) |
| 55 | + |
| 56 | + @classmethod |
| 57 | + def file_path(self, repo_path: str = None) -> str: |
| 58 | + return os.path.join(repo_path or get_repo_path(), CUSTOM_DESIGN_FILENAME) |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def load_from_file( |
| 62 | + self, |
| 63 | + all_configurations: bool = True, |
| 64 | + file_path: str = None, |
| 65 | + repo_path: str = None, |
| 66 | + project: Dict = None, |
| 67 | + ) -> 'CustomDesign': |
| 68 | + model = self.__load_from_file(file_path=file_path, project=project, repo_path=repo_path) |
| 69 | + |
| 70 | + if all_configurations and project_platform_activated(): |
| 71 | + model.custom_designs = {} |
| 72 | + |
| 73 | + for project_name, project in build_repo_path_for_all_projects().items(): |
| 74 | + full_path = project['full_path'] |
| 75 | + |
| 76 | + model.custom_designs[project_name] = self.__load_from_file( |
| 77 | + file_path=os.path.join(full_path, CUSTOM_DESIGN_FILENAME), |
| 78 | + project=project, |
| 79 | + ) |
| 80 | + |
| 81 | + return model |
| 82 | + |
| 83 | + @classmethod |
| 84 | + def __load_from_file( |
| 85 | + self, |
| 86 | + file_path: str = None, |
| 87 | + project: Dict = None, |
| 88 | + repo_path: str = None, |
| 89 | + ) -> 'CustomDesign': |
| 90 | + yaml_config = {} |
| 91 | + |
| 92 | + file_path_to_use = file_path or self.file_path(repo_path=repo_path) |
| 93 | + if os.path.exists(file_path_to_use): |
| 94 | + with open(file_path_to_use, 'r') as fp: |
| 95 | + content = fp.read() |
| 96 | + if content: |
| 97 | + yaml_config = yaml.safe_load(content) or {} |
| 98 | + |
| 99 | + return self.load(project=project, **yaml_config) |
| 100 | + |
| 101 | + def save(self, file_path: str = None, repo_path: str = None) -> None: |
| 102 | + if not file_path: |
| 103 | + file_path = self.file_path(repo_path=repo_path) |
| 104 | + |
| 105 | + content_original = None |
| 106 | + if os.path.exists(file_path): |
| 107 | + with open(file_path) as f: |
| 108 | + content_original = f.read() |
| 109 | + |
| 110 | + with open(file_path, 'w'): |
| 111 | + try: |
| 112 | + data = self.to_dict() |
| 113 | + content = yaml.safe_dump(data) |
| 114 | + safe_write(file_path, content) |
| 115 | + except Exception as err: |
| 116 | + if content_original: |
| 117 | + safe_write(file_path, content_original) |
| 118 | + raise err |
| 119 | + |
| 120 | + def to_dict(self, **kwargs) -> Dict: |
| 121 | + return super().to_dict( |
| 122 | + convert_enum=True, |
| 123 | + ignore_attributes=['custom_designs'], |
| 124 | + ignore_empty=True, |
| 125 | + ) |
0 commit comments