Skip to content

Commit

Permalink
conf: add to context of task exec/shell and if/unless expressions: `t…
Browse files Browse the repository at this point in the history
…ask_name` and `now`
  • Loading branch information
jesteria committed Jan 9, 2025
1 parent 817c49c commit d985910
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/fate/common/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ def compose(cls, task: CompletingTask, output: TaskOutput) -> typing.Optional[Ta
label=output.label,
label_ext=output.label and f'-{output.label}',
task_ended_at=datetime.datetime.fromtimestamp(task.ended_()),
task_name=task.__name__,
)

return cls(
Expand Down
36 changes: 29 additions & 7 deletions src/fate/conf/types/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
import typing
import uuid
from datetime import timedelta
from datetime import datetime, timedelta
from enum import IntEnum

import croniter
Expand Down Expand Up @@ -192,6 +192,20 @@ def scheduling_(self):
self._DefaultScheduling.__members__,
)

@property
def __trunk__(self):
try:
return self.__parents__[-2]
except IndexError:
return self

@property
def _context(self):
return {
'now': datetime.now(),
'task_name': self.__trunk__.__name__,
}

@at_depth(0)
@property
def exec_(self) -> typing.Tuple[str]:
Expand All @@ -203,7 +217,7 @@ def exec_(self) -> typing.Tuple[str]:

if 'exec' in self:
try:
return tuple(template.render_str_list(self['exec']))
return tuple(template.render_str_list(self['exec'], **self._context))
except TypeError:
raise ConfTypeError(f'{self.__name__}: "exec" requires string or list of strings '
f"not: {self['exec']!r}")
Expand Down Expand Up @@ -246,7 +260,7 @@ def exec_(self) -> typing.Tuple[str]:
)

try:
return (executable, '-c', template.render_str(script))
return (executable, '-c', template.render_str(script, **self._context))
except jinja2.TemplateError as exc:
raise ConfValueError(f"{exc.__class__.__name__} @ {self.__path__}.shell: {exc}")

Expand Down Expand Up @@ -285,7 +299,7 @@ def if_(self):
target = bracket_match['expr'] if bracket_match else expression

try:
evaluation = template.eval_expr(target)
evaluation = template.eval_expr(target, **self._context)
except jinja2.TemplateError as exc:
raise ConfValueError(f"{exc.__class__.__name__} @ {self.__path__}.{key}: {exc}")

Expand Down Expand Up @@ -389,13 +403,21 @@ def result_(self, **context) -> str:
if not spec:
return ''

default_path = template.render_template(self._default_path_result, context).strip()
default_context = self._context
default_spec = self._default_path_result

default_path = template.render_template(default_spec,
default_context,
**context).strip()

if spec == self._default_path_result:
if spec == default_spec:
# no need to render default path twice
return default_path

path = template.render_template(spec, context, default=pathlib.Path(default_path)).strip()
path = template.render_template(spec,
default_context,
default=pathlib.Path(default_path),
**context).strip()

if not path:
return ''
Expand Down

0 comments on commit d985910

Please sign in to comment.