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

UserKeyReference.get_user()   A

Complexity

Conditions 2

Size

Total Lines 14

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 14
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 USER_SEPARATOR
17
18
__all__ = [
19
    'InvalidUserKeyReferenceError',
20
]
21
22
23
class InvalidUserKeyReferenceError(ValueError):
24
    def __init__(self, ref):
25
        message = 'Invalid resource reference: %s' % (ref)
26
        self.ref = ref
27
        self.message = message
28
        super(InvalidUserKeyReferenceError, self).__init__(message)
29
30
31
class UserKeyReference(object):
32
    """
33
    Holds a reference to key given name and prefix. For example, if key name is foo and prefix
34
    is bob, this returns a string of form "bob.foo". This assumes '.' is the PREFIX_SEPARATOR.
35
    """
36
37
    def __init__(self, user, name):
38
        self._user = user
39
        self._name = name
40
        self.ref = ('%s%s%s' % (self._user, USER_SEPARATOR, self._name))
41
42
    def __str__(self):
43
        return self.ref
44
45
    @staticmethod
46
    def to_string_reference(user, name):
47
        """
48
        Given a key ``name`` and ``user``, this method returns a new name (string ref)
49
        to address the key value pair in the context of that user.
50
51
        :param user: User to whom key belongs.
52
        :type name: ``str``
53
54
        :param name: Original name of the key.
55
        :type name: ``str``
56
57
        :rtype: ``str``
58
        """
59
        return UserKeyReference(user=user, name=name)
60
61
    @staticmethod
62
    def from_string_reference(ref):
63
        """
64
        Given a user key ``reference``, this method returns the user and actual name of the key.
65
66
        :param ref: Reference to user key.
67
        :type ref: ``str``
68
69
        :rtype: ``tuple`` of ``str`` and ``str``
70
        """
71
        user = UserKeyReference.get_user(ref)
72
        name = UserKeyReference.get_name(ref)
73
74
        return (user, name)
75
76
    @staticmethod
77
    def get_user(ref):
78
        """
79
        Given a user key ``reference``, this method returns the user to whom the key belongs.
80
81
        :param ref: Reference to user key.
82
        :type ref: ``str``
83
84
        :rtype: ``str``
85
        """
86
        try:
87
            return ref.split(USER_SEPARATOR, 1)[0]
88
        except (IndexError, AttributeError):
89
            raise InvalidUserKeyReferenceError(ref=ref)
90
91
    @staticmethod
92
    def get_name(ref):
93
        """
94
        Given a user key ``reference``, this method returns the name of the key.
95
96
        :param ref: Reference to user key.
97
        :type ref: ``str``
98
99
        :rtype: ``str``
100
        """
101
        try:
102
            return ref.split(USER_SEPARATOR, 1)[1]
103
        except (IndexError, AttributeError):
104
            raise InvalidUserKeyReferenceError(ref=ref)
105