forked from django/djangoproject.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
41 lines (31 loc) · 1.07 KB
/
utils.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
from django.conf import settings
from django.http import Http404
def get_doc_root(lang, version, subroot="json"):
return settings.DOCS_BUILD_ROOT.joinpath(lang, version, "_built", subroot)
def get_doc_root_or_404(lang, version, subroot="json"):
docroot = get_doc_root(lang, version, subroot)
if not docroot.exists():
raise Http404(str(docroot))
return docroot
def get_doc_path(docroot, subpath):
# First look for <bits>/index.fjson, then for <bits>.fjson
try:
bits = subpath.strip("/").split("/") + ["index.fjson"]
except AttributeError:
bits = []
doc = docroot.joinpath(*bits)
try:
if doc.exists():
return doc
except NotADirectoryError:
pass # we get here if doc + subpath (without /index.fjson) is a file
bits = bits[:-2] + ["%s.fjson" % bits[-2]]
doc = docroot.joinpath(*bits)
if doc.exists():
return doc
return None
def get_doc_path_or_404(docroot, subpath):
doc = get_doc_path(docroot, subpath)
if doc is None:
raise Http404(doc)
return doc