Skip to content

Commit

Permalink
✨ add time attribute for node. #43
Browse files Browse the repository at this point in the history
  • Loading branch information
perillaroc committed Sep 8, 2022
1 parent 96e3cce commit 5e1cd79
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions takler/core/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def update_calendar(self, time: datetime.datetime):
"""
self.calendar.update(time)
self.update_generated_parameters()
self.calendar_changed(self.calendar)

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

Expand Down
52 changes: 52 additions & 0 deletions takler/core/node.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import datetime
from typing import Union, List, Optional, Dict, TYPE_CHECKING, Set
from pathlib import PurePosixPath
from collections import defaultdict
Expand All @@ -12,9 +13,12 @@
from .limit import Limit, InLimit, InLimitManager
from .expression import Expression
from .repeat import Repeat, RepeatBase
from .time_attr import TimeAttribute

if TYPE_CHECKING:
from .bunch import Bunch
from .calendar import Calendar
from .flow import Flow


# def compute_node_status(node: Node, immediate: bool) -> NodeStatus:
Expand Down Expand Up @@ -109,6 +113,9 @@ def __init__(self, name: str):
# 重复
self.repeat: Optional[Repeat] = None

# 时间
self.times: List[TimeAttribute] = list()

def __enter__(self):
return self

Expand Down Expand Up @@ -418,10 +425,15 @@ def evaluate_trigger(self) -> bool:

return self.trigger_expression.evaluate()

# Resolve -----------------------------------------------------------

def resolve_dependencies(self) -> bool:
if self.is_suspended():
return False

if not self.is_time_dependencies_free():
return False

if not self.evaluate_trigger():
return False

Expand Down Expand Up @@ -769,6 +781,43 @@ def decrement_in_limit(self, limit_set: Set[Limit]):
def add_repeat(self, r: RepeatBase):
self.repeat = Repeat(r)

# Time Attribute -----------------------------------------------------------

def add_time(self, time: datetime.time) -> TimeAttribute:
"""
Add a ``TimeAttribute`` to Node.
Parameters
----------
time
Returns
-------
TimeAttribute
"""
time_attr = TimeAttribute(time)
self.times.append(time_attr)
return time_attr

def is_time_dependencies_free(self):
if len(self.times) == 0:
return True

flow = self.get_root()
if not hasattr(flow, "calendar"):
raise RuntimeError("node should be in a flow to check time dependencies.")

flow_calendar: Calendar = flow.calendar
for time_attr in self.times:
if time_attr.is_free(flow_calendar):
return True

return False

def calendar_changed(self, calendar: Calendar):
for time_attr in self.times:
time_attr.calendar_changed(calendar)

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

def requeue(self, reset_repeat: bool = True):
Expand All @@ -779,6 +828,9 @@ def requeue(self, reset_repeat: bool = True):
"""
self.set_node_status_only(NodeStatus.queued)

for time_attr in self.times:
time_attr.reset()

for event in self.events:
event.reset()

Expand Down
8 changes: 8 additions & 0 deletions takler/core/node_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .node import Node, compute_most_significant_status
from .state import NodeStatus
from .task_node import Task
from .calendar import Calendar


class NodeContainer(Node):
Expand Down Expand Up @@ -105,6 +106,13 @@ def resolve_dependencies(self) -> bool:

return True

# Time Attribute --------------------------------------------

def calendar_changed(self, calendar: Calendar):
super(NodeContainer, self).calendar_changed(calendar)
for node in self.children:
node.calendar_changed(calendar)

# Node operation -----------------------------------------

def requeue(self, reset_repeat: bool = True):
Expand Down
36 changes: 36 additions & 0 deletions takler/core/time_attr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import datetime

from .calendar import Calendar


class TimeAttribute:
def __init__(self, time: datetime.time):
self.time: datetime.time = time
self.free: bool = False

def is_free(self, calendar: Calendar) -> bool:
if self.free:
return True

flow_time = calendar.flow_time
hour = flow_time.hour
minute = flow_time.minute
if hour == self.time.hour and minute == self.time.minute:
return True
else:
return False

def reset(self):
self.clear_free()

def set_free(self):
self.free = True

def clear_free(self):
self.free = False

def calendar_changed(self, calendar: Calendar):
if self.free:
return
if self.is_free(calendar):
self.set_free()

0 comments on commit 5e1cd79

Please sign in to comment.