1
1
from argparse import Namespace
2
2
from typing import TextIO , Dict , Any , Optional
3
+ from enum import Enum
3
4
4
5
import toml
5
6
6
7
from atcodertools .codegen .code_style_config import CodeStyleConfig
7
8
from atcodertools .config .etc_config import EtcConfig
8
9
from atcodertools .config .postprocess_config import PostprocessConfig
10
+ from atcodertools .config .tester_config import TesterConfig
11
+
12
+
13
+ class ConfigType (Enum ):
14
+ CODESTYLE = "codestyle"
15
+ POSTPROCESS = "postprocess"
16
+ TESTER = "tester"
17
+ ETC = "etc"
9
18
10
19
11
20
def _update_config_dict (target_dic : Dict [str , Any ], update_dic : Dict [str , Any ]):
@@ -15,45 +24,82 @@ def _update_config_dict(target_dic: Dict[str, Any], update_dic: Dict[str, Any]):
15
24
}
16
25
17
26
27
+ def get_config_dic (config_dic , config_type : ConfigType , lang = None ):
28
+ result = dict ()
29
+ d = config_dic .get (config_type .value , {})
30
+ lang_dic = {}
31
+ for k , v in d .items ():
32
+ if type (v ) is dict :
33
+ if k == lang :
34
+ lang_dic = v
35
+ else :
36
+ result [k ] = v
37
+ result = _update_config_dict (result , lang_dic )
38
+ return result
39
+
40
+
18
41
class Config :
19
42
20
43
def __init__ (self ,
21
44
code_style_config : CodeStyleConfig = CodeStyleConfig (),
22
45
postprocess_config : PostprocessConfig = PostprocessConfig (),
46
+ tester_config : TesterConfig = TesterConfig (),
23
47
etc_config : EtcConfig = EtcConfig ()
24
48
):
25
49
self .code_style_config = code_style_config
26
50
self .postprocess_config = postprocess_config
51
+ self .tester_config = tester_config
27
52
self .etc_config = etc_config
28
53
29
54
@classmethod
30
- def load (cls , fp : TextIO , args : Optional [Namespace ] = None ):
55
+ def load (cls , fp : TextIO , get_config_type , args : Optional [Namespace ] = None , lang = None ):
31
56
"""
32
57
:param fp: .toml file's file pointer
33
58
:param args: command line arguments
34
59
:return: Config instance
35
60
"""
36
61
config_dic = toml .load (fp )
37
62
38
- code_style_config_dic = config_dic .get ('codestyle' , {})
39
- postprocess_config_dic = config_dic .get ('postprocess' , {})
40
- etc_config_dic = config_dic .get ('etc' , {})
41
-
42
- if args :
43
- code_style_config_dic = _update_config_dict (code_style_config_dic ,
44
- dict (
45
- template_file = args .template ,
46
- workspace_dir = args .workspace ,
47
- lang = args .lang ))
48
- etc_config_dic = _update_config_dict (etc_config_dic ,
49
- dict (
50
- download_without_login = args .without_login ,
51
- parallel_download = args .parallel ,
52
- save_no_session_cache = args .save_no_session_cache ,
53
- skip_existing_problems = args .skip_existing_problems ))
54
-
55
- return Config (
56
- code_style_config = CodeStyleConfig (** code_style_config_dic ),
57
- postprocess_config = PostprocessConfig (** postprocess_config_dic ),
58
- etc_config = EtcConfig (** etc_config_dic )
59
- )
63
+ result = Config ()
64
+ if not lang :
65
+ if args and args .lang :
66
+ lang = args .lang
67
+ elif "codestyle" in config_dic :
68
+ lang = config_dic ["codestyle" ].get ("lang" , None )
69
+
70
+ if ConfigType .CODESTYLE in get_config_type :
71
+ code_style_config_dic = get_config_dic (
72
+ config_dic , ConfigType .CODESTYLE , lang )
73
+ if args :
74
+ code_style_config_dic = _update_config_dict (code_style_config_dic ,
75
+ dict (
76
+ template_file = args .template ,
77
+ workspace_dir = args .workspace ,
78
+ lang = lang ))
79
+ result .code_style_config = CodeStyleConfig (** code_style_config_dic )
80
+ if ConfigType .POSTPROCESS in get_config_type :
81
+ postprocess_config_dic = get_config_dic (
82
+ config_dic , ConfigType .POSTPROCESS )
83
+ result .postprocess_config = PostprocessConfig (
84
+ ** postprocess_config_dic )
85
+ if ConfigType .TESTER in get_config_type :
86
+ tester_config_dic = get_config_dic (
87
+ config_dic , ConfigType .TESTER , lang )
88
+ if args :
89
+ tester_config_dic = _update_config_dict (tester_config_dic ,
90
+ dict (compile_before_testing = args .compile_before_testing ,
91
+ compile_only_when_diff_detected = args .compile_only_when_diff_detected ,
92
+ compile_command = args .compile_command ))
93
+ result .tester_config = TesterConfig (** tester_config_dic )
94
+ if ConfigType .ETC in get_config_type :
95
+ etc_config_dic = get_config_dic (config_dic , ConfigType .ETC )
96
+ if args :
97
+ etc_config_dic = _update_config_dict (etc_config_dic ,
98
+ dict (
99
+ download_without_login = args .without_login ,
100
+ parallel_download = args .parallel ,
101
+ save_no_session_cache = args .save_no_session_cache ,
102
+ skip_existing_problems = args .skip_existing_problems ))
103
+ result .etc_config = EtcConfig (** etc_config_dic )
104
+
105
+ return result
0 commit comments