1
1
from collections .abc import MutableMapping
2
2
from pathlib import Path
3
+ from typing import Any
3
4
4
5
import tomlkit
5
6
6
- data_home = Path .home () / "Library/Application Support" / " shelloracle"
7
+ data_home = Path .home () / ". shelloracle"
7
8
8
9
9
10
def _default_config () -> tomlkit .TOMLDocument :
@@ -20,27 +21,35 @@ class Configuration(MutableMapping):
20
21
def __init__ (self ) -> None :
21
22
self ._ensure_config_exists ()
22
23
23
- def __getitem__ (self , item ) :
24
+ def __getitem__ (self , item : str ) -> dict :
24
25
with self .filepath .open ("r" ) as file :
25
26
config = tomlkit .load (file )
26
27
return config [item ]
27
28
28
- def __setitem__ (self , key , value ) :
29
+ def __setitem__ (self , key : str , value : Any ) -> None :
29
30
with self .filepath .open ("r" ) as file :
30
31
config = tomlkit .load (file )
31
32
config [key ] = value
32
33
config .multiline = True
33
34
with self .filepath .open ("w" ) as file :
34
35
tomlkit .dump (config , file )
35
36
36
- def __delitem__ (self , key ):
37
- raise NotImplementedError ()
37
+ def __delitem__ (self , key : str ) -> None :
38
+ with self .filepath .open ("r" ) as file :
39
+ config = tomlkit .load (file )
40
+ del config [key ]
41
+ with self .filepath .open ("w" ) as file :
42
+ tomlkit .dump (config , file )
38
43
39
- def __iter__ (self ):
40
- raise NotImplementedError ()
44
+ def __iter__ (self ) -> None :
45
+ with self .filepath .open ("r" ) as file :
46
+ config = tomlkit .load (file )
47
+ return iter (config )
41
48
42
49
def __len__ (self ) -> int :
43
- raise NotImplementedError ()
50
+ with self .filepath .open ("r" ) as file :
51
+ config = tomlkit .load (file )
52
+ return len (config )
44
53
45
54
def _ensure_config_exists (self ) -> None :
46
55
if self .filepath .exists ():
0 commit comments