Skip to content

Commit 88a2e63

Browse files
committed
follow the custom that load's argument is file pointer, which reduces the responsibility to load file from config class
1 parent 276b0ea commit 88a2e63

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

atcodertools/codegen/code_gen_config.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import TextIO
2+
13
import toml
24

35
INDENT_TYPE_SPACE = 'space'
@@ -29,7 +31,6 @@ def indent(self, depth):
2931
return "\t" * self.indent_width * depth
3032

3133
@classmethod
32-
def load(cls, config_file_path):
33-
with open(config_file_path) as f:
34-
kwargs = toml.load(f).get("codegen")
35-
return CodeGenConfig(**kwargs)
34+
def load(cls, fp: TextIO):
35+
kwargs = toml.load(fp).get("codegen")
36+
return CodeGenConfig(**kwargs)

atcodertools/tools/envgen.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ def check_lang(lang: str):
216216
def get_code_gen_config(config_path: Optional[str] = None):
217217
def _load(path: str):
218218
logging.info("Going to load {} as config".format(path))
219-
return CodeGenConfig.load(path)
219+
with open(path, 'r') as f:
220+
return CodeGenConfig.load(f)
220221

221222
if config_path:
222223
return _load(config_path)

tests/test_codegen.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,17 @@ def get_generator(self, lang: str) -> CodeGenerator:
8383
return self.lang_to_code_generator[lang](f.read())
8484

8585
def test_load_code_gen_config(self):
86-
toml_path = os.path.join(RESOURCE_DIR, "atcodertools-test.toml")
87-
config = CodeGenConfig.load(toml_path)
86+
with open(os.path.join(RESOURCE_DIR, "atcodertools-test.toml"), 'r') as f:
87+
config = CodeGenConfig.load(f)
88+
8889
self.assertEqual(8, config.indent_width)
8990
self.assertEqual(INDENT_TYPE_SPACE, config.indent_type)
9091

9192
def test_init_code_gen_config_with_invalid_parameters(self):
92-
self._expect_error_when_init_config(indent_type='SPACE', indent_width=4)
93-
self._expect_error_when_init_config(indent_type='space', indent_width=-1)
93+
self._expect_error_when_init_config(
94+
indent_type='SPACE', indent_width=4)
95+
self._expect_error_when_init_config(
96+
indent_type='space', indent_width=-1)
9497

9598
def _expect_error_when_init_config(self, **kwargs):
9699
try:

0 commit comments

Comments
 (0)