forked from python-visualization/folium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate_switcher.py
62 lines (52 loc) · 2.21 KB
/
update_switcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
This script is used to update switcher.json on docs releases. It adds the new version to
the list of versions and sets the latest version to the new version.
"""
import argparse
import json
import os
def main():
# Define CLI arguments
parser = argparse.ArgumentParser(description="Update switcher.json")
parser.add_argument(
"--version", "-v", required=True, type=str, help="The new version to add"
)
args = parser.parse_args()
# drop the "v" prefix
new_version_without_prefix = args.version[1:]
# Setup path to switcher.json (relative to this script) and load it
switcher_path = os.path.join(os.path.dirname(__file__), "_static", "switcher.json")
with open(switcher_path) as f:
switcher = json.load(f)
# first we get the version number of the previous version
for i, version in enumerate(switcher):
if version.get("name", "").startswith("latest"):
latest_index = i
previous_version = version["version"]
if previous_version == new_version_without_prefix:
print(
f"Version {new_version_without_prefix} already is the latest version. Exiting."
)
return
# now replace the name and version of this one with the new version
switcher[i]["name"] = f"latest ({new_version_without_prefix})"
switcher[i]["version"] = new_version_without_prefix
break
else:
raise ValueError("'latest' version not found in switcher.json")
# Add the previous version to the list of versions (we always insert it after latest)
if any(version["version"] == previous_version for version in switcher):
print(
f"Previous version {previous_version} already exists in switcher.json. Not adding it again."
)
else:
previous_version_entry = {
"version": previous_version,
"url": f"https://python-visualization.github.io/folium/v{previous_version}/",
}
switcher.insert(latest_index + 1, previous_version_entry)
# Write the updated switcher.json
with open(switcher_path, "w") as f:
json.dump(switcher, f, indent=2)
if __name__ == "__main__":
main()