Skip to content

Commit 11c5e19

Browse files
authored
Move versions declaration in pyproject.toml (#700)
### Why * Inspired by aeecleclair/Titan#480 * Now both the version of itself and of the Titan client are declared in the TOML config file ### How * I tried cleverly adding [`toml_file="pyproject.toml",`](https://docs.pydantic.dev/latest/concepts/pydantic_settings/#other-settings-source) in the `SettingsConfigDict` (line 49) so that the model would load fields from both config files (`.env` and `pyproject.toml`), but it seems Pydantic does not allow that at all... * So I went using Python's *native* [`tomllib`](https://docs.python.org/3/library/tomllib.html) TOML parser : it the only solution that worked for me, and it's short. * (The `with` is out of the class because Pydantic started crying about the type of the bytes flow from reading the file) ### [`pyproject.toml`](https://packaging.python.org/en/latest/guides/writing-pyproject-toml/) * I just wanted to add ```toml version = "4.3.3" minimal-titan-version-code = 139 ``` to the `[project]` table, but I realized it **didn't even exist**, so I ended up writing the bare minimum. * Perhaps we should fill this file seriously... * it is powerful for automation and description (it could not hurt to have it filled), * and required to make a release on [PyPI](https://pypi.org/user/ECLAIR/) (we already release CalypSSO and the HA wrapper, why not Hyperion itself?).
1 parent 3959662 commit 11c5e19

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

app/core/utils/config.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import tomllib
12
from functools import cached_property
3+
from pathlib import Path
24
from typing import Any
35

46
import jwt
@@ -209,16 +211,25 @@ class Settings(BaseSettings):
209211
# If this token is not set, the service will not be able to access the data and no integrity check will be performed
210212
MYECLPAY_DATA_VERIFIER_ACCESS_TOKEN: str | None = None
211213

212-
#################################
213-
# Hardcoded Hyperion parameters #
214-
#################################
214+
# Maximum wallet balance for MyECLPay in cents, we will prevent user from adding more money to their wallet if it will make their balance exceed this value
215215

216-
# Hyperion follows Semantic Versioning
217-
# https://semver.org/
218-
HYPERION_VERSION: str = "4.5.1"
219-
MINIMAL_TITAN_VERSION_CODE: int = 139
216+
#############################
217+
# pyproject.toml parameters #
218+
#############################
220219

221-
# Maximum wallet balance for MyECLPay in cents, we will prevent user from adding more money to their wallet if it will make their balance exceed this value
220+
@computed_field # type: ignore[prop-decorator]
221+
@cached_property
222+
def HYPERION_VERSION(cls) -> str:
223+
with Path("pyproject.toml").open("rb") as pyproject_binary:
224+
pyproject = tomllib.load(pyproject_binary)
225+
return str(pyproject["project"]["version"])
226+
227+
@computed_field # type: ignore[prop-decorator]
228+
@cached_property
229+
def MINIMAL_TITAN_VERSION_CODE(cls) -> str:
230+
with Path("pyproject.toml").open("rb") as pyproject_binary:
231+
pyproject = tomllib.load(pyproject_binary)
232+
return str(pyproject["project"]["minimal-titan-version-code"])
222233

223234
######################################
224235
# Automatically generated parameters #
@@ -374,6 +385,8 @@ def init_cached_property(self) -> "Settings":
374385
By calling them in this validator, we force their initialization during the instantiation of the class.
375386
This allow them to raise error on Hyperion startup if they are not correctly configured instead of creating an error on runtime.
376387
"""
388+
self.HYPERION_VERSION # noqa: B018
389+
self.MINIMAL_TITAN_VERSION_CODE # noqa: B018
377390
self.KNOWN_AUTH_CLIENTS # noqa: B018
378391
self.RSA_PRIVATE_KEY # noqa: B018
379392
self.RSA_PUBLIC_KEY # noqa: B018

pyproject.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
[project]
2+
name = "hyperion"
3+
description = "Back-end of MyECL, the app of the students of École Centrale de Lyon, made by ÉCLAIR"
4+
readme = "README.md"
5+
authors = [{ name = "AEECL ECLAIR" }]
6+
7+
# Hyperion follows Semantic Versioning
8+
# https://semver.org/
9+
version = "4.5.1"
10+
minimal-titan-version-code = 139
11+
requires-python = ">= 3.11, < 3.13"
12+
13+
license = "MIT"
14+
license-files = ["LICENSE", "assets/privacy.txt", "assets/terms-and-conditions.txt", "assets/myeclpay-terms-of-service.txt"]
15+
16+
117
[tool.ruff]
218
# By default ruff also respect gitignore files
319
# Same as Black.

0 commit comments

Comments
 (0)