# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations
from collections.abc import Sequence
from typing import TYPE_CHECKING, Any
from airflow._shared.timezones import timezone
from airflow.timetables.base import DagRunInfo, DataInterval, Timetable
if TYPE_CHECKING:
    from pendulum import DateTime
    from airflow.sdk.definitions.asset import BaseAsset
    from airflow.timetables.base import TimeRestriction
    from airflow.utils.types import DagRunType
class _TrivialTimetable(Timetable):
    """Some code reuse for "trivial" timetables that has nothing complex."""
    periodic = False
    run_ordering: Sequence[str] = ("logical_date",)
    @classmethod
    def deserialize(cls, data: dict[str, Any]) -> Timetable:
        return cls()
    def __eq__(self, other: object) -> bool:
        """
        As long as *other* is of the same type.
        This is only for testing purposes and should not be relied on otherwise.
        """
        if not isinstance(other, type(self)):
            return NotImplemented
        return True
    def serialize(self) -> dict[str, Any]:
        return {}
    def infer_manual_data_interval(self, *, run_after: DateTime) -> DataInterval:
        return DataInterval.exact(run_after)
class AssetTriggeredTimetable(_TrivialTimetable):
    """
    Timetable that never schedules anything.
    This should not be directly used anywhere, but only set if a DAG is triggered by assets.
    :meta private:
    """
    description: str = "Triggered by assets"
    def __init__(self, assets: BaseAsset) -> None:
        super().__init__()
        self.asset_condition = assets
    @classmethod
    def deserialize(cls, data: dict[str, Any]) -> Timetable:
        from airflow.serialization.serialized_objects import decode_asset_condition
        return cls(decode_asset_condition(data["asset_condition"]))
    @property
    def summary(self) -> str:
        return "Asset"
    def serialize(self) -> dict[str, Any]:
        from airflow.serialization.serialized_objects import encode_asset_condition
        return {"asset_condition": encode_asset_condition(self.asset_condition)}
    def generate_run_id(
        self,
        *,
        run_type: DagRunType,
        data_interval: DataInterval | None,
        run_after: DateTime,
        **extra,
    ) -> str:
        """
        Generate Run ID based on Run Type, run_after and logical Date.
        :param run_type: type of DagRun
        :param data_interval: the data interval
        :param run_after: the date before which dag run won't start.
        """
        from airflow.models.dagrun import DagRun
        logical_date = data_interval.start if data_interval is not None else run_after
        return DagRun.generate_run_id(run_type=run_type, logical_date=logical_date, run_after=run_after)
    def next_dagrun_info(
        self,
        *,
        last_automated_data_interval: DataInterval | None,
        restriction: TimeRestriction,
    ) -> DagRunInfo | None:
        return None