Skip to content

Commit 40d0165

Browse files
committed
feat (Configuration): use a cached_property for preferred usernames and subs lookups
fix: reorder imports fix: set exclude=True on computed field fix (Configuration): Don't use a computed field. [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci
1 parent eeacdf5 commit 40d0165

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

diracx-core/src/diracx/core/config/schema.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
from datetime import date, datetime
5+
from functools import cached_property
56
from typing import Annotated, Any, MutableMapping, TypeVar
67

78
from pydantic import BaseModel as _BaseModel
@@ -158,6 +159,11 @@ class RegistryConfig(BaseModel):
158159
Groups: MutableMapping[str, GroupConfig]
159160
"""DIRAC groups section, subsections represent the name of the group."""
160161

162+
@cached_property
163+
def _preferred_username_to_sub(self) -> dict[str, str]:
164+
"""Compute reverse lookup map from preferred username to user sub."""
165+
return {user.PreferedUsername: sub for sub, user in self.Users.items()}
166+
161167
def sub_from_preferred_username(self, preferred_username: str) -> str:
162168
"""Get the user sub from the preferred username.
163169
@@ -171,10 +177,10 @@ def sub_from_preferred_username(self, preferred_username: str) -> str:
171177
KeyError: If no user with the given preferred username is found.
172178
173179
"""
174-
for sub, user in self.Users.items():
175-
if user.PreferedUsername == preferred_username:
176-
return sub
177-
raise KeyError(f"User {preferred_username} not found in registry")
180+
try:
181+
return self._preferred_username_to_sub[preferred_username]
182+
except KeyError:
183+
raise KeyError(f"User {preferred_username} not found in registry") from None
178184

179185

180186
class DIRACConfig(BaseModel):

0 commit comments

Comments
 (0)