Skip to content

Commit

Permalink
✨ add parameters for Node.
Browse files Browse the repository at this point in the history
Add user parameters and find_parameter/find_parent_parameter methods.
  • Loading branch information
perillaroc committed Apr 27, 2022
1 parent f9bb896 commit c17489f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
38 changes: 36 additions & 2 deletions takler/core/node.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Union, List, Optional, Mapping
from typing import Union, List, Optional, Dict
from pathlib import PurePosixPath
from collections import defaultdict

Expand Down Expand Up @@ -83,7 +83,7 @@ def __init__(self, name: str):
self.children = list() # type: List[Node]

# 参数
self.parameters = dict() # type: Mapping[str, Parameter]
self.parameters = dict() # type: Dict[str, Parameter]

# 触发器
self.trigger_expression = None # type: Optional[Expression]
Expand Down Expand Up @@ -337,6 +337,7 @@ def handle_status_change(self):
self.swim_status_change()

# Trigger --------------------------------------------------------

def add_trigger(self, trigger: Union[str, Expression]):
if isinstance(trigger, str):
self.trigger_expression = Expression(trigger)
Expand Down Expand Up @@ -369,6 +370,39 @@ def resolve_dependencies(self) -> bool:

return True

# Parameter ------------------------------------------------------

def add_parameter(self, name: str, value: Union[str, float, int, bool]):
"""
Add a ``Parameter`` to this node.
"""
p = Parameter(name=name, value=value)
self.parameters[name] = p
return p

def find_parameter(self, name: str) -> Optional[Parameter]:
"""
Find a ``parameter`` only in this node.
"""
return self.parameters.get(name, None)

def find_parent_parameter(self, name: str) -> Optional[Parameter]:
"""
Find a ``Parameter`` up along the node tree.
"""
p = self.find_parameter(name)
if p is not None:
return p

parent_node = self.parent
while parent_node is not None:
p = parent_node.find_parameter(name)
if p is not None:
return p
parent_node = parent_node.parent

return None

# Node Operations ------------------------------------------------

def requeue(self):
Expand Down
23 changes: 20 additions & 3 deletions takler/core/parameter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
from typing import Union, Optional

# Bunch level
TAKLER_HOST = "TAKLER_HOST"
TAKLER_PORT = "TAKLER_PORT"
TAKLER_HOME = "TAKLER_HOME"

# Flow level
FLOW = "FLOW"
TAKLER_DATE = "TAKLER_DATE"
TAKLER_TIME = "TAKLER_TIME"

# Task level
TASK = "TASK"
TAKLER_NAME = "TAKLER_NAME"


class Parameter(object):
def __init__(self, name: str, value: Optional[Union[str, int, float]] = None):
self.name = name
self.value = value
def __init__(self, name: str, value: Optional[Union[str, int, float, bool]] = None):
self.name = name # type: str
self.value = value # type: Optional[Union[str, int, float, bool]]

def __eq__(self, other):
return type(other) == type(self) and other.name == self.name and other.value == self.value

0 comments on commit c17489f

Please sign in to comment.