-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
51 lines (46 loc) · 1.46 KB
/
config.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
42
43
44
45
46
47
48
49
50
51
import sys, yaml
from pygame import Color
from typing import Tuple
class Config:
def __init__(self, path: str):
try:
self.file = open(path)
self.config = yaml.load(self.file.read(), yaml.Loader)
self.file.close()
except:
print("Can't load config file, exiting!")
print("You can download default config from https://raw.githubusercontent.com/goteusz-maszyk/pypong/master/config.yml")
sys.exit(1)
def get_color(self, path: str, raise_error: bool = False) -> Tuple:
try:
parsed: Tuple = tuple(map(int, self.config[path].split(" ")))
return Color(parsed)
except:
if raise_error:
print("Config value", path, "is invalid! Exiting.")
sys.exit(1)
return None
def get_string(self, path: str, raise_error: bool = False) -> str:
try:
return str(self.config[path])
except:
if raise_error:
print("Config value", path, "is invalid! Exiting.")
sys.exit(1)
return None
def get_float(self, path: str, raise_error: bool = False) -> float:
try:
return float(self.config[path])
except:
if raise_error:
print("Config value", path, "is invalid! Exiting.")
sys.exit(1)
return None
def get_int(self, path: str, raise_error: bool = False) -> float:
try:
return int(self.config[path])
except:
if raise_error:
print("Config value", path, "is invalid! Exiting.")
sys.exit(1)
return None