mirror of
https://framagit.org/framasoft/framaspace/argos.git
synced 2025-04-28 18:02:41 +02:00
🎨 — Use pathlib.Path instead of os.path
This commit is contained in:
parent
cb0a638545
commit
f52dd5dd8a
4 changed files with 16 additions and 17 deletions
|
@ -1,6 +1,7 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
from pathlib import Path
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
@ -32,10 +33,11 @@ def coroutine(f):
|
||||||
|
|
||||||
|
|
||||||
def validate_config_access(ctx, param, value):
|
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
|
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} is not readabale.")
|
||||||
|
|
||||||
raise click.BadParameter(f"the file {value} does not exists or is not reachable.")
|
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()
|
settings = get_app_settings()
|
||||||
|
|
||||||
current_dir = os.path.dirname(__file__)
|
current_dir = Path(__file__).resolve().parent
|
||||||
alembic_cfg = Config(os.path.join(current_dir, "server/migrations/alembic.ini"))
|
alembic_cfg = Config(current_dir / "server" / "migrations" / "alembic.ini")
|
||||||
alembic_cfg.set_main_option("sqlalchemy.url", settings.database_url)
|
alembic_cfg.set_main_option("sqlalchemy.url", settings.database_url)
|
||||||
command.upgrade(alembic_cfg, "head")
|
command.upgrade(alembic_cfg, "head")
|
||||||
|
|
||||||
|
@ -251,12 +253,9 @@ async def generate_config():
|
||||||
Redirect the output to a file to save it:
|
Redirect the output to a file to save it:
|
||||||
argos server generate-config > /etc/argos/config.yaml
|
argos server generate-config > /etc/argos/config.yaml
|
||||||
"""
|
"""
|
||||||
current_dir = os.path.dirname(__file__)
|
config_example = Path(__file__).resolve().parent / "config-example.yaml"
|
||||||
with open(
|
with config_example.open("r", encoding="utf-8") as f:
|
||||||
os.path.join(current_dir, "config-example.yaml"), "r", encoding="utf-8"
|
|
||||||
) as f:
|
|
||||||
print(f.read())
|
print(f.read())
|
||||||
f.close()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
@ -35,7 +35,7 @@ def get_application() -> FastAPI:
|
||||||
appli.include_router(routes.api, prefix="/api")
|
appli.include_router(routes.api, prefix="/api")
|
||||||
appli.include_router(routes.views)
|
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")
|
appli.mount("/static", StaticFiles(directory=static_dir), name="static")
|
||||||
return appli
|
return appli
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""Web interface for humans"""
|
"""Web interface for humans"""
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from functools import cmp_to_key
|
from functools import cmp_to_key
|
||||||
from os import path
|
from pathlib import Path
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
@ -18,8 +18,8 @@ from argos.server.routes.dependencies import get_config, get_db
|
||||||
|
|
||||||
route = APIRouter()
|
route = APIRouter()
|
||||||
|
|
||||||
current_dir = path.dirname(__file__)
|
current_dir = Path(__file__).resolve().parent
|
||||||
templates = Jinja2Templates(directory=path.join(current_dir, "../templates"))
|
templates = Jinja2Templates(directory=current_dir / ".." / "templates")
|
||||||
SEVERITY_LEVELS = {"ok": 1, "warning": 2, "critical": 3, "unknown": 4}
|
SEVERITY_LEVELS = {"ok": 1, "warning": 2, "critical": 3, "unknown": 4}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""Pydantic schemas for server"""
|
"""Pydantic schemas for server"""
|
||||||
import os
|
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from os import environ
|
from os import environ
|
||||||
|
from pathlib import Path
|
||||||
from typing import Optional, Union
|
from typing import Optional, Union
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
@ -77,9 +77,9 @@ def read_yaml_config(filename):
|
||||||
|
|
||||||
|
|
||||||
def _load_yaml(filename):
|
def _load_yaml(filename):
|
||||||
base_dir = os.path.dirname(filename)
|
base_dir = Path(filename).resolve().parent
|
||||||
YamlIncludeConstructor.add_to_loader_class(
|
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:
|
with open(filename, "r", encoding="utf-8") as stream:
|
||||||
|
|
Loading…
Reference in a new issue