|
| 1 | +from airflow.models import DAG |
| 2 | +from airflow.operators.dummy import DummyOperator |
| 3 | +from datetime import datetime |
| 4 | +from airflow.plugins_manager import AirflowPlugin |
| 5 | +from airflow.decorators import task, get_current_context |
| 6 | +from airflow.models.baseoperator import BaseOperator |
| 7 | + |
| 8 | +@task |
| 9 | +def print_config(**context): |
| 10 | + # This should not throw an error as logical_date is part of airflow context. |
| 11 | + logical_date = context["logical_date"] |
| 12 | + |
| 13 | + # Removed usage - should trigger violations |
| 14 | + execution_date = context["execution_date"] |
| 15 | + next_ds = context["next_ds"] |
| 16 | + next_ds_nodash = context["next_ds_nodash"] |
| 17 | + next_execution_date = context["next_execution_date"] |
| 18 | + prev_ds = context["prev_ds"] |
| 19 | + prev_ds_nodash = context["prev_ds_nodash"] |
| 20 | + prev_execution_date = context["prev_execution_date"] |
| 21 | + prev_execution_date_success = context["prev_execution_date_success"] |
| 22 | + tomorrow_ds = context["tomorrow_ds"] |
| 23 | + yesterday_ds = context["yesterday_ds"] |
| 24 | + yesterday_ds_nodash = context["yesterday_ds_nodash"] |
| 25 | + |
| 26 | +with DAG( |
| 27 | + dag_id="example_dag", |
| 28 | + schedule_interval="@daily", |
| 29 | + start_date=datetime(2023, 1, 1), |
| 30 | + template_searchpath=["/templates"], |
| 31 | +) as dag: |
| 32 | + task1 = DummyOperator( |
| 33 | + task_id="task1", |
| 34 | + params={ |
| 35 | + # Removed variables in template |
| 36 | + "execution_date": "{{ execution_date }}", |
| 37 | + "next_ds": "{{ next_ds }}", |
| 38 | + "prev_ds": "{{ prev_ds }}" |
| 39 | + }, |
| 40 | + ) |
| 41 | + |
| 42 | +class CustomMacrosPlugin(AirflowPlugin): |
| 43 | + name = "custom_macros" |
| 44 | + macros = { |
| 45 | + "execution_date_macro": lambda context: context["execution_date"], |
| 46 | + "next_ds_macro": lambda context: context["next_ds"] |
| 47 | + } |
| 48 | + |
| 49 | +@task |
| 50 | +def print_config(): |
| 51 | + context = get_current_context() |
| 52 | + execution_date = context["execution_date"] |
| 53 | + next_ds = context["next_ds"] |
| 54 | + next_ds_nodash = context["next_ds_nodash"] |
| 55 | + next_execution_date = context["next_execution_date"] |
| 56 | + prev_ds = context["prev_ds"] |
| 57 | + prev_ds_nodash = context["prev_ds_nodash"] |
| 58 | + prev_execution_date = context["prev_execution_date"] |
| 59 | + prev_execution_date_success = context["prev_execution_date_success"] |
| 60 | + tomorrow_ds = context["tomorrow_ds"] |
| 61 | + yesterday_ds = context["yesterday_ds"] |
| 62 | + yesterday_ds_nodash = context["yesterday_ds_nodash"] |
| 63 | + |
| 64 | +class CustomOperator(BaseOperator): |
| 65 | + def execute(self, context): |
| 66 | + execution_date = context["execution_date"] |
| 67 | + next_ds = context["next_ds"] |
| 68 | + next_ds_nodash = context["next_ds_nodash"] |
| 69 | + next_execution_date = context["next_execution_date"] |
| 70 | + prev_ds = context["prev_ds"] |
| 71 | + prev_ds_nodash = context["prev_ds_nodash"] |
| 72 | + prev_execution_date = context["prev_execution_date"] |
| 73 | + prev_execution_date_success = context["prev_execution_date_success"] |
| 74 | + tomorrow_ds = context["tomorrow_ds"] |
| 75 | + yesterday_ds = context["yesterday_ds"] |
| 76 | + yesterday_ds_nodash = context["yesterday_ds_nodash"] |
0 commit comments