From 46597212c3e0686ed1b127de3a6a4727c8758aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Clgen=20Sar=C4=B1kavak?= Date: Mon, 3 Nov 2025 01:17:24 +0300 Subject: [PATCH] Make use of Pathlib's "/" It provides a cleaner and easier to read experience, comparing to .joinpath() --- djangoproject/settings/common.py | 8 +++--- djangoproject/settings/dev.py | 6 ++--- djangoproject/settings/prod.py | 6 ++--- docs/management/commands/update_docs.py | 35 ++++++++++++------------- docs/models.py | 4 +-- docs/tests/test_models.py | 6 ++--- docs/tests/test_templates.py | 4 +-- docs/utils.py | 7 ++--- docs/views.py | 4 +-- fundraising/tests/test_views.py | 2 +- 10 files changed, 39 insertions(+), 43 deletions(-) diff --git a/djangoproject/settings/common.py b/djangoproject/settings/common.py index 82819c3cc0..f96e1e3774 100644 --- a/djangoproject/settings/common.py +++ b/djangoproject/settings/common.py @@ -15,7 +15,7 @@ ) try: - with DATA_DIR.joinpath("conf", "secrets.json").open() as handle: + with (DATA_DIR / "conf" / "secrets.json").open() as handle: SECRETS = json.load(handle) except OSError: SECRETS = { @@ -54,7 +54,7 @@ DEFAULT_FROM_EMAIL = "noreply@djangoproject.com" FUNDRAISING_DEFAULT_FROM_EMAIL = "fundraising@djangoproject.com" -FIXTURE_DIRS = [str(PROJECT_PACKAGE.joinpath("fixtures"))] +FIXTURE_DIRS = [PROJECT_PACKAGE / "fixtures"] INSTALLED_APPS = [ "accounts", @@ -184,7 +184,7 @@ SITE_ID = 1 -STATICFILES_DIRS = [str(PROJECT_PACKAGE.joinpath("static"))] +STATICFILES_DIRS = [PROJECT_PACKAGE / "static"] STATIC_URL = "/s/" @@ -196,7 +196,7 @@ TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", - "DIRS": [str(PROJECT_PACKAGE.joinpath("templates"))], + "DIRS": [PROJECT_PACKAGE / "templates"], "APP_DIRS": True, "OPTIONS": { "builtins": [ diff --git a/djangoproject/settings/dev.py b/djangoproject/settings/dev.py index 80c2475a31..ff0cd31dd7 100644 --- a/djangoproject/settings/dev.py +++ b/djangoproject/settings/dev.py @@ -27,14 +27,14 @@ EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" -MEDIA_ROOT = str(DATA_DIR.joinpath("media_root")) +MEDIA_ROOT = DATA_DIR / "media_root" SESSION_COOKIE_SECURE = False -STATIC_ROOT = str(DATA_DIR.joinpath("static_root")) +STATIC_ROOT = DATA_DIR / "static_root" # Docs settings -DOCS_BUILD_ROOT = DATA_DIR.joinpath("djangodocs") +DOCS_BUILD_ROOT = DATA_DIR / "djangodocs" # django-hosts settings diff --git a/djangoproject/settings/prod.py b/djangoproject/settings/prod.py index e9221d9d42..a3a4bd54f7 100644 --- a/djangoproject/settings/prod.py +++ b/djangoproject/settings/prod.py @@ -40,7 +40,7 @@ } LOGGING["loggers"]["django.request"]["handlers"].append("syslog") -MEDIA_ROOT = str(DATA_DIR.joinpath("media")) +MEDIA_ROOT = DATA_DIR / "media" MEDIA_URL = f"https://media.{DOMAIN_NAME}/" @@ -61,12 +61,12 @@ }, } -STATIC_ROOT = str(DATA_DIR.joinpath("static")) +STATIC_ROOT = DATA_DIR / "static" STATIC_URL = f"https://static.{DOMAIN_NAME}/" # Docs settings -DOCS_BUILD_ROOT = DATA_DIR.joinpath("data", "docbuilds") +DOCS_BUILD_ROOT = DATA_DIR / "data" / "docbuilds" # django-hosts settings diff --git a/docs/management/commands/update_docs.py b/docs/management/commands/update_docs.py index d1f3df0486..707b67097c 100644 --- a/docs/management/commands/update_docs.py +++ b/docs/management/commands/update_docs.py @@ -133,10 +133,8 @@ def build_doc_release(self, release, force=False, interactive=False): self.stdout.write(f"Starting update for {release} at {datetime.now()}...") # checkout_dir is shared for all languages. - checkout_dir = settings.DOCS_BUILD_ROOT.joinpath("sources", release.version) - parent_build_dir = settings.DOCS_BUILD_ROOT.joinpath( - release.lang, release.version - ) + checkout_dir = settings.DOCS_BUILD_ROOT / "sources" / release.version + parent_build_dir = settings.DOCS_BUILD_ROOT / release.lang / release.version if not checkout_dir.exists(): checkout_dir.mkdir(parents=True) if not parent_build_dir.exists(): @@ -159,20 +157,21 @@ def build_doc_release(self, release, force=False, interactive=False): ) return - source_dir = checkout_dir.joinpath("docs") + source_dir = checkout_dir / "docs" if release.lang != "en": scm_url = release.scm_url.replace( "django.git", "django-docs-translations.git" ) - trans_dir = checkout_dir.joinpath("django-docs-translation") + trans_dir = checkout_dir / "django-docs-translation" if not trans_dir.exists(): trans_dir.mkdir() self.update_git(scm_url, trans_dir) - if not source_dir.joinpath("locale").exists(): - source_dir.joinpath("locale").symlink_to( - trans_dir.joinpath("translations") - ) + + locale_dir = source_dir / "locale" + if not locale_dir.exists(): + locale_dir.symlink_to(trans_dir / "translations") + extra_kwargs = {"stdout": subprocess.DEVNULL} if self.verbosity == 0 else {} subprocess.check_call( "cd %s && make translations" % trans_dir, shell=True, **extra_kwargs @@ -189,7 +188,7 @@ def build_doc_release(self, release, force=False, interactive=False): # for builder in builders: # Wipe and re-create the build directory. See #18930. - build_dir = parent_build_dir.joinpath("_build", builder) + build_dir = parent_build_dir / "_build" / builder if build_dir.exists(): shutil.rmtree(str(build_dir)) build_dir.mkdir(parents=True) @@ -207,7 +206,7 @@ def build_doc_release(self, release, force=False, interactive=False): srcdir=source_dir, confdir=source_dir, outdir=build_dir, - doctreedir=build_dir.joinpath(".doctrees"), + doctreedir=build_dir / ".doctrees", buildername=builder, # Translated docs builds generate a lot of warnings, so send # 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): # Create a zip file of the HTML build for offline reading. # This gets moved into MEDIA_ROOT for downloading. # - html_build_dir = parent_build_dir.joinpath("_build", "djangohtml") + html_build_dir = parent_build_dir / "_build" / "djangohtml" zipfile_name = f"django-docs-{release.version}-{release.lang}.zip" - zipfile_path = Path(settings.MEDIA_ROOT).joinpath("docs", zipfile_name) + zipfile_path = settings.MEDIA_ROOT / "docs" / zipfile_name if not zipfile_path.parent.exists(): zipfile_path.parent.mkdir(parents=True) if self.verbosity >= 2: @@ -257,8 +256,8 @@ def zipfile_inclusion_filter(file_path): # Copy the build results to the directory used for serving # the documentation in the least disruptive way possible. # - build_dir = parent_build_dir.joinpath("_build") - built_dir = parent_build_dir.joinpath("_built") + build_dir = parent_build_dir / "_build" + built_dir = parent_build_dir / "_built" subprocess.check_call( [ "rsync", @@ -273,7 +272,7 @@ def zipfile_inclusion_filter(file_path): if release.is_default: self._setup_stable_symlink(release, built_dir) - json_built_dir = parent_build_dir.joinpath("_built", "json") + json_built_dir = parent_build_dir / "_built" / "json" documents = gen_decoded_documents(json_built_dir) release.sync_to_db(documents) @@ -287,7 +286,7 @@ def update_git(self, url, destdir, changed_dir="."): repo, branch = url.rsplit("@", 1) else: repo, branch = url, "main" - if destdir.joinpath(".git").exists(): + if (destdir / ".git").exists(): remote = "origin" branch_with_remote = f"{remote}/{branch}" try: diff --git a/docs/models.py b/docs/models.py index 17c2cca01a..531aaf7c3e 100644 --- a/docs/models.py +++ b/docs/models.py @@ -178,9 +178,7 @@ def sync_to_db(self, decoded_documents): self.documents.all().delete() # Read excluded paths from robots.docs.txt. - robots_path = settings.BASE_DIR.joinpath( - "djangoproject", "static", "robots.docs.txt" - ) + robots_path = settings.BASE_DIR / "djangoproject" / "static" / "robots.docs.txt" with open(str(robots_path)) as fh: excluded_paths = [ line.strip().split("/")[-1] diff --git a/docs/tests/test_models.py b/docs/tests/test_models.py index d17bd4d051..af8e246038 100644 --- a/docs/tests/test_models.py +++ b/docs/tests/test_models.py @@ -526,10 +526,8 @@ def test_excluded_documents(self): from robots indexing. """ # Read the first Disallow line of robots.txt. - robots_path = settings.BASE_DIR.joinpath( - "djangoproject", "static", "robots.docs.txt" - ) - with open(str(robots_path)) as fh: + robots_path = settings.BASE_DIR / "djangoproject" / "static" / "robots.docs.txt" + with robots_path.open() as fh: for line in fh: if line.startswith("Disallow:"): break diff --git a/docs/tests/test_templates.py b/docs/tests/test_templates.py index 66f221eeff..a0649e34ae 100644 --- a/docs/tests/test_templates.py +++ b/docs/tests/test_templates.py @@ -27,8 +27,8 @@ def test_get_all_doc_versions_empty(self): def test_get_all_doc_versions(self): tmp_docs_build_root = Path(tempfile.mkdtemp()) self.addCleanup(shutil.rmtree, tmp_docs_build_root) - os.makedirs(tmp_docs_build_root.joinpath("en", "1.8", "_built", "json")) - os.makedirs(tmp_docs_build_root.joinpath("en", "1.11", "_built", "json")) + os.makedirs(tmp_docs_build_root / "en" / "1.8" / "_built" / "json") + os.makedirs(tmp_docs_build_root / "en" / "1.11" / "_built" / "json") with self.settings(DOCS_BUILD_ROOT=tmp_docs_build_root): self.assertEqual(get_all_doc_versions({}), ["1.8", "1.11", "dev"]) diff --git a/docs/utils.py b/docs/utils.py index 500104df9c..bbbf579008 100644 --- a/docs/utils.py +++ b/docs/utils.py @@ -1,12 +1,13 @@ import re import unicodedata +from pathlib import Path from django.conf import settings from django.http import Http404 def get_doc_root(lang, version, builder="json"): - return settings.DOCS_BUILD_ROOT.joinpath(lang, version, "_built", builder) + return settings.DOCS_BUILD_ROOT / lang / version / "_built" / builder def get_doc_root_or_404(lang, version, builder="json"): @@ -22,7 +23,7 @@ def get_doc_path(docroot, subpath): bits = subpath.strip("/").split("/") + ["index.fjson"] except AttributeError: bits = [] - doc = docroot.joinpath(*bits) + doc = docroot / Path(*bits) try: if doc.exists(): return doc @@ -30,7 +31,7 @@ def get_doc_path(docroot, subpath): pass # we get here if doc + subpath (without /index.fjson) is a file bits = bits[:-2] + ["%s.fjson" % bits[-2]] - doc = docroot.joinpath(*bits) + doc = docroot / Path(*bits) if doc.exists(): return doc diff --git a/docs/views.py b/docs/views.py index a525b3c1da..e7fdc2f9bc 100644 --- a/docs/views.py +++ b/docs/views.py @@ -81,7 +81,7 @@ def load_json_file(path): context = { "doc": load_json_file(doc_path), - "env": load_json_file(docroot.joinpath("globalcontext.json")), + "env": load_json_file(docroot / "globalcontext.json"), "lang": lang, "version": version, "canonical_version": canonical_version, @@ -91,7 +91,7 @@ def load_json_file(path): "rtd_version": rtd_version, "docurl": url, "update_date": datetime.datetime.fromtimestamp( - (docroot.joinpath("last_build")).stat().st_mtime + (docroot / "last_build").stat().st_mtime ), "redirect_from": request.GET.get("from", None), } diff --git a/fundraising/tests/test_views.py b/fundraising/tests/test_views.py index aca9d85903..a5b2749306 100644 --- a/fundraising/tests/test_views.py +++ b/fundraising/tests/test_views.py @@ -260,7 +260,7 @@ def setUp(self): ) def stripe_data(self, filename): - file_path = settings.BASE_DIR.joinpath(f"fundraising/test_data/{filename}.json") + file_path = settings.BASE_DIR / f"fundraising/test_data/{filename}.json" with file_path.open() as f: data = json.load(f) return stripe.convert_to_stripe_object(data, stripe.api_key, None)