File: lazy_initializer.py

package info (click to toggle)
freeorion 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 194,940 kB
  • sloc: cpp: 186,508; python: 40,969; ansic: 1,164; xml: 719; makefile: 32; sh: 7
file content (30 lines) | stat: -rw-r--r-- 803 bytes parent folder | download
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
from functools import wraps
from typing import Callable


class InitializerLock:
    """
    Class that ensure that it's properly initialized before the first use of data.
    """

    def __init__(self, name):
        self.__initialized = False
        self.__name = name

    def lock(self):
        self.__initialized = False

    def unlock(self):
        self.__initialized = True

    def __call__(self, function: Callable):
        @wraps(function)
        def wrapper(*args, **kwargs):
            if self.__initialized:
                return function(*args, **kwargs)
            else:
                raise ValueError(
                    f"Call of '{function.__name__}' is forbidden before {self.__class__.__name__}('{self.__name}') is initialized"
                )

        return wrapper