-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
92 lines (73 loc) · 2.89 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import importlib.metadata
import importlib.resources
import os
from functools import lru_cache
from pathlib import Path
from typing import Self
from pydantic import FilePath, HttpUrl, model_validator, BaseModel
from pydantic_settings import (
BaseSettings,
SettingsConfigDict,
YamlConfigSettingsSource,
JsonConfigSettingsSource,
PydanticBaseSettingsSource,
)
from swagger_coverage_tool.src.tools.types import ServiceKey, ServiceName
class ServiceConfig(BaseModel):
key: ServiceKey
name: ServiceName
tags: list[str] | None = None
repository: HttpUrl | None = None
swagger_url: HttpUrl | None = None
swagger_file: FilePath | None = None
@model_validator(mode='after')
def validate_model(self) -> Self:
if (not self.swagger_url) and (not self.swagger_file):
raise ValueError(
'Either `swagger_url` or `swagger_file` must be provided. '
'Please provide one of them to load the Swagger configuration.'
)
return self
class Settings(BaseSettings):
model_config = SettingsConfigDict(
extra='allow',
env_file=os.path.join(os.getcwd(), ".env"),
env_prefix="SWAGGER_COVERAGE_",
env_file_encoding="utf-8",
env_nested_delimiter=".",
yaml_file=os.path.join(os.getcwd(), "swagger_coverage_config.yaml"),
yaml_file_encoding="utf-8",
json_file=os.path.join(os.getcwd(), "swagger_coverage_config.json"),
json_file_encoding="utf-8"
)
services: list[ServiceConfig]
results_dir: Path = Path(os.path.join(os.getcwd(), "coverage-results"))
history_file: Path | None = Path(os.path.join(os.getcwd(), "coverage-history.json"))
history_retention_limit: int = 30
html_report_file: Path | None = Path(os.path.join(os.getcwd(), "index.html"))
json_report_file: Path | None = Path(os.path.join(os.getcwd(), "coverage-report.json"))
@property
def html_report_template_file(self):
try:
return importlib.resources.files("swagger_coverage_tool.src.reports.templates") / "index.html"
except importlib.metadata.PackageNotFoundError:
return Path(os.path.join(os.getcwd(), "swagger_coverage_tool/src/reports/templates/index.html"))
@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
return (
YamlConfigSettingsSource(cls),
JsonConfigSettingsSource(cls),
env_settings,
dotenv_settings,
init_settings,
)
@lru_cache(maxsize=None)
def get_settings() -> Settings:
return Settings()