Skip to content

Commit 6697528

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

File tree

10 files changed

+39
-43
lines changed

10 files changed

+39
-43
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 = "[email protected]"
5555
FUNDRAISING_DEFAULT_FROM_EMAIL = "[email protected]"
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
@@ -133,10 +133,8 @@ def build_doc_release(self, release, force=False, interactive=False):
133133
self.stdout.write(f"Starting update for {release} at {datetime.now()}...")
134134

135135
# checkout_dir is shared for all languages.
136-
checkout_dir = settings.DOCS_BUILD_ROOT.joinpath("sources", release.version)
137-
parent_build_dir = settings.DOCS_BUILD_ROOT.joinpath(
138-
release.lang, release.version
139-
)
136+
checkout_dir = settings.DOCS_BUILD_ROOT / "sources" / release.version
137+
parent_build_dir = settings.DOCS_BUILD_ROOT / release.lang / release.version
140138
if not checkout_dir.exists():
141139
checkout_dir.mkdir(parents=True)
142140
if not parent_build_dir.exists():
@@ -159,20 +157,21 @@ def build_doc_release(self, release, force=False, interactive=False):
159157
)
160158
return
161159

162-
source_dir = checkout_dir.joinpath("docs")
160+
source_dir = checkout_dir / "docs"
163161

164162
if release.lang != "en":
165163
scm_url = release.scm_url.replace(
166164
"django.git", "django-docs-translations.git"
167165
)
168-
trans_dir = checkout_dir.joinpath("django-docs-translation")
166+
trans_dir = checkout_dir / "django-docs-translation"
169167
if not trans_dir.exists():
170168
trans_dir.mkdir()
171169
self.update_git(scm_url, trans_dir)
172-
if not source_dir.joinpath("locale").exists():
173-
source_dir.joinpath("locale").symlink_to(
174-
trans_dir.joinpath("translations")
175-
)
170+
171+
locale_dir = source_dir / "locale"
172+
if not locale_dir.exists():
173+
locale_dir.symlink_to(trans_dir / "translations")
174+
176175
extra_kwargs = {"stdout": subprocess.DEVNULL} if self.verbosity == 0 else {}
177176
subprocess.check_call(
178177
"cd %s && make translations" % trans_dir, shell=True, **extra_kwargs
@@ -189,7 +188,7 @@ def build_doc_release(self, release, force=False, interactive=False):
189188
#
190189
for builder in builders:
191190
# Wipe and re-create the build directory. See #18930.
192-
build_dir = parent_build_dir.joinpath("_build", builder)
191+
build_dir = parent_build_dir / "_build" / builder
193192
if build_dir.exists():
194193
shutil.rmtree(str(build_dir))
195194
build_dir.mkdir(parents=True)
@@ -207,7 +206,7 @@ def build_doc_release(self, release, force=False, interactive=False):
207206
srcdir=source_dir,
208207
confdir=source_dir,
209208
outdir=build_dir,
210-
doctreedir=build_dir.joinpath(".doctrees"),
209+
doctreedir=build_dir / ".doctrees",
211210
buildername=builder,
212211
# Translated docs builds generate a lot of warnings, so send
213212
# stderr to stdout to be logged (rather than generating an email)
@@ -232,9 +231,9 @@ def build_doc_release(self, release, force=False, interactive=False):
232231
# Create a zip file of the HTML build for offline reading.
233232
# This gets moved into MEDIA_ROOT for downloading.
234233
#
235-
html_build_dir = parent_build_dir.joinpath("_build", "djangohtml")
234+
html_build_dir = parent_build_dir / "_build" / "djangohtml"
236235
zipfile_name = f"django-docs-{release.version}-{release.lang}.zip"
237-
zipfile_path = Path(settings.MEDIA_ROOT).joinpath("docs", zipfile_name)
236+
zipfile_path = settings.MEDIA_ROOT / "docs" / zipfile_name
238237
if not zipfile_path.parent.exists():
239238
zipfile_path.parent.mkdir(parents=True)
240239
if self.verbosity >= 2:
@@ -257,8 +256,8 @@ def zipfile_inclusion_filter(file_path):
257256
# Copy the build results to the directory used for serving
258257
# the documentation in the least disruptive way possible.
259258
#
260-
build_dir = parent_build_dir.joinpath("_build")
261-
built_dir = parent_build_dir.joinpath("_built")
259+
build_dir = parent_build_dir / "_build"
260+
built_dir = parent_build_dir / "_built"
262261
subprocess.check_call(
263262
[
264263
"rsync",
@@ -273,7 +272,7 @@ def zipfile_inclusion_filter(file_path):
273272
if release.is_default:
274273
self._setup_stable_symlink(release, built_dir)
275274

276-
json_built_dir = parent_build_dir.joinpath("_built", "json")
275+
json_built_dir = parent_build_dir / "_built" / "json"
277276
documents = gen_decoded_documents(json_built_dir)
278277
release.sync_to_db(documents)
279278

@@ -287,7 +286,7 @@ def update_git(self, url, destdir, changed_dir="."):
287286
repo, branch = url.rsplit("@", 1)
288287
else:
289288
repo, branch = url, "main"
290-
if destdir.joinpath(".git").exists():
289+
if (destdir / ".git").exists():
291290
remote = "origin"
292291
branch_with_remote = f"{remote}/{branch}"
293292
try:

docs/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,7 @@ def sync_to_db(self, decoded_documents):
178178
self.documents.all().delete()
179179

180180
# Read excluded paths from robots.docs.txt.
181-
robots_path = settings.BASE_DIR.joinpath(
182-
"djangoproject", "static", "robots.docs.txt"
183-
)
181+
robots_path = settings.BASE_DIR / "djangoproject" / "static" / "robots.docs.txt"
184182
with open(str(robots_path)) as fh:
185183
excluded_paths = [
186184
line.strip().split("/")[-1]

docs/tests/test_models.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,8 @@ def test_excluded_documents(self):
526526
from robots indexing.
527527
"""
528528
# Read the first Disallow line of robots.txt.
529-
robots_path = settings.BASE_DIR.joinpath(
530-
"djangoproject", "static", "robots.docs.txt"
531-
)
532-
with open(str(robots_path)) as fh:
529+
robots_path = settings.BASE_DIR / "djangoproject" / "static" / "robots.docs.txt"
530+
with robots_path.open() as fh:
533531
for line in fh:
534532
if line.startswith("Disallow:"):
535533
break

docs/tests/test_templates.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def test_get_all_doc_versions_empty(self):
2727
def test_get_all_doc_versions(self):
2828
tmp_docs_build_root = Path(tempfile.mkdtemp())
2929
self.addCleanup(shutil.rmtree, tmp_docs_build_root)
30-
os.makedirs(tmp_docs_build_root.joinpath("en", "1.8", "_built", "json"))
31-
os.makedirs(tmp_docs_build_root.joinpath("en", "1.11", "_built", "json"))
30+
os.makedirs(tmp_docs_build_root / "en" / "1.8" / "_built" / "json")
31+
os.makedirs(tmp_docs_build_root / "en" / "1.11" / "_built" /"json")
3232
with self.settings(DOCS_BUILD_ROOT=tmp_docs_build_root):
3333
self.assertEqual(get_all_doc_versions({}), ["1.8", "1.11", "dev"])
3434

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
@@ -260,7 +260,7 @@ def setUp(self):
260260
)
261261

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

0 commit comments

Comments
 (0)