-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathfetch_docs_urls.py
More file actions
71 lines (60 loc) · 2.76 KB
/
fetch_docs_urls.py
File metadata and controls
71 lines (60 loc) · 2.76 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
61
62
63
64
65
66
67
68
69
70
71
import posixpath
import requests
from blessings import Terminal
from django.conf import settings
from django.core.management.base import BaseCommand
from sphinx.util.inventory import InventoryFile
from cbv.models import Klass, ProjectVersion
t = Terminal()
class Command(BaseCommand):
args = ""
help = "Fetches the docs urls for CBV Classes."
# Django no longer hosts docs for < 1.7, so we only want versions that
# are both in CCBV and at least as recent as 1.7
django_versions = ProjectVersion.objects.exclude(
sortable_version_number__lt="0107"
).values_list("version_number", flat=True)
# Django has custom inventory file name
inv_filename = "_objects"
def bless_prints(self, version, msg):
# wish the blessings lib supports method chaining..
a = t.blue(f"Django {version}: ")
z = t.green(msg)
print(a + z)
def handle(self, *args, **options):
"""
Docs urls for Classes can differ between Django versions.
This script sets correct urls for specific Classes using bits from
`sphinx.ext.intersphinx` to fetch docs inventory data.
"""
for v in self.django_versions:
cnt = 1
ver_url = f"https://docs.djangoproject.com/en/{v}"
ver_inv_url = ver_url + "/" + self.inv_filename
# get flat list of CBV classes per Django version
qs_lookups = {"module__project_version__version_number": v}
ver_classes = Klass.objects.filter(**qs_lookups).values_list(
"name", flat=True
)
self.bless_prints(v, f"Found {len(ver_classes)} classes")
self.bless_prints(v, f"Getting inventory @ {ver_inv_url}")
# fetch some inventory dataz
# the arg `r.raw` should be a Sphinx instance object..
r = requests.get(ver_inv_url, stream=True)
r.raise_for_status()
invdata = InventoryFile.load(r.raw, ver_url, posixpath.join)
# we only want classes..
for item in invdata["py:class"]:
# ..which come from one of our sources
if any(source in item for source in settings.CBV_SOURCES.keys()):
# get class name
inv_klass = item.split(".")[-1]
# save hits to db and update only required classes
for vc in ver_classes:
if vc == inv_klass:
url = invdata["py:class"][item][2]
qs_lookups.update({"name": inv_klass})
Klass.objects.filter(**qs_lookups).update(docs_url=url)
cnt += 1
continue
self.bless_prints(v, f"Updated {cnt} classes\n")