-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteel_toes.py
99 lines (79 loc) · 3.28 KB
/
steel_toes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""Steel Toes Kedro Hook.
A hook that changes the filepath of your data based on current working git branch.
Example:
Since steel_toes requires access to the project_context to work properly you will
need to use a bit of an unconventional method to initialize your hooks.
>>> from steel_toes import SteelToes
>>> class ProjectContext(KedroContext):
... project_name = "kedro0160"
... project_version = "0.16.1"
... package_name = "kedro0160"
>>> @property
>>> def hooks(self):
... self._hooks = [ SteelToes(self) ]
... return self._hooks
"""
from typing import Any, Dict, Union
from kedro.framework.context import KedroContext
from kedro.framework.hooks import hook_impl
from kedro.io.data_catalog import DataCatalog
from kedro.pipeline import Pipeline
from .core import announce_protection, get_current_branch, inject_branch
class SteelToes:
"""Steel Toes Kedro Hook.
A hook that changes the filepath of your data based on current working git branch.
Arguments:
context (KedroContext): ProjectContext for your kedro project
announce (bool): Announces protected datasets on startup. Default False
Example:
since steel_toes requires access to the project_context to work properly you will
need to use a bit of an unconventional method to initialize your hooks.
>>> from steel_toes import SteelToes
>>> class ProjectContext(KedroContext):
... project_name = "kedro0160"
... project_version = "0.16.1"
... package_name = "kedro0160"
>>> @property
>>> def hooks(self):
... self._hooks = [ SteelToes(self) ]
... return self._hooks
"""
def __init__(
self,
context: KedroContext,
branch: Union[str, None] = None,
announce: bool = False,
) -> None:
"""Initialize a steel_toes kedro hook instance."""
self.context = context
project_path = str(self.context.project_path)
if branch is None:
branch = get_current_branch(
project_path
# self.context.project_path
) # pragma: no cover
if branch is None: # pragma: no cover
# branch is not mocked
self.branch = ""
else:
self.branch = branch
self.announce = announce
@hook_impl
def before_pipeline_run(self, pipeline: Pipeline, catalog: DataCatalog) -> None:
"""Inject branch information `before_pipeline_run` if the dataset exists."""
for dataset in pipeline.all_inputs():
inject_branch(self.branch, catalog, dataset)
@hook_impl
def after_catalog_created(self, catalog: DataCatalog) -> None:
"""Inject branch information `after_catalog_created` if the dataset exists."""
for dataset in catalog.list():
inject_branch(self.branch, catalog, dataset)
if self.announce:
announce_protection(catalog)
@hook_impl
def after_node_run(self, catalog: DataCatalog, outputs: Dict[str, Any]) -> None:
"""Inject branch information `after_node_run`.
On first run of a branch it will create this will create the dataset
"""
for output in outputs:
inject_branch(self.branch, catalog, output, save_mode=True)