|
| 1 | +from typing import Any, Dict, Optional |
| 2 | + |
| 3 | +from mat3ra.code.entity import InMemoryEntitySnakeCase |
| 4 | +from mat3ra.esse.models.context_provider import ContextProviderSchema |
| 5 | + |
| 6 | + |
| 7 | +class ContextProvider(ContextProviderSchema, InMemoryEntitySnakeCase): |
| 8 | + """ |
| 9 | + Context provider for a template. |
| 10 | +
|
| 11 | + Attributes: |
| 12 | + name: The name of this item (required) |
| 13 | + domain: Domain of the context provider |
| 14 | + entityName: Entity name associated with the context provider |
| 15 | + data: Data object for the context provider |
| 16 | + extraData: Additional data object for the context provider |
| 17 | + isEdited: Flag indicating if the context provider has been edited |
| 18 | + context: Context object for the context provider |
| 19 | + """ |
| 20 | + |
| 21 | + @property |
| 22 | + def name_str(self) -> str: |
| 23 | + return self.name.value if hasattr(self.name, 'value') else str(self.name) |
| 24 | + |
| 25 | + @property |
| 26 | + def extra_data_key(self) -> str: |
| 27 | + return f"{self.name_str}ExtraData" |
| 28 | + |
| 29 | + @property |
| 30 | + def is_edited_key(self) -> str: |
| 31 | + return f"is{self.name_str}Edited" |
| 32 | + |
| 33 | + @property |
| 34 | + def is_unit_context_provider(self) -> bool: |
| 35 | + return self.entityName == "unit" |
| 36 | + |
| 37 | + @property |
| 38 | + def is_subworkflow_context_provider(self) -> bool: |
| 39 | + return self.entityName == "subworkflow" |
| 40 | + |
| 41 | + def _get_data_from_context(self, context: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: |
| 42 | + if not context: |
| 43 | + return {} |
| 44 | + data = context.get(self.name_str) |
| 45 | + is_edited = context.get(self.is_edited_key) |
| 46 | + extra_data = context.get(self.extra_data_key) |
| 47 | + result = {} |
| 48 | + if data is not None: |
| 49 | + result["data"] = data |
| 50 | + if is_edited is not None: |
| 51 | + result["isEdited"] = is_edited |
| 52 | + if extra_data is not None: |
| 53 | + result["extraData"] = extra_data |
| 54 | + return result |
| 55 | + |
| 56 | + def _get_effective_data(self, context: Optional[Dict[str, Any]] = None) -> Any: |
| 57 | + context_data = self._get_data_from_context(context or self.context) |
| 58 | + return context_data.get("data", self.data) |
| 59 | + |
| 60 | + def _get_effective_is_edited(self, context: Optional[Dict[str, Any]] = None) -> bool: |
| 61 | + context_data = self._get_data_from_context(context or self.context) |
| 62 | + return context_data.get("isEdited", self.isEdited) |
| 63 | + |
| 64 | + def _get_effective_extra_data(self, context: Optional[Dict[str, Any]] = None) -> Optional[Any]: |
| 65 | + context_data = self._get_data_from_context(context or self.context) |
| 66 | + return context_data.get("extraData", self.extraData) |
| 67 | + |
| 68 | + def yield_data(self, context: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: |
| 69 | + data = self._get_effective_data(context) |
| 70 | + is_edited = self._get_effective_is_edited(context) |
| 71 | + extra_data = self._get_effective_extra_data(context) |
| 72 | + result = { |
| 73 | + self.name_str: data, |
| 74 | + self.is_edited_key: is_edited, |
| 75 | + } |
| 76 | + if extra_data: |
| 77 | + result[self.extra_data_key] = extra_data |
| 78 | + return result |
| 79 | + |
| 80 | + def yield_data_for_rendering(self, context: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: |
| 81 | + return self.yield_data(context) |
| 82 | + |
| 83 | + def merge_context_data(self, result: Dict[str, Any], provider_context: Optional[Dict[str, Any]] = None) -> None: |
| 84 | + """ |
| 85 | + Merge this provider's rendering context data into result dictionary. |
| 86 | + Merges context keys if they are objects, otherwise overrides them. |
| 87 | +
|
| 88 | + Args: |
| 89 | + result: Dictionary to merge into (modified in place) |
| 90 | + provider_context: Optional external context to override provider's internal data |
| 91 | + """ |
| 92 | + context = self.yield_data_for_rendering(provider_context) |
| 93 | + for key, value in context.items(): |
| 94 | + if key in result and isinstance(result[key], dict) and isinstance(value, dict): |
| 95 | + result[key] = {**result[key], **value} |
| 96 | + else: |
| 97 | + result[key] = value |
| 98 | + |
0 commit comments