Skip to content

Commit f41437a

Browse files
committed
use pathlib operations where possible
1 parent dceab52 commit f41437a

File tree

3 files changed

+22
-31
lines changed

3 files changed

+22
-31
lines changed

generic_parser/entrypoint_parser.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,8 @@ def _handle_kwargs(self, kwargs):
369369
f"Only '{ID_JSON:s}' and '{ID_SECTION:s}'"
370370
" arguments are allowed, when using a json file."
371371
)
372-
with open(kwargs[ID_JSON]) as json_file:
373-
json_dict = json.load(json_file)
374372

373+
json_dict = json.loads(Path(kwargs[ID_JSON]).read_text())
375374
if ID_SECTION in kwargs:
376375
json_dict = json_dict[kwargs[ID_SECTION]]
377376

@@ -415,7 +414,7 @@ def _read_config(self, cfgfile_path, section=None):
415414
# create new config parser, as it keeps defaults between files
416415
cfgparse = self._create_config_parser()
417416

418-
with open(cfgfile_path) as config_file:
417+
with Path(cfgfile_path).open() as config_file:
419418
cfgparse.read_file(config_file)
420419

421420
sections = cfgparse.sections()
@@ -797,5 +796,4 @@ def _to_key_value_str(key, value):
797796
else:
798797
lines += f"; {' '.join(unknown)}\n"
799798

800-
with open(filepath, "w") as f:
801-
f.write(lines)
799+
Path(filepath).write_text(lines)

generic_parser/tools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import sys
1111
from contextlib import contextmanager
1212
from io import StringIO
13+
from pathlib import Path
1314

1415
LOG = logging.getLogger(__name__)
1516

@@ -111,7 +112,7 @@ def silence():
111112
"""
112113
Suppress all console output. ``sys.stdout`` and ``sys.stderr`` are rerouted to ``devnull``.
113114
"""
114-
with open(os.devnull, "w") as devnull, log_out(stdout=devnull, stderr=devnull):
115+
with Path(os.devnull).open("w") as devnull, log_out(stdout=devnull, stderr=devnull):
115116
yield
116117

117118

tests/test_entrypoint.py

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,17 @@ def test_as_argv(): # almost identical to above
181181

182182
def test_as_config(tmp_path):
183183
cfg_file = tmp_path / "config.ini"
184-
with open(cfg_file, "w") as f:
185-
f.write(
186-
"\n".join(
187-
[
188-
"[Section]",
189-
"name = 'myname'",
190-
"int = 3",
191-
"list = [4, 5, 6]",
192-
"unknown = 'other'",
193-
]
194-
)
184+
Path(cfg_file).write_text(
185+
"\n".join(
186+
[
187+
"[Section]",
188+
"name = 'myname'",
189+
"int = 3",
190+
"list = [4, 5, 6]",
191+
"unknown = 'other'",
192+
]
195193
)
194+
)
196195

197196
# test config as kwarg
198197
opt1, unknown1 = paramtest_function(entry_cfg=cfg_file, section="Section")
@@ -309,8 +308,7 @@ def test_save_cli_options_cfg(tmp_path):
309308
save_options_to_config(cfg_file, opt, unknown)
310309
opt_load, unknown_load = paramtest_function(entry_cfg=cfg_file)
311310

312-
with open(cfg_file) as f:
313-
content = f.read()
311+
content = Path(cfg_file).read_text()
314312
assert "Unknown" in content
315313
assert "--other" in content
316314

@@ -352,16 +350,13 @@ def fun(opt):
352350
return opt
353351

354352
cfg_quotes = tmp_path / "config_quotes.ini"
355-
with open(cfg_quotes, "w") as f:
356-
f.write("[Section]\nname = 'My String with Spaces'")
353+
Path(cfg_quotes).write_text("[Section]\nname = 'My String with Spaces'")
357354

358355
cfg_doublequotes = tmp_path / "config_doublequotes.ini"
359-
with open(cfg_doublequotes, "w") as f:
360-
f.write('[Section]\nname = "My String with Spaces"')
356+
Path(cfg_doublequotes).write_text('[Section]\nname = "My String with Spaces"')
361357

362358
cfg_noquotes = tmp_path / "config_noquotes.ini"
363-
with open(cfg_noquotes, "w") as f:
364-
f.write("[Section]\nname = My String with Spaces")
359+
Path(cfg_noquotes).write_text("[Section]\nname = My String with Spaces")
365360

366361
opt_quotes = fun(entry_cfg=cfg_quotes)
367362
opt_doublequotes = fun(entry_cfg=cfg_doublequotes)
@@ -520,8 +515,7 @@ def fun(opt):
520515
assert opt.bos == "myString"
521516

522517
cfg_file = tmp_path / "bos.ini"
523-
with open(cfg_file, "w") as f:
524-
f.write("[Section]\nbos = 'myString'")
518+
Path(cfg_file).write_text("[Section]\nbos = 'myString'")
525519
opt = fun(entry_cfg=cfg_file)
526520
assert opt.bos == "myString"
527521

@@ -538,8 +532,7 @@ def fun(opt):
538532
return opt
539533

540534
cfg_file = tmp_path / "bos.ini"
541-
with open(cfg_file, "w") as f:
542-
f.write("[Section]\nbos1 = 'myString'\nbos2 = True")
535+
Path(cfg_file).write_text("[Section]\nbos1 = 'myString'\nbos2 = True")
543536
opt = fun(entry_cfg=cfg_file)
544537
assert opt.bos1 == "myString"
545538
assert opt.bos2 is True
@@ -578,8 +571,7 @@ def fun(opt):
578571
return opt
579572

580573
cfg_file = tmp_path / "bol.ini"
581-
with open(cfg_file, "w") as f:
582-
f.write("[Section]\nbol1 = 1,2\nbol2 = True")
574+
Path(cfg_file).write_text("[Section]\nbol1 = 1,2\nbol2 = True")
583575
opt = fun(entry_cfg=cfg_file)
584576
assert opt.bol1 == [1, 2]
585577
assert opt.bol2 is True

0 commit comments

Comments
 (0)