Source code for airflow.providers.openlineage.utils.asset_compat_lineage_collector
# 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__importannotationsfromimportlib.utilimportfind_spec# TODO: replace this module with common.compat provider once common.compat 1.3.0 releaseddef_get_asset_compat_hook_lineage_collector():fromairflow.lineage.hookimportget_hook_lineage_collectorcollector=get_hook_lineage_collector()ifall(getattr(collector,asset_method_name,None)forasset_method_namein("add_input_asset","add_output_asset","collected_assets")):returncollector# dataset is renamed as asset in Airflow 3.0fromfunctoolsimportwrapsfromairflow.lineage.hookimportDatasetLineageInfo,HookLineageDatasetLineageInfo.asset=DatasetLineageInfo.datasetdefrename_dataset_kwargs_as_assets_kwargs(function):@wraps(function)defwrapper(*args,**kwargs):if"asset_kwargs"inkwargs:kwargs["dataset_kwargs"]=kwargs.pop("asset_kwargs")if"asset_extra"inkwargs:kwargs["dataset_extra"]=kwargs.pop("asset_extra")returnfunction(*args,**kwargs)returnwrappercollector.create_asset=rename_dataset_kwargs_as_assets_kwargs(collector.create_dataset)collector.add_input_asset=rename_dataset_kwargs_as_assets_kwargs(collector.add_input_dataset)collector.add_output_asset=rename_dataset_kwargs_as_assets_kwargs(collector.add_output_dataset)defcollected_assets_compat(collector)->HookLineage:"""Get the collected hook lineage information."""lineage=collector.collected_datasetsreturnHookLineage([DatasetLineageInfo(dataset=item.dataset,count=item.count,context=item.context)foriteminlineage.inputs],[DatasetLineageInfo(dataset=item.dataset,count=item.count,context=item.context)foriteminlineage.outputs],)setattr(collector.__class__,"collected_assets",property(lambdacollector:collected_assets_compat(collector)),)returncollector
[docs]defget_hook_lineage_collector():# HookLineageCollector added in 2.10try:iffind_spec("airflow.assets"):# Dataset has been renamed as Asset in 3.0fromairflow.lineage.hookimportget_hook_lineage_collectorreturnget_hook_lineage_collector()return_get_asset_compat_hook_lineage_collector()exceptImportError:classNoOpCollector:""" NoOpCollector is a hook lineage collector that does nothing. It is used when you want to disable lineage collection. """defadd_input_asset(self,*_,**__):passdefadd_output_asset(self,*_,**__):passreturnNoOpCollector()