summaryrefslogtreecommitdiffstats
path: root/server/src/authority_string_transformer.py
blob: 8db484b202703dc9f823f84bd38fe30f147c2b78 (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
"""AuthroityStringTransformer."""

from users_text_manager import AUTHORITY


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

class AuthorityStringTransformer:  # pylint:disable=W0232
    """Transforms authority between number and strin format."""
    @staticmethod
    def to_string(authority):
        """Transform number authority value to string value.

        Args:
            authority: Authority in number format.

        Returns:
            authority: Corrosponding authority in string format.
        """
        if authority == AUTHORITY.READONLY:
            return 'RO'
        elif authority == AUTHORITY.READWRITE:
            return 'RW'
        raise AuthorityStringTransformerError('Invalid number.')

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

        Args:
            authority: Authority in string format.

        Returns:
            authority: Corrosponding authority in number format.
        """
        if string == 'RO':
            return AUTHORITY.READONLY
        elif string == 'RW':
            return AUTHORITY.READWRITE
        raise AuthorityStringTransformerError('Invalid string.')