potiuk commented on a change in pull request #12383: URL: https://github.com/apache/airflow/pull/12383#discussion_r524964573
########## File path: airflow/providers_manager.py ########## @@ -0,0 +1,78 @@ +# +# 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. +"""Manages all providers.""" +import importlib +import logging +import pkgutil +import traceback + +import yaml + +try: + import importlib.resources as pkg_resources +except ImportError: + # Try backported to PY<37 `importlib_resources`. + import importlib_resources as pkg_resources + + +log = logging.getLogger(__name__) + + +class ProvidersManager: + """Manages all provider packages.""" + + def __find_all_providers(self, paths: str): + def onerror(_): + exception_string = traceback.format_exc() + log.warning(exception_string) + + for module_info in pkgutil.walk_packages(paths, prefix="airflow.providers.", onerror=onerror): + try: + imported_module = importlib.import_module(module_info.name) + except Exception as e: # noqa pylint: disable=broad-except + log.warning("Error when importing %s:%s", module_info.name, e) + continue + try: + provider_info = yaml.safe_load(pkg_resources.read_text(imported_module, 'provider.yaml')) + # TODO(potiuk): map to a class maybe? Or we might stick to a dictionary + self._provider_directory[provider_info['package-name']] = provider_info + except FileNotFoundError: + # This is OK - this is not a provider package + pass + except TypeError as e: + if "is not a package" not in str(e): + log.warning("Error when loading 'provider.yaml' file from %s:%s}", module_info.name, e) + # Otherwise this is OK - this is likely a module + except Exception as e: # noqa pylint: disable=broad-except + log.warning("Error when loading 'provider.yaml' file from %s:%s", module_info.name, e) + + def __init__(self): + self._provider_directory = {} + try: + from airflow import providers + except ImportError as e: + log.warning("No providers are present or error when importing them! :%s", e) + return + self.__find_all_providers(providers.__path__) + + # TODO(potiuk) - add refresh mechanism to re-import providers and update the dictionary Review comment: Yeah. that's why I not even looked at it yet, I have not thought that through if we actually need it or not. It's certainly not something for 2.0, so I remove this comment indeed. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
