|
| 1 | +from typing import List, Optional |
| 2 | +from defusedxml.ElementTree import fromstring |
| 3 | + |
| 4 | +from tableauserverclient.models.schedule_item import ScheduleItem |
| 5 | +from tableauserverclient.models.task_item import TaskItem |
| 6 | + |
| 7 | +class LinkedTaskItem: |
| 8 | + def __init__(self) -> None: |
| 9 | + self.id: Optional[str] = None |
| 10 | + self.num_steps: Optional[int] = None |
| 11 | + self.schedule: Optional[ScheduleItem] = None |
| 12 | + |
| 13 | + @classmethod |
| 14 | + def from_response(cls, resp: bytes, namespace) -> List["LinkedTaskItem"]: |
| 15 | + parsed_response = fromstring(resp) |
| 16 | + return [cls._parse_element(x, namespace) for x in parsed_response.findall(".//t:linkedTasks[@id]", namespaces=namespace)] |
| 17 | + |
| 18 | + @classmethod |
| 19 | + def _parse_element(cls, xml, namespace) -> "LinkedTaskItem": |
| 20 | + task = cls() |
| 21 | + task.id = xml.get("id") |
| 22 | + task.num_steps = int(xml.get("numSteps")) |
| 23 | + task.schedule = ScheduleItem.from_element(xml, namespace)[0] |
| 24 | + return task |
| 25 | + |
| 26 | +class LinkedTaskStepItem: |
| 27 | + def __init__(self) -> None: |
| 28 | + self.id: Optional[str] = None |
| 29 | + self.step_number: Optional[int] = None |
| 30 | + self.stop_downstream_on_failure: Optional[bool] = None |
| 31 | + self.task_details: List[LinkedTaskFlowRunItem] = [] |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def from_task_xml(cls, xml, namespace) -> List["LinkedTaskStepItem"]: |
| 35 | + return [cls._parse_element(x, namespace) for x in xml.findall(".//t:linkedTaskSteps[@id]", namespace)] |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def _parse_element(cls, xml, namespace) -> "LinkedTaskStepItem": |
| 39 | + step = cls() |
| 40 | + step.id = xml.get("id") |
| 41 | + step.step_number = int(xml.get("stepNumber")) |
| 42 | + step.stop_downstream_on_failure = string_to_bool(xml.get("stopDownstreamTasksOnFailure")) |
| 43 | + step.task_details = LinkedTaskFlowRunItem._parse_element(xml, namespace) |
| 44 | + return step |
| 45 | + |
| 46 | +class LinkedTaskFlowRunItem: |
| 47 | + def __init__(self) -> None: |
| 48 | + self.flow_run_id: Optional[str] = None |
| 49 | + self.flow_run_priority: Optional[int] = None |
| 50 | + self.flow_run_consecutive_failed_count: Optional[int] = None |
| 51 | + self.flow_run_task_type: Optional[str] = None |
| 52 | + self.flow_id: Optional[str] = None |
| 53 | + self.flow_name: Optional[str] = None |
| 54 | + |
| 55 | + @classmethod |
| 56 | + def _parse_element(cls, xml, namespace) -> List["LinkedTaskFlowRunItem"]: |
| 57 | + all_tasks = [] |
| 58 | + for flow_run in xml.findall(".//t:flowRun[@id]", namespace): |
| 59 | + task = cls() |
| 60 | + task.flow_run_id = flow_run.get("id") |
| 61 | + task.flow_run_priority = int(flow_run.get("priority")) |
| 62 | + task.flow_run_consecutive_failed_count = int(flow_run.get("consecutiveFailedCount")) |
| 63 | + task.flow_run_task_type = flow_run.get("type") |
| 64 | + flow = flow_run.find(".//t:flow[@id]", namespace) |
| 65 | + task.flow_id = flow.get("id") |
| 66 | + task.flow_name = flow.get("name") |
| 67 | + all_tasks.append(task) |
| 68 | + |
| 69 | + return all_tasks |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +def string_to_bool(s: str) -> bool: |
| 74 | + return s.lower() == "true" |
0 commit comments