From 3e8397d9f0e981c924d666d75a76fe57249284c5 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Tue, 9 Jan 2024 11:38:27 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Switch=20to=20`pathlib.Path`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements/base.in | 2 +- src/nrc/conf/includes/base.py | 35 +++++++++---------- src/nrc/conf/local_example.py | 11 ++---- src/nrc/setup.py | 3 +- .../tests/test_self_signed_certificates.py | 6 ++-- src/nrc/wsgi.py | 3 +- 6 files changed, 27 insertions(+), 33 deletions(-) diff --git a/requirements/base.in b/requirements/base.in index 2b88eb6e..f0c6cb85 100644 --- a/requirements/base.in +++ b/requirements/base.in @@ -32,4 +32,4 @@ django-log-outgoing-requests # WSGI servers & monitoring - production oriented uwsgi sentry-sdk -elastic-apm \ No newline at end of file +elastic-apm diff --git a/src/nrc/conf/includes/base.py b/src/nrc/conf/includes/base.py index 131bf4b8..f40e4428 100644 --- a/src/nrc/conf/includes/base.py +++ b/src/nrc/conf/includes/base.py @@ -1,5 +1,6 @@ import datetime import os +from pathlib import Path from django.urls import reverse_lazy @@ -13,12 +14,8 @@ # Build paths inside the project, so further paths can be defined relative to # the code root. -DJANGO_PROJECT_DIR = os.path.abspath( - os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir) -) -BASE_DIR = os.path.abspath( - os.path.join(DJANGO_PROJECT_DIR, os.path.pardir, os.path.pardir) -) +DJANGO_PROJECT_DIR = Path(__file__).resolve(strict=True).parents[2] +BASE_DIR = DJANGO_PROJECT_DIR.parents[1] # # Core Django settings @@ -165,7 +162,7 @@ TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", - "DIRS": [os.path.join(DJANGO_PROJECT_DIR, "templates")], + "DIRS": [DJANGO_PROJECT_DIR / "templates"], "APP_DIRS": False, # conflicts with explicity specifying the loaders "OPTIONS": { "context_processors": [ @@ -183,7 +180,7 @@ WSGI_APPLICATION = "nrc.wsgi.application" # Translations -LOCALE_PATHS = (os.path.join(DJANGO_PROJECT_DIR, "conf", "locale"),) +LOCALE_PATHS = (DJANGO_PROJECT_DIR / "conf" / "locale",) # # SERVING of static and media files @@ -191,10 +188,10 @@ STATIC_URL = "/static/" -STATIC_ROOT = os.path.join(BASE_DIR, "static") +STATIC_ROOT = BASE_DIR / "static" # Additional locations of static files -STATICFILES_DIRS = [os.path.join(DJANGO_PROJECT_DIR, "static")] +STATICFILES_DIRS = [DJANGO_PROJECT_DIR / "static"] # List of finder classes that know how to find static files in # various locations. @@ -203,11 +200,11 @@ "django.contrib.staticfiles.finders.AppDirectoriesFinder", ] -MEDIA_ROOT = os.path.join(BASE_DIR, "media") +MEDIA_ROOT = BASE_DIR / "media" MEDIA_URL = "/media/" -FIXTURE_DIRS = [os.path.join(DJANGO_PROJECT_DIR, "fixtures")] +FIXTURE_DIRS = [DJANGO_PROJECT_DIR / "fixtures"] # # Sending EMAIL @@ -226,7 +223,7 @@ # # LOGGING # -LOGGING_DIR = os.path.join(BASE_DIR, "log") +LOGGING_DIR = BASE_DIR / "log" LOG_REQUESTS = config("LOG_REQUESTS", default=False) LOGGING = { @@ -257,7 +254,7 @@ "django": { "level": "DEBUG", "class": "logging.handlers.RotatingFileHandler", - "filename": os.path.join(LOGGING_DIR, "django.log"), + "filename": LOGGING_DIR / "django.log", "formatter": "verbose", "maxBytes": 1024 * 1024 * 10, # 10 MB "backupCount": 10, @@ -265,7 +262,7 @@ "project": { "level": "DEBUG", "class": "logging.handlers.RotatingFileHandler", - "filename": os.path.join(LOGGING_DIR, "nrc.log"), + "filename": LOGGING_DIR / "nrc.log", "formatter": "verbose", "maxBytes": 1024 * 1024 * 10, # 10 MB "backupCount": 10, @@ -273,7 +270,7 @@ "performance": { "level": "INFO", "class": "logging.handlers.RotatingFileHandler", - "filename": os.path.join(LOGGING_DIR, "performance.log"), + "filename": LOGGING_DIR / "performance.log", "formatter": "performance", "maxBytes": 1024 * 1024 * 10, # 10 MB "backupCount": 10, @@ -281,7 +278,7 @@ "notifications": { "level": "INFO", "class": "logging.handlers.RotatingFileHandler", - "filename": os.path.join(LOGGING_DIR, "notifications.log"), + "filename": LOGGING_DIR / "notifications.log", "formatter": "performance", "maxBytes": 1024 * 1024 * 10, # 10 MB "backupCount": 10, @@ -401,7 +398,7 @@ if "GIT_SHA" in os.environ: GIT_SHA = config("GIT_SHA", "") # in docker (build) context, there is no .git directory -elif os.path.exists(os.path.join(BASE_DIR, ".git")): +elif BASE_DIR.joinpath(".git").exists(): try: import git except ImportError: @@ -566,7 +563,7 @@ # ZGW-CONSUMERS # ZGW_CONSUMERS_TEST_SCHEMA_DIRS = [ - os.path.join(DJANGO_PROJECT_DIR, "tests", "schemas"), + DJANGO_PROJECT_DIR / "tests" / "schemas", ] # diff --git a/src/nrc/conf/local_example.py b/src/nrc/conf/local_example.py index acc0e0e4..e5d0e593 100644 --- a/src/nrc/conf/local_example.py +++ b/src/nrc/conf/local_example.py @@ -1,17 +1,12 @@ -import os +from pathlib import Path # # Any machine specific settings when using development settings. # # Automatically figure out the ROOT_DIR and PROJECT_DIR. -DJANGO_PROJECT_DIR = os.path.abspath( - os.path.join(os.path.dirname(__file__), os.path.pardir) -) -ROOT_DIR = os.path.abspath( - os.path.join(DJANGO_PROJECT_DIR, os.path.pardir, os.path.pardir) -) - +DJANGO_PROJECT_DIR = Path(__file__).resolve(strict=True).parents[1] +ROOT_DIR = DJANGO_PROJECT_DIR.parents[1] DATABASES = { "default": { diff --git a/src/nrc/setup.py b/src/nrc/setup.py index e6c6b3ef..84778a4f 100644 --- a/src/nrc/setup.py +++ b/src/nrc/setup.py @@ -11,6 +11,7 @@ """ import os import tempfile +from pathlib import Path from dotenv import load_dotenv from self_certifi import load_self_signed_certs as _load_self_signed_certs @@ -20,7 +21,7 @@ def setup_env(): # load the environment variables containing the secrets/config - dotenv_path = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, ".env") + dotenv_path = Path(__file__).parents[2] / ".env" load_dotenv(dotenv_path) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nrc.conf.dev") diff --git a/src/nrc/tests/test_self_signed_certificates.py b/src/nrc/tests/test_self_signed_certificates.py index 17d31fc8..7cae78e9 100644 --- a/src/nrc/tests/test_self_signed_certificates.py +++ b/src/nrc/tests/test_self_signed_certificates.py @@ -21,7 +21,7 @@ from nrc import setup from nrc.setup import load_self_signed_certs -CERTS_DIR = os.path.join(settings.BASE_DIR, "certs") +CERTS_DIR = settings.BASE_DIR / "certs" EXTRA_CERTS_ENVVAR = "EXTRA_VERIFY_CERTS" @@ -42,7 +42,7 @@ def can_connect(hostname: str): class SelfSignedCertificateTests(TestCase): - root_cert = os.path.join(CERTS_DIR, "openzaak.crt") + root_cert = CERTS_DIR / "openzaak.crt" @classmethod def setUpClass(cls): @@ -50,7 +50,7 @@ def setUpClass(cls): setup._certs_initialized = False cls._original_certs = os.environ.get(EXTRA_CERTS_ENVVAR) - os.environ[EXTRA_CERTS_ENVVAR] = cls.root_cert + os.environ[EXTRA_CERTS_ENVVAR] = str(cls.root_cert) load_self_signed_certs() @classmethod diff --git a/src/nrc/wsgi.py b/src/nrc/wsgi.py index df7c2406..31fffcd4 100644 --- a/src/nrc/wsgi.py +++ b/src/nrc/wsgi.py @@ -7,6 +7,7 @@ https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/ """ import os +from pathlib import Path from django.core.wsgi import get_wsgi_application @@ -19,7 +20,7 @@ def init_newrelic(): import newrelic.agent newrelic.agent.initialize( - os.path.join(os.environ.get("PROJECT_ROOT"), "newrelic.ini"), + str(Path(os.environ.get("PROJECT_ROOT"), "newrelic.ini")), "production", ) except Exception as e: