Skip to content

Preferences

User preferences and configuration options.

preferences

Classes

OutputFormats

Bases: StrEnum

Source code in diracx-core/src/diracx/core/preferences.py
class OutputFormats(StrEnum):
    RICH = "RICH"
    JSON = "JSON"

    @classmethod
    def default(cls):
        return cls.RICH if sys.stdout.isatty() else cls.JSON
Attributes
RICH = 'RICH' class-attribute instance-attribute
JSON = 'JSON' class-attribute instance-attribute
Functions
default() classmethod
Source code in diracx-core/src/diracx/core/preferences.py
@classmethod
def default(cls):
    return cls.RICH if sys.stdout.isatty() else cls.JSON

LogLevels

Bases: Enum

Source code in diracx-core/src/diracx/core/preferences.py
class LogLevels(Enum):
    ERROR = logging.ERROR
    WARNING = logging.WARNING
    INFO = logging.INFO
    DEBUG = logging.DEBUG
Attributes
ERROR = logging.ERROR class-attribute instance-attribute
WARNING = logging.WARNING class-attribute instance-attribute
INFO = logging.INFO class-attribute instance-attribute
DEBUG = logging.DEBUG class-attribute instance-attribute

DiracxPreferences

Bases: BaseSettings

Source code in diracx-core/src/diracx/core/preferences.py
class DiracxPreferences(BaseSettings):
    model_config = SettingsConfigDict(env_prefix="DIRACX_")

    url: AnyHttpUrl
    ca_path: Path | None = None
    output_format: OutputFormats = Field(default_factory=OutputFormats.default)
    log_level: LogLevels = LogLevels.INFO
    credentials_path: Path = Field(
        default_factory=lambda: Path.home() / ".cache" / "diracx" / "credentials.json"
    )

    @classmethod
    def from_env(cls):
        return cls(_env_file=dotenv_files_from_environment("DIRACX_DOTENV"))

    @field_validator("log_level", mode="before")
    @classmethod
    def validate_log_level(cls, v: str):
        if isinstance(v, str):
            return getattr(LogLevels, v.upper())
        return v
Attributes
model_config = SettingsConfigDict(env_prefix='DIRACX_') class-attribute instance-attribute
url instance-attribute
ca_path = None class-attribute instance-attribute
output_format = Field(default_factory=(OutputFormats.default)) class-attribute instance-attribute
log_level = LogLevels.INFO class-attribute instance-attribute
credentials_path = Field(default_factory=(lambda: Path.home() / '.cache' / 'diracx' / 'credentials.json')) class-attribute instance-attribute
Functions
from_env() classmethod
Source code in diracx-core/src/diracx/core/preferences.py
@classmethod
def from_env(cls):
    return cls(_env_file=dotenv_files_from_environment("DIRACX_DOTENV"))
validate_log_level(v) classmethod
Source code in diracx-core/src/diracx/core/preferences.py
@field_validator("log_level", mode="before")
@classmethod
def validate_log_level(cls, v: str):
    if isinstance(v, str):
        return getattr(LogLevels, v.upper())
    return v

Functions

get_diracx_preferences() cached

Caches the preferences.

Source code in diracx-core/src/diracx/core/preferences.py
@lru_cache(maxsize=1)
def get_diracx_preferences() -> DiracxPreferences:
    """Caches the preferences."""
    return DiracxPreferences()