4
4
import time
5
5
from collections import OrderedDict
6
6
from pathlib import Path
7
+ from typing import TYPE_CHECKING , Dict , Optional , cast
8
+
9
+ if TYPE_CHECKING :
10
+ from cloudbot .bot import AbstractBot
7
11
8
12
logger = logging .getLogger ("cloudbot" )
9
13
10
14
11
15
class Config (OrderedDict ):
12
- def __init__ (self , bot , * , filename = "config.json" ):
16
+ def __init__ (
17
+ self , bot : "AbstractBot" , * , filename : str = "config.json"
18
+ ) -> None :
13
19
super ().__init__ ()
14
20
self .filename = filename
15
21
self .path = Path (self .filename ).resolve ()
16
22
self .bot = bot
17
23
18
- self ._api_keys = {}
24
+ self ._api_keys : Dict [ str , Optional [ str ]] = {}
19
25
20
26
# populate self with config data
21
27
self .load_config ()
22
28
23
- def get_api_key (self , name , default = None ):
29
+ def get_api_key (
30
+ self , name : str , default : Optional [str ] = None
31
+ ) -> Optional [str ]:
24
32
try :
25
33
return self ._api_keys [name ]
26
34
except LookupError :
27
- self . _api_keys [ name ] = value = self . get ( "api_keys" , {}). get (
28
- name , default
35
+ value = cast (
36
+ Optional [ str ], self . get ( "api_keys" , {}). get ( name , default )
29
37
)
38
+
39
+ self ._api_keys [name ] = value
30
40
return value
31
41
32
- def load_config (self ):
42
+ def load_config (self ) -> None :
33
43
"""(re)loads the bot config from the config file"""
34
44
self ._api_keys .clear ()
35
45
if not self .path .exists ():
@@ -50,7 +60,7 @@ def load_config(self):
50
60
self .update (data )
51
61
logger .debug ("Config loaded from file." )
52
62
53
- def save_config (self ):
63
+ def save_config (self ) -> None :
54
64
"""saves the contents of the config dict to the config file"""
55
65
with self .path .open ("w" , encoding = "utf-8" ) as f :
56
66
json .dump (self , f , indent = 4 )
0 commit comments