| 207 | |
| 208 | |
| 209 | class UserKeyValueLookup(BaseKeyValueLookup): |
| 210 | |
| 211 | scope = USER_SCOPE |
| 212 | |
| 213 | def __init__( |
| 214 | self, |
| 215 | user, |
| 216 | prefix=None, |
| 217 | key_prefix=None, |
| 218 | cache=None, |
| 219 | scope=FULL_USER_SCOPE, |
| 220 | context=None, |
| 221 | ): |
| 222 | if not scope: |
| 223 | scope = FULL_USER_SCOPE |
| 224 | |
| 225 | if scope == USER_SCOPE: |
| 226 | scope = FULL_USER_SCOPE |
| 227 | |
| 228 | self._prefix = prefix |
| 229 | self._key_prefix = key_prefix or "" |
| 230 | self._value_cache = cache or {} |
| 231 | self._user = user |
| 232 | self._scope = scope |
| 233 | self._context = context if context else dict() |
| 234 | |
| 235 | def __str__(self): |
| 236 | return self._value_cache[self._key_prefix] |
| 237 | |
| 238 | def __getitem__(self, key): |
| 239 | return self._get(key) |
| 240 | |
| 241 | def __getattr__(self, name): |
| 242 | return self._get(name) |
| 243 | |
| 244 | def _get(self, name): |
| 245 | # get the value for this key and save in value_cache |
| 246 | if self._key_prefix: |
| 247 | key = "%s.%s" % (self._key_prefix, name) |
| 248 | else: |
| 249 | key = UserKeyReference(name=name, user=self._user).ref |
| 250 | |
| 251 | if self._prefix: |
| 252 | kvp_key = DATASTORE_KEY_SEPARATOR.join([self._prefix, key]) |
| 253 | else: |
| 254 | kvp_key = key |
| 255 | |
| 256 | value = self._get_kv(kvp_key) |
| 257 | self._value_cache[key] = value |
| 258 | # return a KeyValueLookup as response since the lookup may not be complete e.g. if |
| 259 | # the lookup is for 'key_base.key_value' it is likely that the calling code, e.g. Jinja, |
| 260 | # will expect to do a dictionary style lookup for key_base and key_value as subsequent |
| 261 | # calls. Saving the value in cache avoids extra DB calls. |
| 262 | return UserKeyValueLookup( |
| 263 | prefix=self._prefix, |
| 264 | user=self._user, |
| 265 | key_prefix=key, |
| 266 | cache=self._value_cache, |
no outgoing calls