Passed
Push — develop ( 1b4b8c...fa1775 )
by Dean
03:19
created

State.inner()   A

Complexity

Conditions 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
c 1
b 0
f 0
rs 9.2
cc 4
1
from core.helpers import error_view, message_view
2
3
import functools
4
5
6
class State(object):
7
    class Types(object):
8
        initializing    = 10
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
initializing = 10
^
Loading history...
9
        configuring     = 20
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
configuring = 20
^
Loading history...
10
        starting        = 30
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
starting = 30
^
Loading history...
11
12
        started         = 40
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
started = 40
^
Loading history...
13
        error           = 90
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
error = 90
^
Loading history...
14
15
    current = Types.initializing
16
    exception = None
17
18
    @classmethod
19
    def get(cls):
20
        return cls.current
21
22
    @classmethod
23
    def set(cls, state, exception=None):
24
        if state < cls.current:
25
            raise ValueError('Unable to reverse states')
26
27
        cls.current = state
28
        cls.exception = exception
29
30
    @classmethod
31
    def wait(cls, state=Types.started, message='Plugin is starting up...'):
32
        def wrapper(func):
33
            @functools.wraps(func)
34
            def inner(*args, **kwargs):
35
                if cls.current == State.Types.error:
36
                    if cls.exception:
37
                        return error_view(type(cls.exception).__name__, cls.exception.message)
38
39
                    return message_view(message='Unknown startup error')
40
41
                if cls.current < state:
42
                    return message_view(message=message)
43
44
                return func(*args, **kwargs)
45
46
            return inner
47
48
        return wrapper
49