| 110 | |
| 111 | @staticmethod |
| 112 | def normalize(attribute: Any, parent: Path, env_error: bool = True) -> Any: |
| 113 | if isinstance(attribute, str): |
| 114 | attribute = attribute.strip() |
| 115 | if attribute.startswith("${") and attribute.endswith("}"): |
| 116 | variable = attribute[2:-1].split(":") |
| 117 | if variable[0] in os.environ.keys(): |
| 118 | return os.environ[variable[0]] |
| 119 | else: |
| 120 | if len(variable) > 1: |
| 121 | return variable[1] |
| 122 | else: |
| 123 | if env_error: |
| 124 | raise ValueError( |
| 125 | f"Variable {variable[0]} not found in environment" |
| 126 | ) |
| 127 | else: |
| 128 | return "" |
| 129 | elif ( |
| 130 | attribute.startswith("file:") |
| 131 | and Path(parent / attribute.split(":")[1]).exists() |
| 132 | ): |
| 133 | with open(parent / attribute.split(":")[1], "r") as f: |
| 134 | items = json.load(f) |
| 135 | if isinstance(items, list): |
| 136 | return [Prompty.normalize(value, parent) for value in items] |
| 137 | elif isinstance(items, dict): |
| 138 | return { |
| 139 | key: Prompty.normalize(value, parent) |
| 140 | for key, value in items.items() |
| 141 | } |
| 142 | else: |
| 143 | return items |
| 144 | else: |
| 145 | return attribute |
| 146 | elif isinstance(attribute, list): |
| 147 | return [Prompty.normalize(value, parent) for value in attribute] |
| 148 | elif isinstance(attribute, dict): |
| 149 | return { |
| 150 | key: Prompty.normalize(value, parent) |
| 151 | for key, value in attribute.items() |
| 152 | } |
| 153 | else: |
| 154 | return attribute |
| 155 | |
| 156 | |
| 157 | def param_hoisting( |