-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathshared.py
41 lines (35 loc) · 1.26 KB
/
shared.py
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
import sys
from typing import Optional, Any
from dataclasses import MISSING
def is_verbose() -> bool:
cmd = " ".join(sys.argv)
if " -v " in cmd or " --verbose " in cmd:
return True
else:
return False
class SharedModule:
"""module used to store information between python files"""
# things that are not changed when self.reset is called
VERSION: str = "1.0"
_instance: Optional[Any] = None # to make a singleton
red = MISSING
yellow = MISSING
reset = MISSING
white = MISSING
purple = MISSING
verbose = is_verbose()
def __new__(cls):
"make sure the instance will be a singleton"
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
else:
raise Exception("Tried to create another instance of SharedModule")
def __setattr__(self, name: str, value) -> None:
"forbid creation of new attributes."
if hasattr(self, name):
assert getattr(self, name) is MISSING, f"tried to declare twice the attribute '{name}'"
object.__setattr__(self, name, value)
else:
raise TypeError(f'SharedModule forbids the creation of unexpected attribute "{name}"')
shared = SharedModule()