Skip to content

Commit

Permalink
style: Improve pathlib usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgate committed Apr 24, 2024
1 parent fde870e commit ae2e630
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions shell_logger/html_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _append_html(
message = f"Unsupported type: {type(arg)}"
raise RuntimeError(message)

with open(output, "a") as output_file:
with output.open("a") as output_file:
_append_html(output_file, *args)


Expand Down Expand Up @@ -608,7 +608,7 @@ def output_block(
The HTML equivalent of each line of the output in turn.
"""
if isinstance(output, Path):
with open(output) as f:
with output.open() as f:
for string in output_block_html(f, name, cmd_id):
yield string
if isinstance(output, str):
Expand Down
4 changes: 2 additions & 2 deletions shell_logger/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ def tee( # noqa: C901
sys_stderr = None if kwargs.get("quiet_stderr") else sys.stderr
stdout_io = StringIO() if kwargs.get("stdout_str") else None
stderr_io = StringIO() if kwargs.get("stderr_str") else None
stdout_path = open(kwargs.get("stdout_path", os.devnull), "a")
stderr_path = open(kwargs.get("stderr_path", os.devnull), "a")
stdout_path = kwargs.get("stdout_path", Path(os.devnull)).open("a")
stderr_path = kwargs.get("stderr_path", Path(os.devnull)).open("a")
stdout_tee = [sys_stdout, stdout_io, stdout_path]
stderr_tee = [sys_stderr, stderr_io, stderr_path]

Expand Down
16 changes: 8 additions & 8 deletions shell_logger/shell_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def append(path: Path) -> ShellLogger:
path = path.parent / (path.name[:-5] + ".json")

# Deserialize the corresponding JSON object into a ShellLogger.
with open(path, "r") as jf:
with path.open("r") as jf:
loaded_logger = json.load(jf, cls=ShellLoggerDecoder)
return loaded_logger

Expand Down Expand Up @@ -218,7 +218,7 @@ def __init__( # noqa: PLR0913
self.html_file = html_file.resolve()
if self.is_parent():
if self.html_file.exists():
with open(self.html_file, "a") as f:
with self.html_file.open("a") as f:
f.write(
f"<!-- {self.init_time:} Append to log started -->"
)
Expand Down Expand Up @@ -455,14 +455,14 @@ def finalize(self) -> None:
"""
if self.is_parent():
html_text = opening_html_text() + "\n"
with open(self.html_file, "w") as f:
with self.html_file.open("w") as f:
f.write(html_text)

for element in self.to_html():
append_html(element, output=self.html_file)

if self.is_parent():
with open(self.html_file, "a") as html:
with self.html_file.open("a") as html:
html.write(closing_html_text())
html.write("\n")

Expand All @@ -479,7 +479,7 @@ def finalize(self) -> None:
json_file = self.stream_dir / (
self.name.replace(" ", "_") + ".json"
)
with open(json_file, "w") as jf:
with json_file.open("w") as jf:
json.dump(
self, jf, cls=ShellLoggerEncoder, sort_keys=True, indent=4
)
Expand Down Expand Up @@ -558,7 +558,7 @@ def log( # noqa: PLR0913
)

# Print the command to be executed.
with open(stdout_path, "a"), open(stderr_path, "a"):
with stdout_path.open("a"), stderr_path.open("a"):
if verbose:
print(cmd)

Expand Down Expand Up @@ -626,7 +626,7 @@ def _run(self, command: str, **kwargs) -> SimpleNamespace:
kwargs[key] = True

# Change to the directory in which to execute the command.
old_pwd = Path(os.getcwd())
old_pwd = Path.cwd()
if kwargs.get("pwd"):
self.shell.cd(kwargs.get("pwd"))
aux_info = self.auxiliary_information()
Expand Down Expand Up @@ -664,7 +664,7 @@ def _run(self, command: str, **kwargs) -> SimpleNamespace:
completed_process.trace_path = trace_output
completed_process.stats = stats
if kwargs.get("trace_str") and trace_output:
with open(trace_output) as f:
with trace_output.open() as f:
completed_process.trace = f.read()
else:
completed_process.trace = None
Expand Down
20 changes: 10 additions & 10 deletions test/test_shell_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_log_method_creates_tmp_stdout_stderr_files(
print(f"{stderr_file}")

# Make sure the information written to these files is correct.
with open(stdout_file, "r") as out, open(stderr_file, "r") as err:
with stdout_file.open("r") as out, stderr_file.open("r") as err:
out_txt = out.readline()
err_txt = err.readline()
assert "Hello world out" in out_txt
Expand Down Expand Up @@ -236,7 +236,7 @@ def test_child_logger_duration_displayed_correctly_in_html(
child3 = shell_logger.add_child("Child 3")
child3.log("Wait 0.006s", "sleep 0.006")
shell_logger.finalize()
with open(shell_logger.html_file, "r") as hf:
with shell_logger.html_file.open("r") as hf:
html_text = hf.read()
assert child2.duration is not None
assert f"Duration: {child2.duration}" in html_text
Expand All @@ -258,7 +258,7 @@ def test_finalize_creates_json_with_correct_information(
# Load from JSON.
json_file = shell_logger.stream_dir / "Parent.json"
assert json_file.exists()
with open(json_file, "r") as jf:
with json_file.open("r") as jf:
loaded_logger = json.load(jf, cls=ShellLoggerDecoder)

# Parent `ShellLogger`.
Expand Down Expand Up @@ -298,7 +298,7 @@ def test_finalize_creates_html_with_correct_information(
# Load the HTML file.
html_file = shell_logger.stream_dir / "Parent.html"
assert html_file.exists()
with open(html_file, "r") as hf:
with html_file.open("r") as hf:
html_text = hf.read()

# Ensure the command information is present.
Expand Down Expand Up @@ -379,7 +379,7 @@ def test_json_file_can_reproduce_html_file(
# Load the original HTML file's contents.
html_file = shell_logger.log_dir / "Parent.html"
assert html_file.exists()
with open(html_file, "r") as hf:
with html_file.open("r") as hf:
original_html = hf.read()

# Delete the HTML file.
Expand All @@ -388,15 +388,15 @@ def test_json_file_can_reproduce_html_file(
# Load the JSON data.
json_file = shell_logger.stream_dir / "Parent.json"
assert json_file.exists()
with open(json_file, "r") as jf:
with json_file.open("r") as jf:
loaded_logger = json.load(jf, cls=ShellLoggerDecoder)

# Finalize the loaded `ShellLogger` object.
loaded_logger.finalize()

# Load the new HTML file's contents and compare.
assert html_file.exists()
with open(html_file, "r") as hf:
with html_file.open("r") as hf:
new_html = hf.read()
print(f"New Read: {html_file.resolve()}")
assert original_html == new_html
Expand Down Expand Up @@ -810,7 +810,7 @@ def test_sgr_gets_converted_to_html() -> None:
# Load the HTML file and make sure it checks out.
html_file = logger.stream_dir / f"{logger.name}.html"
assert html_file.exists()
with open(html_file, "r") as hf:
with html_file.open("r") as hf:
html_text = hf.read()
assert "\x1B" not in html_text
assert ">Hello</span>" in html_text
Expand Down Expand Up @@ -845,7 +845,7 @@ def test_html_print(capsys: CaptureFixture) -> None:
# Load the HTML file and make sure it checks out.
html_file = logger.stream_dir / f"{logger.name}.html"
assert html_file.exists()
with open(html_file, "r") as hf:
with html_file.open("r") as hf:
html_text = hf.read()
assert "brown fox" not in out
assert "brown fox" not in err
Expand Down Expand Up @@ -893,7 +893,7 @@ def test_append_mode() -> None:
# Load the HTML file and ensure it checks out.
html_file = logger1.stream_dir / f"{logger1.name}.html"
assert html_file.exists()
with open(html_file, "r") as hf:
with html_file.open("r") as hf:
html_text = hf.read()
assert "once" in html_text
assert "ONCE" in html_text
Expand Down

0 comments on commit ae2e630

Please sign in to comment.