|
37 | 37 |
|
38 | 38 | DATA_TRANSFORM_CONFIG_TEMPLATE = { |
39 | 39 | "sdkVersion": get_version(), |
| 40 | + "type": "script", |
40 | 41 | "entryPoint": "", |
41 | | - "dataspace": "", |
| 42 | + "dataspace": "default", |
42 | 43 | "permissions": { |
43 | 44 | "read": {}, |
44 | 45 | "write": {}, |
45 | 46 | }, |
46 | 47 | } |
47 | 48 |
|
| 49 | +FUNCTION_CONFIG_TEMPLATE = { |
| 50 | + "sdkVersion": get_version(), |
| 51 | + "type": "function", |
| 52 | + "entryPoint": "", |
| 53 | +} |
48 | 54 | STANDARD_LIBS = set(sys.stdlib_module_names) |
49 | 55 |
|
50 | 56 |
|
@@ -230,57 +236,66 @@ def scan_file(file_path: str) -> DataAccessLayerCalls: |
230 | 236 | return visitor.found() |
231 | 237 |
|
232 | 238 |
|
233 | | -def dc_config_json_from_file(file_path: str) -> dict[str, Any]: |
| 239 | +def dc_config_json_from_file(file_path: str, type: str) -> dict[str, Any]: |
234 | 240 | """Create a Data Cloud Custom Code config JSON from a script.""" |
235 | | - output = scan_file(file_path) |
236 | | - config = DATA_TRANSFORM_CONFIG_TEMPLATE.copy() |
| 241 | + config: dict[str, Any] |
| 242 | + if type == "script": |
| 243 | + config = DATA_TRANSFORM_CONFIG_TEMPLATE.copy() |
| 244 | + elif type == "function": |
| 245 | + config = FUNCTION_CONFIG_TEMPLATE.copy() |
237 | 246 | config["entryPoint"] = file_path.rpartition("/")[-1] |
| 247 | + return config |
238 | 248 |
|
| 249 | + |
| 250 | +def update_config(file_path: str) -> dict[str, Any]: |
239 | 251 | file_dir = os.path.dirname(file_path) |
240 | 252 | config_json_path = os.path.join(file_dir, "config.json") |
241 | | - |
| 253 | + existing_config: dict[str, Any] |
242 | 254 | if os.path.exists(config_json_path) and os.path.isfile(config_json_path): |
243 | 255 | try: |
244 | 256 | with open(config_json_path, "r") as f: |
245 | 257 | existing_config = json.load(f) |
246 | | - |
247 | | - if "dataspace" in existing_config: |
248 | | - dataspace_value = existing_config["dataspace"] |
249 | | - if not dataspace_value or ( |
250 | | - isinstance(dataspace_value, str) and dataspace_value.strip() == "" |
251 | | - ): |
252 | | - logger.warning( |
253 | | - f"dataspace in {config_json_path} is empty or None. " |
254 | | - f"Updating config file to use dataspace 'default'. " |
255 | | - ) |
256 | | - config["dataspace"] = "default" |
257 | | - else: |
258 | | - config["dataspace"] = dataspace_value |
259 | | - else: |
260 | | - raise ValueError( |
261 | | - f"dataspace must be defined in {config_json_path}. " |
262 | | - f"Please add a 'dataspace' field to the config.json file. " |
263 | | - ) |
264 | 258 | except json.JSONDecodeError as e: |
265 | 259 | raise ValueError( |
266 | 260 | f"Failed to parse JSON from {config_json_path}: {e}" |
267 | 261 | ) from e |
268 | 262 | except OSError as e: |
269 | 263 | raise OSError(f"Failed to read config file {config_json_path}: {e}") from e |
270 | 264 | else: |
271 | | - config["dataspace"] = "default" |
272 | | - |
273 | | - read: dict[str, list[str]] = {} |
274 | | - if output.read_dlo: |
275 | | - read["dlo"] = list(output.read_dlo) |
276 | | - else: |
277 | | - read["dmo"] = list(output.read_dmo) |
278 | | - write: dict[str, list[str]] = {} |
279 | | - if output.write_to_dlo: |
280 | | - write["dlo"] = list(output.write_to_dlo) |
| 265 | + raise ValueError(f"config.json not found at {config_json_path}") |
| 266 | + if existing_config["type"] == "script": |
| 267 | + existing_config["dataspace"] = get_dataspace(existing_config) |
| 268 | + output = scan_file(file_path) |
| 269 | + read: dict[str, list[str]] = {} |
| 270 | + if output.read_dlo: |
| 271 | + read["dlo"] = list(output.read_dlo) |
| 272 | + else: |
| 273 | + read["dmo"] = list(output.read_dmo) |
| 274 | + write: dict[str, list[str]] = {} |
| 275 | + if output.write_to_dlo: |
| 276 | + write["dlo"] = list(output.write_to_dlo) |
| 277 | + else: |
| 278 | + write["dmo"] = list(output.write_to_dmo) |
| 279 | + |
| 280 | + existing_config["permissions"] = {"read": read, "write": write} |
| 281 | + return existing_config |
| 282 | + |
| 283 | + |
| 284 | +def get_dataspace(existing_config: dict[str, str]) -> str: |
| 285 | + if "dataspace" in existing_config: |
| 286 | + dataspace_value = existing_config["dataspace"] |
| 287 | + if not dataspace_value or ( |
| 288 | + isinstance(dataspace_value, str) and dataspace_value.strip() == "" |
| 289 | + ): |
| 290 | + logger.warning( |
| 291 | + "dataspace is empty or None. " |
| 292 | + "Updating config file to use dataspace 'default'. " |
| 293 | + ) |
| 294 | + return "default" |
| 295 | + else: |
| 296 | + return dataspace_value |
281 | 297 | else: |
282 | | - write["dmo"] = list(output.write_to_dmo) |
283 | | - |
284 | | - config["permissions"] = {"read": read, "write": write} |
285 | | - |
286 | | - return config |
| 298 | + raise ValueError( |
| 299 | + "dataspace must be defined. " |
| 300 | + "Please add a 'dataspace' field to the config.json file. " |
| 301 | + ) |
0 commit comments