Skip to content

Commit 6ff4d4d

Browse files
committed
Make use of Pathlib's "/"
It provides a cleaner and easier to read experience, comparing to .joinpath()
1 parent e81dd71 commit 6ff4d4d

File tree

10 files changed

+41
-53
lines changed

10 files changed

+41
-53
lines changed

djangoproject/settings/common.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
)
1616

1717
try:
18-
with DATA_DIR.joinpath("conf", "secrets.json").open() as handle:
18+
with (DATA_DIR / "conf" / "secrets.json").open() as handle:
1919
SECRETS = json.load(handle)
2020
except OSError:
2121
SECRETS = {
@@ -54,7 +54,7 @@
5454
DEFAULT_FROM_EMAIL = "noreply@djangoproject.com"
5555
FUNDRAISING_DEFAULT_FROM_EMAIL = "fundraising@djangoproject.com"
5656

57-
FIXTURE_DIRS = [str(PROJECT_PACKAGE.joinpath("fixtures"))]
57+
FIXTURE_DIRS = [PROJECT_PACKAGE / "fixtures"]
5858

5959
INSTALLED_APPS = [
6060
"accounts",
@@ -184,7 +184,7 @@
184184

185185
SITE_ID = 1
186186

187-
STATICFILES_DIRS = [str(PROJECT_PACKAGE.joinpath("static"))]
187+
STATICFILES_DIRS = [PROJECT_PACKAGE / "static"]
188188

189189
STATIC_URL = "/s/"
190190

@@ -196,7 +196,7 @@
196196
TEMPLATES = [
197197
{
198198
"BACKEND": "django.template.backends.django.DjangoTemplates",
199-
"DIRS": [str(PROJECT_PACKAGE.joinpath("templates"))],
199+
"DIRS": [PROJECT_PACKAGE / "templates"],
200200
"APP_DIRS": True,
201201
"OPTIONS": {
202202
"builtins": [

djangoproject/settings/dev.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727

2828
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
2929

30-
MEDIA_ROOT = str(DATA_DIR.joinpath("media_root"))
30+
MEDIA_ROOT = DATA_DIR / "media_root"
3131

3232
SESSION_COOKIE_SECURE = False
3333

34-
STATIC_ROOT = str(DATA_DIR.joinpath("static_root"))
34+
STATIC_ROOT = DATA_DIR / "static_root"
3535

3636
# Docs settings
37-
DOCS_BUILD_ROOT = DATA_DIR.joinpath("djangodocs")
37+
DOCS_BUILD_ROOT = DATA_DIR / "djangodocs"
3838

3939
# django-hosts settings
4040

djangoproject/settings/prod.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
}
4141
LOGGING["loggers"]["django.request"]["handlers"].append("syslog")
4242

43-
MEDIA_ROOT = str(DATA_DIR.joinpath("media"))
43+
MEDIA_ROOT = DATA_DIR / "media"
4444

4545
MEDIA_URL = f"https://media.{DOMAIN_NAME}/"
4646

@@ -61,12 +61,12 @@
6161
},
6262
}
6363

64-
STATIC_ROOT = str(DATA_DIR.joinpath("static"))
64+
STATIC_ROOT = DATA_DIR / "static"
6565

6666
STATIC_URL = f"https://static.{DOMAIN_NAME}/"
6767

6868
# Docs settings
69-
DOCS_BUILD_ROOT = DATA_DIR.joinpath("data", "docbuilds")
69+
DOCS_BUILD_ROOT = DATA_DIR / "data" / "docbuilds"
7070

7171
# django-hosts settings
7272

docs/management/commands/update_docs.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,8 @@ def build_doc_release(self, release, force=False, interactive=False):
135135
self.stdout.write(f"Starting update for {release} at {datetime.now()}...")
136136

137137
# checkout_dir is shared for all languages.
138-
checkout_dir = settings.DOCS_BUILD_ROOT.joinpath("sources", release.version)
139-
parent_build_dir = settings.DOCS_BUILD_ROOT.joinpath(
140-
release.lang, release.version
141-
)
138+
checkout_dir = settings.DOCS_BUILD_ROOT / "sources" / release.version
139+
parent_build_dir = settings.DOCS_BUILD_ROOT / release.lang / release.version
142140
if not checkout_dir.exists():
143141
checkout_dir.mkdir(parents=True)
144142
if not parent_build_dir.exists():
@@ -161,20 +159,21 @@ def build_doc_release(self, release, force=False, interactive=False):
161159
)
162160
return
163161

164-
source_dir = checkout_dir.joinpath("docs")
162+
source_dir = checkout_dir / "docs"
165163

166164
if release.lang != settings.DEFAULT_LANGUAGE_CODE:
167165
scm_url = release.scm_url.replace(
168166
"django.git", "django-docs-translations.git"
169167
)
170-
trans_dir = checkout_dir.joinpath("django-docs-translation")
168+
trans_dir = checkout_dir / "django-docs-translation"
171169
if not trans_dir.exists():
172170
trans_dir.mkdir()
173171
self.update_git(scm_url, trans_dir)
174-
if not source_dir.joinpath("locale").exists():
175-
source_dir.joinpath("locale").symlink_to(
176-
trans_dir.joinpath("translations")
177-
)
172+
173+
locale_dir = source_dir / "locale"
174+
if not locale_dir.exists():
175+
locale_dir.symlink_to(trans_dir / "translations")
176+
178177
extra_kwargs = {"stdout": subprocess.DEVNULL} if self.verbosity == 0 else {}
179178
subprocess.check_call(
180179
"cd %s && make translations" % trans_dir, shell=True, **extra_kwargs
@@ -191,7 +190,7 @@ def build_doc_release(self, release, force=False, interactive=False):
191190
#
192191
for builder in builders:
193192
# Wipe and re-create the build directory. See #18930.
194-
build_dir = parent_build_dir.joinpath("_build", builder)
193+
build_dir = parent_build_dir / "_build" / builder
195194
if build_dir.exists():
196195
shutil.rmtree(str(build_dir))
197196
build_dir.mkdir(parents=True)
@@ -209,7 +208,7 @@ def build_doc_release(self, release, force=False, interactive=False):
209208
srcdir=source_dir,
210209
confdir=source_dir,
211210
outdir=build_dir,
212-
doctreedir=build_dir.joinpath(".doctrees"),
211+
doctreedir=build_dir / ".doctrees",
213212
buildername=builder,
214213
# Translated docs builds generate a lot of warnings, so send
215214
# stderr to stdout to be logged (rather than generating an email)
@@ -234,9 +233,9 @@ def build_doc_release(self, release, force=False, interactive=False):
234233
# Create a zip file of the HTML build for offline reading.
235234
# This gets moved into MEDIA_ROOT for downloading.
236235
#
237-
html_build_dir = parent_build_dir.joinpath("_build", "djangohtml")
236+
html_build_dir = parent_build_dir / "_build" / "djangohtml"
238237
zipfile_name = f"django-docs-{release.version}-{release.lang}.zip"
239-
zipfile_path = Path(settings.MEDIA_ROOT).joinpath("docs", zipfile_name)
238+
zipfile_path = settings.MEDIA_ROOT / "docs" / zipfile_name
240239
if not zipfile_path.parent.exists():
241240
zipfile_path.parent.mkdir(parents=True)
242241
if self.verbosity >= 2:
@@ -259,8 +258,8 @@ def zipfile_inclusion_filter(file_path):
259258
# Copy the build results to the directory used for serving
260259
# the documentation in the least disruptive way possible.
261260
#
262-
build_dir = parent_build_dir.joinpath("_build")
263-
built_dir = parent_build_dir.joinpath("_built")
261+
build_dir = parent_build_dir / "_build"
262+
built_dir = parent_build_dir / "_built"
264263
subprocess.check_call(
265264
[
266265
"rsync",
@@ -275,7 +274,7 @@ def zipfile_inclusion_filter(file_path):
275274
if release.is_default:
276275
self._setup_stable_symlink(release, built_dir)
277276

278-
json_built_dir = parent_build_dir.joinpath("_built", "json")
277+
json_built_dir = parent_build_dir / "_built" / "json"
279278
documents = gen_decoded_documents(json_built_dir)
280279
release.sync_to_db(documents)
281280

@@ -289,7 +288,7 @@ def update_git(self, url, destdir, changed_dir="."):
289288
repo, branch = url.rsplit("@", 1)
290289
else:
291290
repo, branch = url, "main"
292-
if destdir.joinpath(".git").exists():
291+
if (destdir / ".git").exists():
293292
remote = "origin"
294293
branch_with_remote = f"{remote}/{branch}"
295294
try:

docs/models.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,8 @@ def sync_to_db(self, decoded_documents):
188188
self.documents.all().delete()
189189

190190
# Read excluded paths from robots.docs.txt.
191-
robots_path = settings.BASE_DIR.joinpath(
192-
"djangoproject", "static", "robots.docs.txt"
193-
)
194-
with open(str(robots_path)) as fh:
191+
robots_path = settings.BASE_DIR / "djangoproject" / "static" / "robots.docs.txt"
192+
with robots_path.open() as fh:
195193
excluded_paths = [
196194
line.strip().split("/")[-1]
197195
for line in fh
@@ -472,6 +470,6 @@ def full_path(self):
472470
@cached_property
473471
def body(self):
474472
"""The document's body"""
475-
with open(str(self.full_path)) as fp:
473+
with self.full_path.open() as fp:
476474
doc = json.load(fp)
477475
return doc["body"]

docs/tests/test_models.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,8 @@ def test_excluded_documents(self):
570570
from robots indexing.
571571
"""
572572
# Read the first Disallow line of robots.txt.
573-
robots_path = settings.BASE_DIR.joinpath(
574-
"djangoproject", "static", "robots.docs.txt"
575-
)
576-
with open(str(robots_path)) as fh:
573+
robots_path = settings.BASE_DIR / "djangoproject" / "static" / "robots.docs.txt"
574+
with robots_path.open() as fh:
577575
for line in fh:
578576
if line.startswith("Disallow:"):
579577
break

docs/tests/test_templates.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,8 @@ def test_get_all_doc_versions_empty(self):
3030
def test_get_all_doc_versions(self):
3131
tmp_docs_build_root = Path(tempfile.mkdtemp())
3232
self.addCleanup(shutil.rmtree, tmp_docs_build_root)
33-
os.makedirs(
34-
tmp_docs_build_root.joinpath(
35-
settings.DEFAULT_LANGUAGE_CODE, "1.8", "_built", "json"
36-
)
37-
)
38-
os.makedirs(
39-
tmp_docs_build_root.joinpath(
40-
settings.DEFAULT_LANGUAGE_CODE, "1.11", "_built", "json"
41-
)
42-
)
33+
os.makedirs(tmp_docs_build_root / settings.DEFAULT_LANGUAGE_CODE / "1.8" / "_built" / "json")
34+
os.makedirs(tmp_docs_build_root / settings.DEFAULT_LANGUAGE_CODE / "1.11" / "_built" / "json")
4335
with self.settings(DOCS_BUILD_ROOT=tmp_docs_build_root):
4436
self.assertEqual(get_all_doc_versions({}), ["1.8", "1.11", "dev"])
4537

docs/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import re
22
import unicodedata
3+
from pathlib import Path
34

45
from django.conf import settings
56
from django.http import Http404
67

78

89
def get_doc_root(lang, version, builder="json"):
9-
return settings.DOCS_BUILD_ROOT.joinpath(lang, version, "_built", builder)
10+
return settings.DOCS_BUILD_ROOT / lang / version / "_built" / builder
1011

1112

1213
def get_doc_root_or_404(lang, version, builder="json"):
@@ -22,15 +23,15 @@ def get_doc_path(docroot, subpath):
2223
bits = subpath.strip("/").split("/") + ["index.fjson"]
2324
except AttributeError:
2425
bits = []
25-
doc = docroot.joinpath(*bits)
26+
doc = docroot / Path(*bits)
2627
try:
2728
if doc.exists():
2829
return doc
2930
except NotADirectoryError:
3031
pass # we get here if doc + subpath (without /index.fjson) is a file
3132

3233
bits = bits[:-2] + ["%s.fjson" % bits[-2]]
33-
doc = docroot.joinpath(*bits)
34+
doc = docroot / Path(*bits)
3435
if doc.exists():
3536
return doc
3637

docs/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def load_json_file(path):
8181

8282
context = {
8383
"doc": load_json_file(doc_path),
84-
"env": load_json_file(docroot.joinpath("globalcontext.json")),
84+
"env": load_json_file(docroot / "globalcontext.json"),
8585
"lang": lang,
8686
"version": version,
8787
"canonical_version": canonical_version,
@@ -91,7 +91,7 @@ def load_json_file(path):
9191
"rtd_version": rtd_version,
9292
"docurl": url,
9393
"update_date": datetime.datetime.fromtimestamp(
94-
(docroot.joinpath("last_build")).stat().st_mtime
94+
(docroot / "last_build").stat().st_mtime
9595
),
9696
"redirect_from": request.GET.get("from", None),
9797
}

fundraising/tests/test_views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def setUp(self):
262262
)
263263

264264
def stripe_data(self, filename):
265-
file_path = settings.BASE_DIR.joinpath(f"fundraising/test_data/{filename}.json")
265+
file_path = settings.BASE_DIR / f"fundraising/test_data/{filename}.json"
266266
with file_path.open() as f:
267267
data = json.load(f)
268268
return stripe.convert_to_stripe_object(data, stripe.api_key, None)

0 commit comments

Comments
 (0)