docs.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from __future__ import annotations
  18. import sys
  19. if sys.version_info >= (3, 10):
  20. from importlib import metadata
  21. else:
  22. import importlib_metadata as metadata # type: ignore[no-redef]
  23. def get_docs_url(page: str | None = None) -> str:
  24. """Prepare link to Airflow documentation."""
  25. from airflow.version import version
  26. if any(suffix in version for suffix in ["dev", "a", "b"]):
  27. result = (
  28. "http://apache-airflow-docs.s3-website.eu-central-1.amazonaws.com/docs/apache-airflow/stable/"
  29. )
  30. else:
  31. result = f"https://airflow.apache.org/docs/apache-airflow/{version}/"
  32. if page:
  33. result = result + page
  34. return result
  35. def get_project_url_from_metadata(provider_name: str):
  36. """Return the Project-URL from metadata."""
  37. return metadata.metadata(provider_name).get_all("Project-URL")
  38. def get_doc_url_for_provider(provider_name: str, provider_version: str) -> str:
  39. """Prepare link to Airflow Provider documentation."""
  40. try:
  41. from urllib.parse import urlparse
  42. metadata_items = get_project_url_from_metadata(provider_name)
  43. if isinstance(metadata_items, str):
  44. metadata_items = [metadata_items]
  45. if metadata_items:
  46. for item in metadata_items:
  47. if item.lower().startswith("documentation"):
  48. _, _, url = item.partition(",")
  49. parsed_url = urlparse(url)
  50. if url and (parsed_url.scheme in ("http", "https") and bool(parsed_url.netloc)):
  51. return url.strip()
  52. except metadata.PackageNotFoundError:
  53. pass
  54. # Fallback if provider is apache one
  55. if provider_name.startswith("apache-airflow"):
  56. return f"https://airflow.apache.org/docs/{provider_name}/{provider_version}/"
  57. return "https://airflow.apache.org/docs/apache-airflow-providers/index.html#creating-your-own-providers"