Skip to content

Commit

Permalink
🎨 Switch to pathlib.Path
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Jan 9, 2024
1 parent 668a44b commit e9cd717
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 32 deletions.
2 changes: 1 addition & 1 deletion requirements/base.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ django-log-outgoing-requests
# WSGI servers & monitoring - production oriented
uwsgi
sentry-sdk
elastic-apm
elastic-apm
33 changes: 15 additions & 18 deletions src/nrc/conf/includes/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import os
from pathlib import Path

from django.urls import reverse_lazy

Expand All @@ -12,12 +13,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
Expand Down Expand Up @@ -164,7 +161,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": [
Expand All @@ -182,18 +179,18 @@
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
#

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.
Expand All @@ -202,11 +199,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
Expand All @@ -225,7 +222,7 @@
#
# LOGGING
#
LOGGING_DIR = os.path.join(BASE_DIR, "log")
LOGGING_DIR = BASE_DIR / "log"

LOGGING = {
"version": 1,
Expand Down Expand Up @@ -254,31 +251,31 @@
"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,
},
"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,
},
"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,
},
"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,
Expand Down Expand Up @@ -381,7 +378,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:
Expand Down
11 changes: 3 additions & 8 deletions src/nrc/conf/local_example.py
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion src/nrc/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions src/nrc/tests/test_self_signed_certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -42,15 +42,15 @@ 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):
super().setUpClass()

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
Expand Down
3 changes: 2 additions & 1 deletion src/nrc/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down

0 comments on commit e9cd717

Please sign in to comment.