|
1 |
| -from typing import TextIO |
| 1 | +from argparse import Namespace |
| 2 | +from typing import TextIO, Dict, Any, Optional |
2 | 3 |
|
3 | 4 | import toml
|
4 | 5 |
|
5 | 6 | from atcodertools.codegen.code_style_config import CodeStyleConfig
|
6 |
| -from atcodertools.tools.postprocess_config import PostprocessConfig |
| 7 | +from atcodertools.config.etc_config import EtcConfig |
| 8 | +from atcodertools.config.postprocess_config import PostprocessConfig |
| 9 | + |
| 10 | + |
| 11 | +def _update_config_dict(target_dic: Dict[str, Any], update_dic: Dict[str, Any]): |
| 12 | + return { |
| 13 | + **target_dic, |
| 14 | + **dict((k, v) for k, v in update_dic.items() if v is not None) |
| 15 | + } |
7 | 16 |
|
8 | 17 |
|
9 | 18 | class Config:
|
10 | 19 |
|
11 | 20 | def __init__(self,
|
12 | 21 | code_style_config: CodeStyleConfig = CodeStyleConfig(),
|
13 | 22 | postprocess_config: PostprocessConfig = PostprocessConfig(),
|
| 23 | + etc_config: EtcConfig = EtcConfig() |
14 | 24 | ):
|
15 | 25 | self.code_style_config = code_style_config
|
16 | 26 | self.postprocess_config = postprocess_config
|
| 27 | + self.etc_config = etc_config |
17 | 28 |
|
18 | 29 | @classmethod
|
19 |
| - def load(cls, fp: TextIO): |
| 30 | + def load(cls, fp: TextIO, args: Optional[Namespace] = None): |
| 31 | + """ |
| 32 | + :param fp: .toml file's file pointer |
| 33 | + :param args: command line arguments |
| 34 | + :return: Config instance |
| 35 | + """ |
20 | 36 | config_dic = toml.load(fp)
|
| 37 | + |
21 | 38 | code_style_config_dic = config_dic.get('codestyle', {})
|
22 | 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(template_file=args.template, |
| 45 | + workspace_dir=args.workspace, |
| 46 | + lang=args.lang)) |
| 47 | + etc_config_dic = _update_config_dict(etc_config_dic, |
| 48 | + dict(download_without_login=args.without_login, |
| 49 | + parallel_download=args.parallel, |
| 50 | + save_no_session_cache=args.save_no_session_cache)) |
| 51 | + |
23 | 52 | return Config(
|
24 | 53 | code_style_config=CodeStyleConfig(**code_style_config_dic),
|
25 | 54 | postprocess_config=PostprocessConfig(**postprocess_config_dic),
|
| 55 | + etc_config=EtcConfig(**etc_config_dic) |
26 | 56 | )
|
0 commit comments