diff --git a/docs/source/conf.py b/docs/source/conf.py index 2cba9363..54908e49 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -7,12 +7,9 @@ # -- Import and path setup --------------------------------------------------- import datetime -import json import os import sys -import yaml - on_rtd = os.environ.get("READTHEDOCS") == "True" if on_rtd: @@ -30,6 +27,16 @@ else: source_branch = "main" +# Branch for upstream earthkit repo (used for fetching earthkit-packages.yml) +# Tags will use main +if rtd_version_type in ("tag"): + ek_branch = "main" +# Pull requests and unknmown versions will use develop +# Not sure how you get unknown, but its a valid value of rtd_version_type +elif rtd_version_type in ("external", "unknown"): + ek_branch = "develop" +else: + ek_branch = rtd_version sys.path.insert(0, os.path.abspath("../../src")) sys.path.insert(0, os.path.abspath("./")) @@ -235,21 +242,9 @@ } -def _write_earthkit_packages_js(app): - """Read earthkit-packages.yml and write a JS data file into the output _static dir.""" - config_path = os.path.join(os.path.dirname(__file__), "earthkit-packages.yml") - with open(config_path, encoding="utf-8") as fh: - config = yaml.safe_load(fh) - packages = config.get("packages", []) - static_dir = os.path.join(app.outdir, "_static") - os.makedirs(static_dir, exist_ok=True) - js_path = os.path.join(static_dir, "earthkit-packages.js") - with open(js_path, "w", encoding="utf-8") as fh: - fh.write(f"window.earthkitPackages = {json.dumps(packages)};\n") - - def setup(app): + from earthkit_packages import _write_earthkit_packages_js from skip_api_rules import _skip_api_items - app.connect("builder-inited", _write_earthkit_packages_js) + app.connect("builder-inited", lambda app: _write_earthkit_packages_js(app, ek_branch)) app.connect("autoapi-skip-member", _skip_api_items) diff --git a/docs/source/earthkit_packages.py b/docs/source/earthkit_packages.py new file mode 100644 index 00000000..b5952b86 --- /dev/null +++ b/docs/source/earthkit_packages.py @@ -0,0 +1,43 @@ +# (C) Copyright 2021 ECMWF. +# +# This software is licensed under the terms of the Apache Licence Version 2.0 +# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. +# In applying this licence, ECMWF does not waive the privileges and immunities +# granted to it by virtue of its status as an intergovernmental organisation +# nor does it submit to any jurisdiction. +# + +import json +import os +import urllib.request + +import yaml + + +def _write_earthkit_packages_js(app, ek_branch): + """Fetch earthkit-packages.yml from remote and write a JS data file into the output _static dir. + + Falls back to the local copy if the remote fetch fails. + """ + urls = ( + f"https://raw.githubusercontent.com/ecmwf/earthkit/refs/heads/{ek_branch}/docs/earthkit-packages.yml", + "https://raw.githubusercontent.com/ecmwf/earthkit/refs/heads/main/docs/earthkit-packages.yml", + "https://raw.githubusercontent.com/ecmwf/earthkit/refs/heads/develop/docs/earthkit-packages.yml", + ) + + for url in urls: + try: + with urllib.request.urlopen(url, timeout=10) as response: + config = yaml.safe_load(response.read()) + break + except Exception: + continue + else: + raise RuntimeError("Failed to fetch earthkit-packages.yml from remote URLs.") + + packages = config.get("packages", []) + static_dir = os.path.join(app.outdir, "_static") + os.makedirs(static_dir, exist_ok=True) + js_path = os.path.join(static_dir, "earthkit-packages.js") + with open(js_path, "w", encoding="utf-8") as fh: + fh.write(f"window.earthkitPackages = {json.dumps(packages)};\n")