|
| 1 | +from collections import namedtuple |
| 2 | +import functools |
| 3 | +import re |
| 4 | + |
| 5 | +from metaflow.metaflow_config import NAMESPACED_EVENTS_PREFIX |
| 6 | +from metaflow.util import is_stringish |
| 7 | + |
| 8 | +ParsedEvent = namedtuple( |
| 9 | + "ParsedEvent", |
| 10 | + ["full_name", "project", "branch", "logical_name", "is_namespaced"], |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def namespaced_event_name(event_name): |
| 15 | + """ |
| 16 | + Creates a project-namespaced event name based on @project settings. |
| 17 | +
|
| 18 | + Use this to automatically prefix event names with your @project |
| 19 | + namespace, ensuring events are isolated per project and branch. |
| 20 | +
|
| 21 | + The resolved name follows the format: |
| 22 | + mfns.<project>.<branch>.<event_name> |
| 23 | +
|
| 24 | + If no @project decorator is present, resolves to: |
| 25 | + mfns.<event_name> |
| 26 | +
|
| 27 | + Parameters |
| 28 | + ---------- |
| 29 | + event_name : str |
| 30 | + The logical event name (e.g., 'data_ready') |
| 31 | +
|
| 32 | + Returns |
| 33 | + ------- |
| 34 | + Callable[..., str] |
| 35 | + A callable that returns the fully namespaced event name (str) when invoked |
| 36 | +
|
| 37 | + Examples |
| 38 | + -------- |
| 39 | + With @trigger decorator: |
| 40 | +
|
| 41 | + ``` |
| 42 | + from metaflow import namespaced_event_name, project |
| 43 | +
|
| 44 | + @project(name="foo") |
| 45 | + @trigger(event=namespaced_event_name('data_ready')) |
| 46 | + class MyFlow(FlowSpec): |
| 47 | + ... |
| 48 | +
|
| 49 | + @project(name="foo") |
| 50 | + @trigger(event={'name': namespaced_event_name('data_ready'), 'parameters': {'x': 'y'}}) |
| 51 | + class MyFlow(FlowSpec): |
| 52 | + ``` |
| 53 | +
|
| 54 | + With ArgoEvent: |
| 55 | +
|
| 56 | + ``` |
| 57 | + from metaflow import namespaced_event_name |
| 58 | + from metaflow.integrations import ArgoEvent |
| 59 | +
|
| 60 | + ArgoEvent(namespaced_event_name('data_ready')).publish() |
| 61 | + ``` |
| 62 | + """ |
| 63 | + if event_name is None or not is_stringish(event_name): |
| 64 | + raise ValueError( |
| 65 | + "event_name should be a string not %s" % (str(type(event_name))) |
| 66 | + ) |
| 67 | + |
| 68 | + def _delayed_name_generator(context=None, event_name=None): |
| 69 | + from metaflow import current |
| 70 | + |
| 71 | + project_name = current.get("project_name", None) |
| 72 | + branch_name = current.get("branch_name", None) |
| 73 | + return _make_namespaced_event_name(event_name, project_name, branch_name) |
| 74 | + |
| 75 | + return functools.partial(_delayed_name_generator, event_name=event_name) |
| 76 | + |
| 77 | + |
| 78 | +def _make_namespaced_event_name(logical_name, project=None, branch=None): |
| 79 | + """ |
| 80 | + Construct a namespaced event name from components. |
| 81 | +
|
| 82 | + Parameters |
| 83 | + ---------- |
| 84 | + logical_name : str |
| 85 | + The base event name |
| 86 | + project : str, optional |
| 87 | + Project name from @project decorator |
| 88 | + branch : str, optional |
| 89 | + Branch name from @project decorator |
| 90 | +
|
| 91 | + Returns |
| 92 | + ------- |
| 93 | + str |
| 94 | + Namespaced event name in format mfns.<project>.<branch>.<logical_name> |
| 95 | + or mfns.<logical_name> if no project/branch |
| 96 | + """ |
| 97 | + if project and branch: |
| 98 | + # When it comes to project and branches we want to make |
| 99 | + # sure that we are not having any non-acceptable characters. |
| 100 | + # allow dot (.) in project and branch names |
| 101 | + branch = re.sub(r"[^a-zA-Z0-9_\-\.]", "", branch) |
| 102 | + project = re.sub(r"[^a-zA-Z0-9_\-\.]", "", project) |
| 103 | + return f"{NAMESPACED_EVENTS_PREFIX}.{project}.{branch}.{logical_name}" |
| 104 | + else: |
| 105 | + return f"{NAMESPACED_EVENTS_PREFIX}.{logical_name}" |
0 commit comments