diff --git a/argos/commands.py b/argos/commands.py index 51e9f95..cf9d976 100644 --- a/argos/commands.py +++ b/argos/commands.py @@ -1,6 +1,7 @@ import asyncio import os from functools import wraps +from pathlib import Path from uuid import uuid4 import click @@ -32,10 +33,11 @@ def coroutine(f): def validate_config_access(ctx, param, value): - if os.path.isfile(value) and os.access(value, os.R_OK): + path = Path(value) + if path.is_file() and os.access(path, os.R_OK): return value - if os.path.isfile(value): + if path.is_file(): raise click.BadParameter(f"the file {value} is not readabale.") raise click.BadParameter(f"the file {value} does not exists or is not reachable.") @@ -226,8 +228,8 @@ async def migrate(config): settings = get_app_settings() - current_dir = os.path.dirname(__file__) - alembic_cfg = Config(os.path.join(current_dir, "server/migrations/alembic.ini")) + current_dir = Path(__file__).resolve().parent + alembic_cfg = Config(current_dir / "server" / "migrations" / "alembic.ini") alembic_cfg.set_main_option("sqlalchemy.url", settings.database_url) command.upgrade(alembic_cfg, "head") @@ -251,12 +253,9 @@ async def generate_config(): Redirect the output to a file to save it: argos server generate-config > /etc/argos/config.yaml """ - current_dir = os.path.dirname(__file__) - with open( - os.path.join(current_dir, "config-example.yaml"), "r", encoding="utf-8" - ) as f: + config_example = Path(__file__).resolve().parent / "config-example.yaml" + with config_example.open("r", encoding="utf-8") as f: print(f.read()) - f.close() if __name__ == "__main__": diff --git a/argos/server/main.py b/argos/server/main.py index f3d5d14..e6afac5 100644 --- a/argos/server/main.py +++ b/argos/server/main.py @@ -1,5 +1,5 @@ -import os import sys +from pathlib import Path from fastapi import FastAPI from fastapi.staticfiles import StaticFiles @@ -35,7 +35,7 @@ def get_application() -> FastAPI: appli.include_router(routes.api, prefix="/api") appli.include_router(routes.views) - static_dir = os.path.join(os.path.dirname(__file__), "static") + static_dir = Path(__file__).resolve().parent / "static" appli.mount("/static", StaticFiles(directory=static_dir), name="static") return appli diff --git a/argos/server/routes/views.py b/argos/server/routes/views.py index f2ef269..273a9bd 100644 --- a/argos/server/routes/views.py +++ b/argos/server/routes/views.py @@ -1,7 +1,7 @@ """Web interface for humans""" from collections import defaultdict from functools import cmp_to_key -from os import path +from pathlib import Path from typing import Annotated from urllib.parse import urlparse @@ -18,8 +18,8 @@ from argos.server.routes.dependencies import get_config, get_db route = APIRouter() -current_dir = path.dirname(__file__) -templates = Jinja2Templates(directory=path.join(current_dir, "../templates")) +current_dir = Path(__file__).resolve().parent +templates = Jinja2Templates(directory=current_dir / ".." / "templates") SEVERITY_LEVELS = {"ok": 1, "warning": 2, "critical": 3, "unknown": 4} diff --git a/argos/server/settings.py b/argos/server/settings.py index 501cae9..12a7286 100644 --- a/argos/server/settings.py +++ b/argos/server/settings.py @@ -1,7 +1,7 @@ """Pydantic schemas for server""" -import os from functools import lru_cache from os import environ +from pathlib import Path from typing import Optional, Union import yaml @@ -77,9 +77,9 @@ def read_yaml_config(filename): def _load_yaml(filename): - base_dir = os.path.dirname(filename) + base_dir = Path(filename).resolve().parent YamlIncludeConstructor.add_to_loader_class( - loader_class=yaml.FullLoader, base_dir=base_dir + loader_class=yaml.FullLoader, base_dir=str(base_dir) ) with open(filename, "r", encoding="utf-8") as stream: