|
| 1 | +""" |
| 2 | +This script is used to update switcher.json on docs releases. It adds the new version to |
| 3 | +the list of versions and sets the latest version to the new version. |
| 4 | +""" |
| 5 | + |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +import os |
| 9 | + |
| 10 | + |
| 11 | +def main(): |
| 12 | + # Define CLI arguments |
| 13 | + parser = argparse.ArgumentParser(description="Update switcher.json") |
| 14 | + parser.add_argument( |
| 15 | + "--version", "-v", required=True, type=str, help="The new version to add" |
| 16 | + ) |
| 17 | + args = parser.parse_args() |
| 18 | + |
| 19 | + # Setup path to switcher.json (relative to this script) and load it |
| 20 | + switcher_path = os.path.join(os.path.dirname(__file__), "_static", "switcher.json") |
| 21 | + with open(switcher_path) as f: |
| 22 | + switcher = json.load(f) |
| 23 | + |
| 24 | + # Find index of 'latest' entry |
| 25 | + latest_index = None |
| 26 | + for i, version in enumerate(switcher): |
| 27 | + if version["version"] == "latest": |
| 28 | + latest_index = i |
| 29 | + break |
| 30 | + if latest_index is None: |
| 31 | + raise ValueError("'latest' version not found in switcher.json") |
| 32 | + |
| 33 | + # Add the new version to the list of versions (we always insert it after latest) |
| 34 | + new_version = { |
| 35 | + "version": args.version, |
| 36 | + "url": f"https://python-visualization.github.io/folium/{args.version}/", |
| 37 | + } |
| 38 | + |
| 39 | + # Update the latest version |
| 40 | + switcher[latest_index]["url"] = new_version["url"] |
| 41 | + |
| 42 | + # Make sure version is unique |
| 43 | + if any(version["version"] == args.version for version in switcher): |
| 44 | + print( |
| 45 | + f"Version {args.version} already exists in switcher.json. Not adding it again." |
| 46 | + ) |
| 47 | + else: |
| 48 | + switcher.insert(latest_index + 1, new_version) |
| 49 | + |
| 50 | + # Write the updated switcher.json |
| 51 | + with open(switcher_path, "w") as f: |
| 52 | + json.dump(switcher, f, indent=2) |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + main() |
0 commit comments