summaryrefslogtreecommitdiffstats
path: root/server/src/authority_string_transformer.py
blob: f99daa1428a3d9dc4648dfe6aa1cd15b07c8fe1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""For transformating the type of authroity."""

from users_text_manager import AUTHORITY


_mappings = [
    (AUTHORITY.READONLY, 'RO'),
    (AUTHORITY.READWRITE, 'RW'),
]


class Error(Exception):
    """Error raised by AuthorityStringTransformer."""
    pass


def to_string(authority):
    """Transform number authority value to string value.

    Args:
        authority: Authority in number format.

    Return:
        Corrosponding authority in string format.
    """
    for item in _mappings:
        if item[0] == authority:
            return item[1]
    raise Error('Invalid number.')


def to_number(string):
    """Transform string authority value to number value.

    Args:
        authority: Authority in string format.

    Return:
        Corrosponding authority in number format.
    """
    for item in _mappings:
        if item[1] == string:
            return item[0]
    raise Error('Invalid string.')