From 6299fc7f81ac4c086f1b5c6c02e47152c60d190d Mon Sep 17 00:00:00 2001 From: Jens L Date: Fri, 12 May 2023 20:04:02 +0200 Subject: [PATCH] root: migrate from os.path to Pathlib (#5594) Signed-off-by: Jens Langhammer --- authentik/api/v3/config.py | 4 ++-- authentik/lib/config.py | 26 +++++++++++++++----------- authentik/root/settings.py | 9 ++++----- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/authentik/api/v3/config.py b/authentik/api/v3/config.py index b0d825105..640e03453 100644 --- a/authentik/api/v3/config.py +++ b/authentik/api/v3/config.py @@ -1,5 +1,5 @@ """core Configs API""" -from os import path +from pathlib import Path from django.conf import settings from django.db import models @@ -63,7 +63,7 @@ class ConfigView(APIView): """Get all capabilities this server instance supports""" caps = [] deb_test = settings.DEBUG or settings.TEST - if path.ismount(settings.MEDIA_ROOT) or deb_test: + if Path(settings.MEDIA_ROOT).is_mount() or deb_test: caps.append(Capabilities.CAN_SAVE_MEDIA) if GEOIP_READER.enabled: caps.append(Capabilities.CAN_GEO_IP) diff --git a/authentik/lib/config.py b/authentik/lib/config.py index 6e823243f..2f9d10002 100644 --- a/authentik/lib/config.py +++ b/authentik/lib/config.py @@ -5,6 +5,7 @@ from contextlib import contextmanager from glob import glob from json import dumps, loads from json.decoder import JSONDecodeError +from pathlib import Path from sys import argv, stderr from time import time from typing import Any @@ -42,22 +43,25 @@ class ConfigLoader: def __init__(self): super().__init__() self.__config = {} - base_dir = os.path.realpath(os.path.join(os.path.dirname(__file__), "../..")) - for path in SEARCH_PATHS: + base_dir = Path(__file__).parent.joinpath(Path("../..")).resolve() + for _path in SEARCH_PATHS: + path = Path(_path) # Check if path is relative, and if so join with base_dir - if not os.path.isabs(path): - path = os.path.join(base_dir, path) - if os.path.isfile(path) and os.path.exists(path): + if not path.is_absolute(): + path = base_dir / path + if path.is_file() and path.exists(): # Path is an existing file, so we just read it and update our config with it self.update_from_file(path) - elif os.path.isdir(path) and os.path.exists(path): + elif path.is_dir() and path.exists(): # Path is an existing dir, so we try to read the env config from it env_paths = [ - os.path.join(path, ENVIRONMENT + ".yml"), - os.path.join(path, ENVIRONMENT + ".env.yml"), + path / Path(ENVIRONMENT + ".yml"), + path / Path(ENVIRONMENT + ".env.yml"), + path / Path(ENVIRONMENT + ".yaml"), + path / Path(ENVIRONMENT + ".env.yaml"), ] for env_file in env_paths: - if os.path.isfile(env_file) and os.path.exists(env_file): + if env_file.is_file() and env_file.exists(): # Update config with env file self.update_from_file(env_file) self.update_from_env() @@ -99,13 +103,13 @@ class ConfigLoader: value = url.query return value - def update_from_file(self, path: str): + def update_from_file(self, path: Path): """Update config from file contents""" try: with open(path, encoding="utf8") as file: try: self.update(self.__config, yaml.safe_load(file)) - self.log("debug", "Loaded config", file=path) + self.log("debug", "Loaded config", file=str(path)) self.loaded_file.append(path) except yaml.YAMLError as exc: raise ImproperlyConfigured from exc diff --git a/authentik/root/settings.py b/authentik/root/settings.py index 36c815d49..5abb8d18f 100644 --- a/authentik/root/settings.py +++ b/authentik/root/settings.py @@ -4,6 +4,7 @@ import importlib import logging import os from hashlib import sha512 +from pathlib import Path from urllib.parse import quote_plus import structlog @@ -19,11 +20,9 @@ from authentik.stages.password import BACKEND_APP_PASSWORD, BACKEND_INBUILT, BAC LOGGER = structlog.get_logger() -# Build paths inside the project like this: os.path.join(BASE_DIR, ...) -BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -STATIC_ROOT = BASE_DIR + "/static" -STATICFILES_DIRS = [BASE_DIR + "/web"] -MEDIA_ROOT = BASE_DIR + "/media" +BASE_DIR = Path(__file__).absolute().parent.parent.parent +STATICFILES_DIRS = [BASE_DIR / Path("web")] +MEDIA_ROOT = BASE_DIR / Path("media") DEBUG = CONFIG.y_bool("debug") SECRET_KEY = CONFIG.y("secret_key")