Skip to content

Commit 0a5a9aa

Browse files
Anything configurable from command line is configurable from toml (#80)
* Move configs * Refactor config * All configurations are now available with toml * Update README
1 parent 06ad3cf commit 0a5a9aa

File tree

13 files changed

+153
-99
lines changed

13 files changed

+153
-99
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,31 @@ optional arguments:
132132

133133
- コードスタイルの設定が幅4のスペースインデントである
134134
- コード生成テンプレートとして`~/my_template.cpp`を使う
135+
- ワークスペースのルートは `~/atcoder-workspace/`
136+
- 言語設定は `cpp` (提出時もしくはデフォルトのコードジェネレーター生成時に使われます)
135137
- 問題用ディレクトリ内で毎回`clang-format`を実行して、最後に`CMakeLists.txt`(空)をコンテスト用ディレクトリに生成する
136-
- カスタムコードジェネレーター `custom_code_generator.py`を指定す
138+
- カスタムコードジェネレーター `custom_code_generator.py`を指定する
139+
- AtCoderにログインせずにダウンロードを行う機能を使わない (公開コンテストに対してのみ可能)
140+
- データの並列ダウンロードを無効にする
141+
- ログイン情報のクッキーを保存する
137142

138143
```$xslt
139144
[codestyle]
140145
indent_type = 'space' # 'tab' or 'space'
141146
indent_width = 4
142147
template_file='~/my_template.cpp'
148+
workspace_dir = '~/atcoder-workspace/'
149+
lang = 'cpp' # 'cpp' or 'java' (Currently)
143150
[postprocess]
144151
exec_on_each_problem_dir='clang-format -i ./*.cpp'
145152
exec_on_contest_dir='touch CMakeLists.txt'
146153
code_generator_file="~/custom_code_generator.py"
154+
155+
[etc]
156+
download_without_login=false
157+
parallel_download=false
158+
save_no_session_cache=false
159+
147160
```
148161

149162
### カスタムコードジェネレーター

atcodertools/codegen/code_style_config.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import importlib.machinery as imm
22
import os
3+
from os.path import expanduser
34
from typing import Optional
4-
55
from atcodertools.fileutils.normalize import normalize_path
6+
from atcodertools.tools.templates import get_default_template_path
67

78
INDENT_TYPE_SPACE = 'space'
89
INDENT_TYPE_TAB = 'tab'
@@ -12,13 +13,19 @@ class CodeStyleConfigInitError(Exception):
1213
pass
1314

1415

16+
DEFAULT_WORKSPACE_DIR_PATH = os.path.join(expanduser("~"), "atcoder-workspace")
17+
SUPPORTED_LANGUAGES = ["cpp", "java"]
18+
19+
1520
class CodeStyleConfig:
1621

1722
def __init__(self,
1823
indent_type: str = INDENT_TYPE_SPACE,
1924
indent_width: int = 4,
2025
code_generator_file: Optional[str] = None,
2126
template_file: Optional[str] = None,
27+
workspace_dir: Optional[str] = None,
28+
lang: str = "cpp",
2229
):
2330
code_generator_file = normalize_path(code_generator_file)
2431
template_file = normalize_path(template_file)
@@ -41,9 +48,12 @@ def __init__(self,
4148
template_file)
4249
)
4350

51+
if lang and lang not in SUPPORTED_LANGUAGES:
52+
raise CodeStyleConfigInitError(
53+
"language must be one of {}".format(SUPPORTED_LANGUAGES))
54+
4455
self.indent_type = indent_type
4556
self.indent_width = indent_width
46-
self.code_generator = None
4757

4858
if code_generator_file is not None:
4959
try:
@@ -53,7 +63,20 @@ def __init__(self,
5363
except AttributeError as e:
5464
raise CodeStyleConfigInitError(e, "Error while loading {}".format(
5565
code_generator_file))
56-
self.template_file = template_file
66+
else:
67+
assert lang is not None
68+
if lang == "cpp":
69+
from atcodertools.codegen.code_generators import cpp
70+
self.code_generator = cpp.main
71+
else:
72+
from atcodertools.codegen.code_generators import java
73+
self.code_generator = java.main
74+
75+
self.template_file = normalize_path(
76+
template_file or get_default_template_path(lang))
77+
self.workspace_dir = normalize_path(
78+
workspace_dir or DEFAULT_WORKSPACE_DIR_PATH)
79+
self.lang = lang
5780

5881
def indent(self, depth):
5982
if self.indent_type == INDENT_TYPE_SPACE:

atcodertools/config/config.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,56 @@
1-
from typing import TextIO
1+
from argparse import Namespace
2+
from typing import TextIO, Dict, Any, Optional
23

34
import toml
45

56
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+
}
716

817

918
class Config:
1019

1120
def __init__(self,
1221
code_style_config: CodeStyleConfig = CodeStyleConfig(),
1322
postprocess_config: PostprocessConfig = PostprocessConfig(),
23+
etc_config: EtcConfig = EtcConfig()
1424
):
1525
self.code_style_config = code_style_config
1626
self.postprocess_config = postprocess_config
27+
self.etc_config = etc_config
1728

1829
@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+
"""
2036
config_dic = toml.load(fp)
37+
2138
code_style_config_dic = config_dic.get('codestyle', {})
2239
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+
2352
return Config(
2453
code_style_config=CodeStyleConfig(**code_style_config_dic),
2554
postprocess_config=PostprocessConfig(**postprocess_config_dic),
55+
etc_config=EtcConfig(**etc_config_dic)
2656
)

atcodertools/config/etc_config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class EtcConfig:
2+
3+
def __init__(self,
4+
download_without_login: str = False,
5+
parallel_download: bool = False,
6+
save_no_session_cache: bool = False,
7+
):
8+
self.download_without_login = download_without_login
9+
self.parallel_download = parallel_download
10+
self.save_no_session_cache = save_no_session_cache
File renamed without changes.

atcodertools/tools/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import os
2+
3+
4+
def get_default_config_path():
5+
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "atcodertools-default.toml")

atcodertools/tools/atcodertools-default.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
indent_type = 'space' # 'tab' or 'space'
33
indent_width = 4
44

5+
workspace_dir = '~/atcoder-workspace/'
6+
lang = 'cpp' # 'cpp' or 'java' (Currently)
7+
58
## template_file is a file path to template code
69
#template_file=
710

811
## code_generator_file is a path to .py file defining custom code generator function.
912
## Please check README.md for details.
1013
#code_generator_file=
1114

15+
1216
[postprocess]
1317
## exec_on_each_problem_dir is executed after every problem directory's creation
1418
## with its problem directory as cwd like arc001/A
@@ -17,3 +21,8 @@ indent_width = 4
1721
## exec_on_contest_dir is executed once after `atcoder-tools gen`
1822
## with the contest directory as cwd like arc001/
1923
#exec_on_contest_dir=
24+
25+
[etc]
26+
download_without_login=false
27+
parallel_download=false
28+
save_no_session_cache=false

0 commit comments

Comments
 (0)