|
4 | 4 | from __future__ import annotations
|
5 | 5 |
|
6 | 6 | import sys
|
| 7 | +from io import StringIO |
| 8 | +import hashlib |
7 | 9 | from datetime import datetime
|
8 | 10 | from typing import Any, ClassVar, Dict, List, Optional, TypeVar
|
9 | 11 |
|
@@ -65,12 +67,14 @@ def validate_single_required_field(field_names: list[str], values: list[Any]):
|
65 | 67 | f"Only one of the following parameters are allowed: {', '.join(names)}"
|
66 | 68 | )
|
67 | 69 |
|
68 |
| -def validate_required_fields(field_names:list[str], values:list[Any]): |
69 |
| - for field_name, value in zip(field_names, values): |
| 70 | +def validate_required_fields(field_names: list[str], values: list[Any]): |
| 71 | + for field_name, value in zip(field_names, values): |
70 | 72 | if value is None:
|
71 | 73 | raise ValueError(f"{field_name} is required")
|
72 | 74 | if isinstance(value, str) and not value.strip():
|
73 | 75 | raise ValueError(f"{field_name} cannot be blank")
|
| 76 | + if isinstance(value, list) and len(value) == 0: |
| 77 | + raise ValueError(f"{field_name} cannot be an empty list") |
74 | 78 | {%- macro gen_properties(attribute_defs, additional_names=[]) %}
|
75 | 79 | _convience_properties: ClassVar[list[str]] = [
|
76 | 80 | {%- for attribute_def in attribute_defs %}
|
@@ -455,6 +459,70 @@ class {{ entity_def.name }}({{super_classes[0]}} {%- if "Asset" in super_classes
|
455 | 459 | raise ValueError(
|
456 | 460 | "One of admin_user, admin_groups or admin_roles is required"
|
457 | 461 | )
|
| 462 | + {%- elif entity_def.name == "Process" %} |
| 463 | + @staticmethod |
| 464 | + def generate_qualified_name( |
| 465 | + name: str, |
| 466 | + connection_qualified_name: str, |
| 467 | + inputs: list["Catalog"], |
| 468 | + outputs: list["Catalog"], |
| 469 | + parent: Optional["Process"] = None, |
| 470 | + process_id: Optional[str] = None, |
| 471 | + ) -> str: |
| 472 | + def append_relationship(output: StringIO, relationship: Asset): |
| 473 | + if relationship.guid: |
| 474 | + output.write(relationship.guid) |
| 475 | + |
| 476 | + def append_relationships(output: StringIO, relationships: list["Catalog"]): |
| 477 | + for catalog in relationships: |
| 478 | + append_relationship(output, catalog) |
| 479 | + |
| 480 | + validate_required_fields( |
| 481 | + ["name", "connection_qualified_name", "inputs", "outputs"], |
| 482 | + [name, connection_qualified_name, inputs, outputs], |
| 483 | + ) |
| 484 | + if process_id and process_id.strip(): |
| 485 | + return f"{connection_qualified_name}/{process_id}" |
| 486 | + buffer = StringIO() |
| 487 | + buffer.write(name) |
| 488 | + buffer.write(connection_qualified_name) |
| 489 | + if parent: |
| 490 | + append_relationship(buffer, parent) |
| 491 | + append_relationships(buffer, inputs) |
| 492 | + append_relationships(buffer, outputs) |
| 493 | + ret_value = hashlib.md5( |
| 494 | + buffer.getvalue().encode(), usedforsecurity=False |
| 495 | + ).hexdigest() |
| 496 | + buffer.close() |
| 497 | + return ret_value |
| 498 | + |
| 499 | + @classmethod |
| 500 | + def create( |
| 501 | + cls, |
| 502 | + name: str, |
| 503 | + connection_qualified_name: str, |
| 504 | + inputs: list["Catalog"], |
| 505 | + outputs: list["Catalog"], |
| 506 | + process_id: Optional[str] = None, |
| 507 | + parent: Optional[Process] = None, |
| 508 | + ) -> Process.Attributes: |
| 509 | + qualified_name = Process.Attributes.generate_qualified_name( |
| 510 | + name=name, |
| 511 | + connection_qualified_name=connection_qualified_name, |
| 512 | + process_id=process_id, |
| 513 | + inputs=inputs, |
| 514 | + outputs=outputs, |
| 515 | + parent=parent, |
| 516 | + ) |
| 517 | + connector_name = connection_qualified_name.split("/")[1] |
| 518 | + return Process.Attributes( |
| 519 | + name=name, |
| 520 | + qualified_name=qualified_name, |
| 521 | + connector_name=connector_name, |
| 522 | + connection_qualified_name=connection_qualified_name, |
| 523 | + inputs=inputs, |
| 524 | + outputs=outputs, |
| 525 | + ) |
458 | 526 | {%- elif entity_def.name == "Readme" %}
|
459 | 527 | @classmethod
|
460 | 528 | # @validate_arguments()
|
@@ -755,6 +823,27 @@ class {{ entity_def.name }}({{super_classes[0]}} {%- if "Asset" in super_classes
|
755 | 823 | raise ValueError(
|
756 | 824 | "One of admin_user, admin_groups or admin_roles is required"
|
757 | 825 | )
|
| 826 | + {%- elif entity_def.name == "Process" %} |
| 827 | + @classmethod |
| 828 | + def create( |
| 829 | + cls, |
| 830 | + name: str, |
| 831 | + connection_qualified_name: str, |
| 832 | + inputs: list["Catalog"], |
| 833 | + outputs: list["Catalog"], |
| 834 | + process_id: Optional[str] = None, |
| 835 | + parent: Optional[Process] = None, |
| 836 | + ) -> Process: |
| 837 | + return Process( |
| 838 | + attributes=Process.Attributes.create( |
| 839 | + name=name, |
| 840 | + connection_qualified_name=connection_qualified_name, |
| 841 | + process_id=process_id, |
| 842 | + inputs=inputs, |
| 843 | + outputs=outputs, |
| 844 | + parent=parent, |
| 845 | + ) |
| 846 | + ) |
758 | 847 | {%- elif entity_def.name == "Database" %}
|
759 | 848 | @classmethod
|
760 | 849 | # @validate_arguments()
|
|
0 commit comments