File tree Expand file tree Collapse file tree 2 files changed +495
-0
lines changed Expand file tree Collapse file tree 2 files changed +495
-0
lines changed Original file line number Diff line number Diff line change 1+ import json
2+
3+ class Config :
4+ def __init__ (self , config = None , path = 'config.json' ) -> None :
5+ if config is None :
6+ config = {}
7+ self .config = config
8+ self .path = path
9+
10+ def read (self , config_key = None ) -> any :
11+ try :
12+ with open (self .path , "r" ) as config_file :
13+ self .config = json .load (config_file )
14+ except (FileNotFoundError , json .JSONDecodeError ):
15+ self .save (self .config )
16+
17+ if config_key is None :
18+ return self .config
19+ else :
20+ return self .config .get (config_key )
21+
22+ def save (self , config : dict ) -> None :
23+ self .config = config
24+ try :
25+ with open (self .path , "w" ) as config_file :
26+ json .dump (self .config , config_file , indent = 4 )
27+ except (FileNotFoundError , IOError ):
28+ print ("Ошибка записи в файл 'config.json'" )
29+
30+
31+ def update (self , key : str , value : any ) -> None :
32+ self .config = self .read ()
33+ self .config [key ] = value
34+ self .save (self .config )
You can’t perform that action at this time.
0 commit comments