Skip to content

Commit 7ffea46

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 [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci
1 parent 0adb119 commit 7ffea46

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

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 datetime
5+
from functools import cached_property
56
from typing import Annotated, Any, MutableMapping, TypeVar
67

78
from pydantic import BaseModel as _BaseModel
@@ -149,6 +150,11 @@ class RegistryConfig(BaseModel):
149150
Groups: MutableMapping[str, GroupConfig]
150151
"""DIRAC groups section, subsections represent the name of the group."""
151152

153+
@cached_property
154+
def _preferred_username_to_sub(self) -> dict[str, str]:
155+
"""Compute reverse lookup map from preferred username to user sub."""
156+
return {user.PreferedUsername: sub for sub, user in self.Users.items()}
157+
152158
def sub_from_preferred_username(self, preferred_username: str) -> str:
153159
"""Get the user sub from the preferred username.
154160
@@ -162,10 +168,10 @@ def sub_from_preferred_username(self, preferred_username: str) -> str:
162168
KeyError: If no user with the given preferred username is found.
163169
164170
"""
165-
for sub, user in self.Users.items():
166-
if user.PreferedUsername == preferred_username:
167-
return sub
168-
raise KeyError(f"User {preferred_username} not found in registry")
171+
try:
172+
return self._preferred_username_to_sub[preferred_username]
173+
except KeyError:
174+
raise KeyError(f"User {preferred_username} not found in registry") from None
169175

170176

171177
class DIRACConfig(BaseModel):

0 commit comments

Comments
 (0)