Пример #1
0
def get_key_reference(scope, name, user=None):
    """
    Given a key name and user this method returns a new name (string ref)
    to address the key value pair in the context of that user.

    :param user: User to whom key belongs.
    :type user: ``str``

    :param name: Original name of the key.
    :type name: ``str``

    :rtype: ``str``
    """
    if scope == SYSTEM_SCOPE or scope == FULL_SYSTEM_SCOPE:
        return name
    elif scope == USER_SCOPE or scope == FULL_USER_SCOPE:
        if not user:
            raise InvalidUserException(
                "A valid user must be specified for user key ref."
            )
        return UserKeyReference(name=name, user=user).ref
    else:
        raise InvalidScopeException(
            'Scope "%s" is not valid. Allowed scopes are %s.' % (scope, ALLOWED_SCOPES)
        )
Пример #2
0
    def _get(self, name):
        # get the value for this key and save in value_cache
        if self._key_prefix:
            key = "%s.%s" % (self._key_prefix, name)
        else:
            key = UserKeyReference(name=name, user=self._user).ref

        if self._prefix:
            kvp_key = DATASTORE_KEY_SEPARATOR.join([self._prefix, key])
        else:
            kvp_key = key

        value = self._get_kv(kvp_key)
        self._value_cache[key] = value
        # return a KeyValueLookup as response since the lookup may not be complete e.g. if
        # the lookup is for 'key_base.key_value' it is likely that the calling code, e.g. Jinja,
        # will expect to do a dictionary style lookup for key_base and key_value as subsequent
        # calls. Saving the value in cache avoids extra DB calls.
        return UserKeyValueLookup(
            prefix=self._prefix,
            user=self._user,
            key_prefix=key,
            cache=self._value_cache,
            scope=self._scope,
        )