Completed
Pull Request — master (#2669)
by Lakshmi
06:24
created

get_key_reference()   A

Complexity

Conditions 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
from st2common.constants.keyvalue import SYSTEM_SCOPE
17
from st2common.models.system.keyvalue import KeyReference
18
from st2common.persistence.keyvalue import KeyValuePair
19
20
__all__ = [
21
    'KeyValueLookup',
22
    'get_key_reference',
23
]
24
25
26
class KeyValueLookup(object):
27
28
    def __init__(self, key_prefix='', cache=None, scope=None):
29
        self._key_prefix = key_prefix
30
        self._scope = scope if scope else SYSTEM_SCOPE
31
        if cache is None:
32
            cache = {}
33
        self._value_cache = cache
34
35
    def __str__(self):
36
        return self._value_cache[self._key_prefix]
37
38
    def __getitem__(self, key):
39
        return self._get(key)
40
41
    def __getattr__(self, name):
42
        return self._get(name)
43
44
    def _get(self, name):
45
        # get the value for this key and save in value_cache
46
        key = KeyReference(name=name, prefix=self._key_prefix).ref
47
        value = self._get_kv(key)
48
        self._value_cache[key] = value
49
        # return a KeyValueLookup as response since the lookup may not be complete e.g. if
50
        # the lookup is for 'key_base.key_value' it is likely that the calling code, e.g. Jinja,
51
        # will expect to do a dictionary style lookup for key_base and key_value as subsequent
52
        # calls. Saving the value in cache avoids extra DB calls.
53
        return KeyValueLookup(key_prefix=key, cache=self._value_cache, scope=self._scope)
54
55
    def _get_kv(self, key):
56
        scope = self._scope
57
        kvp = KeyValuePair.get_by_scope_and_name(scope=scope, name=key)
58
        # A good default value for un-matched value is empty string since that will be used
59
        # for rendering templates.
60
        return kvp.value if kvp else ''
61
62
63
def get_key_reference(name, scope, prefix):
64
    """
65
    Given a key name, scope and prefix, this method returns a new name (string ref)
66
    to address the key value pair in the context of that scope.
67
68
    :param name: Original name of the key.
69
    :type name: ``str``
70
71
    :param scope: Scope the key is under.
72
    :type scope: ``str``
73
74
    :param prefix: Prefix to use for the key.
75
    :type prefix: ``str``
76
77
    :rtype: ``str``
78
    """
79
    if scope == SYSTEM_SCOPE:
80
        return KeyReference(name=name).ref
81
    return KeyReference(name=name, prefix=prefix).ref
82