forked from python-visualization/folium
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate_switcher.py
More file actions
60 lines (50 loc) · 2.05 KB
/
update_switcher.py
File metadata and controls
60 lines (50 loc) · 2.05 KB
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
"""
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
import re
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()
# 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["version"] == "latest":
latest_index = i
previous_version = re.search(
r"latest \(([v.\d]+)\)", version["name"]
).group(1)
if previous_version == args.version:
print(f"Version {args.version} already is the latest version. Exiting.")
return
# now replace the name of this one with the new version
switcher[i]["name"] = f"latest ({args.version})"
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/{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()