943acbc573
Durcit la convention de nommage des projets (dérive constatée : 'Sliding Automation', 'code_versioning'... au lieu des formes canoniques). - trilium_api.py : projets_canoniques() lit le référentiel = valeurs du label projet sur les notes de type=projet (source unique, pas de constante en dur). Note-projet CodeVersioning créée (manquait). - mcp_server.py : _valider_projet() branché dans les 6 tools de création (add_decision/history/backlog, new_conversation, create_entite, add_skill). Refuse un projet non canonique (suggestion si faute) ou inconnu (renvoi au processus de création de projet). Ne verrouille pas si référentiel illisible. - lint_audit.py : VAL-nommage aligné sur le référentiel (attrape casse, espace ET snake_case ; l'ancien 'contient un espace' ratait code_versioning). - Données : 79 notes ré-étiquetées vers les 3 formes canoniques. Quality by design : l'erreur de nommage devient impossible à l'écriture, le Lint n'est plus que le filet de sécurité.
90 lines
2.3 KiB
Python
90 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
import math
|
|
import uuid
|
|
from typing import Any, ClassVar, Generic, TypeVar
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
class Convertor(Generic[T]):
|
|
regex: ClassVar[str] = ""
|
|
|
|
def convert(self, value: str) -> T:
|
|
raise NotImplementedError() # pragma: no cover
|
|
|
|
def to_string(self, value: T) -> str:
|
|
raise NotImplementedError() # pragma: no cover
|
|
|
|
|
|
class StringConvertor(Convertor[str]):
|
|
regex = "[^/]+"
|
|
|
|
def convert(self, value: str) -> str:
|
|
return value
|
|
|
|
def to_string(self, value: str) -> str:
|
|
value = str(value)
|
|
assert "/" not in value, "May not contain path separators"
|
|
assert value, "Must not be empty"
|
|
return value
|
|
|
|
|
|
class PathConvertor(Convertor[str]):
|
|
regex = ".*"
|
|
|
|
def convert(self, value: str) -> str:
|
|
return str(value)
|
|
|
|
def to_string(self, value: str) -> str:
|
|
return str(value)
|
|
|
|
|
|
class IntegerConvertor(Convertor[int]):
|
|
regex = "[0-9]+"
|
|
|
|
def convert(self, value: str) -> int:
|
|
return int(value)
|
|
|
|
def to_string(self, value: int) -> str:
|
|
value = int(value)
|
|
assert value >= 0, "Negative integers are not supported"
|
|
return str(value)
|
|
|
|
|
|
class FloatConvertor(Convertor[float]):
|
|
regex = r"[0-9]+(\.[0-9]+)?"
|
|
|
|
def convert(self, value: str) -> float:
|
|
return float(value)
|
|
|
|
def to_string(self, value: float) -> str:
|
|
value = float(value)
|
|
assert value >= 0.0, "Negative floats are not supported"
|
|
assert not math.isnan(value), "NaN values are not supported"
|
|
assert not math.isinf(value), "Infinite values are not supported"
|
|
return ("%0.20f" % value).rstrip("0").rstrip(".")
|
|
|
|
|
|
class UUIDConvertor(Convertor[uuid.UUID]):
|
|
regex = "[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{12}"
|
|
|
|
def convert(self, value: str) -> uuid.UUID:
|
|
return uuid.UUID(value)
|
|
|
|
def to_string(self, value: uuid.UUID) -> str:
|
|
return str(value)
|
|
|
|
|
|
CONVERTOR_TYPES: dict[str, Convertor[Any]] = {
|
|
"str": StringConvertor(),
|
|
"path": PathConvertor(),
|
|
"int": IntegerConvertor(),
|
|
"float": FloatConvertor(),
|
|
"uuid": UUIDConvertor(),
|
|
}
|
|
|
|
|
|
def register_url_convertor(key: str, convertor: Convertor[Any]) -> None:
|
|
CONVERTOR_TYPES[key] = convertor
|