Skip to content
Snippets Groups Projects
Commit 0ce91f8e authored by Moul's avatar Moul
Browse files

Fix PTH123 `open("foo")` should be replaced by `Path("foo").open()`

Use Path().{read,write}_txt() when possible
parent fd5eb09b
No related branches found
No related tags found
No related merge requests found
...@@ -51,8 +51,7 @@ class G1MonetaryLicense: ...@@ -51,8 +51,7 @@ class G1MonetaryLicense:
""" """
selected_language_code = self.language_prompt() selected_language_code = self.language_prompt()
license_path = self.get_license_path(selected_language_code) license_path = self.get_license_path(selected_language_code)
with open(license_path, encoding="utf-8") as _license: click.echo_via_pager(license_path.read_text(encoding="utf-8"))
click.echo_via_pager(_license.read())
def language_prompt(self) -> str: def language_prompt(self) -> str:
return click.prompt( return click.prompt(
......
...@@ -17,6 +17,7 @@ import math ...@@ -17,6 +17,7 @@ import math
import re import re
import shlex import shlex
import time import time
from pathlib import Path
from typing import List, Optional, Tuple from typing import List, Optional, Tuple
import click import click
...@@ -212,7 +213,7 @@ def parse_file_containing_amounts_recipients( ...@@ -212,7 +213,7 @@ def parse_file_containing_amounts_recipients(
""" """
reference = "" reference = ""
amounts, recipients = [], [] amounts, recipients = [], []
with open(file_path, encoding="utf-8") as file: with Path(file_path).open(encoding="utf-8") as file:
for n, raw_line in enumerate(file): for n, raw_line in enumerate(file):
line = shlex.split(raw_line, True) line = shlex.split(raw_line, True)
if line: if line:
......
...@@ -196,9 +196,7 @@ generated revocation document corresponding to {pubkey_cksum} public key?", ...@@ -196,9 +196,7 @@ generated revocation document corresponding to {pubkey_cksum} public key?",
else: else:
click.echo("Ok, goodbye!") click.echo("Ok, goodbye!")
sys.exit(SUCCESS_EXIT_STATUS) sys.exit(SUCCESS_EXIT_STATUS)
# write doc rev_path.write_text(content, encoding="utf-8")
with open(rev_path, "w", encoding="utf-8") as file:
file.write(content)
click.echo( click.echo(
f"Revocation document file stored into `{path}` for following public key: {pubkey_cksum}", f"Revocation document file stored into `{path}` for following public key: {pubkey_cksum}",
) )
...@@ -218,8 +216,7 @@ def verify_document(doc: str) -> Revocation: ...@@ -218,8 +216,7 @@ def verify_document(doc: str) -> Revocation:
if not Path(doc).is_file(): if not Path(doc).is_file():
sys.exit(f"Error: file {doc} does not exist") sys.exit(f"Error: file {doc} does not exist")
with open(doc, encoding="utf-8") as document: original_doc = Path(doc).read_text(encoding="utf-8")
original_doc = document.read()
try: try:
rev_doc = Revocation.from_signed_raw(original_doc) rev_doc = Revocation.from_signed_raw(original_doc)
......
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with Silkaj. If not, see <https://www.gnu.org/licenses/>. # along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
from pathlib import Path
import pytest import pytest
from click.testing import CliRunner from click.testing import CliRunner
...@@ -50,8 +52,7 @@ def test_parse_file_containing_amounts_recipients( ...@@ -50,8 +52,7 @@ def test_parse_file_containing_amounts_recipients(
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
with open(FILE_PATH, "w", encoding="utf-8") as f: Path(FILE_PATH).write_text(file_content, encoding="utf-8")
f.write(file_content)
amounts, recipients = parse_file_containing_amounts_recipients(FILE_PATH) amounts, recipients = parse_file_containing_amounts_recipients(FILE_PATH)
assert amounts == amounts_exp assert amounts == amounts_exp
assert recipients == recipients_exp assert recipients == recipients_exp
...@@ -77,8 +78,7 @@ SPEC_ERR = "No amounts or recipients specified" ...@@ -77,8 +78,7 @@ SPEC_ERR = "No amounts or recipients specified"
def test_parse_file_containing_amounts_recipients_errors(file_content, error, capsys): def test_parse_file_containing_amounts_recipients_errors(file_content, error, capsys):
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
with open(FILE_PATH, "w", encoding="utf-8") as f: Path(FILE_PATH).write_text(file_content, encoding="utf-8")
f.write(file_content)
with pytest.raises(SystemExit): with pytest.raises(SystemExit):
parse_file_containing_amounts_recipients(FILE_PATH) parse_file_containing_amounts_recipients(FILE_PATH)
assert error in capsys.readouterr().out assert error in capsys.readouterr().out
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with Silkaj. If not, see <https://www.gnu.org/licenses/>. # along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
from pathlib import Path
import pytest import pytest
from click.testing import CliRunner from click.testing import CliRunner
...@@ -41,7 +43,6 @@ pubkey_seedhex_authfile = ( ...@@ -41,7 +43,6 @@ pubkey_seedhex_authfile = (
) )
def test_checksum_command(command, excepted_output): def test_checksum_command(command, excepted_output):
with CliRunner().isolated_filesystem(): with CliRunner().isolated_filesystem():
with open("authfile", "w", encoding="utf-8") as f: Path("authfile").write_text(pubkey_seedhex_authfile, encoding="utf-8")
f.write(pubkey_seedhex_authfile)
result = CliRunner().invoke(cli, args=command) result = CliRunner().invoke(cli, args=command)
assert result.output == f"{excepted_output}\n" assert result.output == f"{excepted_output}\n"
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with Silkaj. If not, see <https://www.gnu.org/licenses/>. # along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
...@@ -78,8 +79,7 @@ def test_long_language_code_handling(): ...@@ -78,8 +79,7 @@ def test_long_language_code_handling():
license_path = g1ml.get_license_path(language_code) license_path = g1ml.get_license_path(language_code)
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
with open(license_path, "w", encoding="utf-8") as f: license_path.write_text(content, encoding="utf-8")
f.write(content)
result = runner.invoke(cli.cli, args=["license"], input=language_code) result = runner.invoke(cli.cli, args=["license"], input=language_code)
assert language_code in result.output assert language_code in result.output
assert content in result.output assert content in result.output
......
...@@ -184,8 +184,7 @@ def test_revocation_cli_dry_run(subcommand, expected_warn, monkeypatch): ...@@ -184,8 +184,7 @@ def test_revocation_cli_dry_run(subcommand, expected_warn, monkeypatch):
file = "revocation.txt" file = "revocation.txt"
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
with open(file, "w", encoding="utf-8") as f: Path(file).write_text(REV_DOC.signed_raw(), encoding="utf-8")
f.write(REV_DOC.signed_raw())
result = runner.invoke(cli, args=command) result = runner.invoke(cli, args=command)
assert "6upqFiJ66WV6N3bPc8x8y7rXT3syqKRmwnVyunCtEj7o" in result.output assert "6upqFiJ66WV6N3bPc8x8y7rXT3syqKRmwnVyunCtEj7o" in result.output
assert "Version: 10" in result.output assert "Version: 10" in result.output
...@@ -433,8 +432,7 @@ def test_revocation_cli_verify( ...@@ -433,8 +432,7 @@ def test_revocation_cli_verify(
# verify file # verify file
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
with open(file, "w", encoding="utf-8") as f: Path(file).write_text(doc.signed_raw(), encoding="utf-8")
f.write(doc.signed_raw())
result = runner.invoke(cli, args=command) result = runner.invoke(cli, args=command)
for expect in expected: for expect in expected:
assert expect in result.output assert expect in result.output
...@@ -706,8 +704,7 @@ def test_revocation_cli_publish( ...@@ -706,8 +704,7 @@ def test_revocation_cli_publish(
# test publication # test publication
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
with open(file, "w", encoding="utf-8") as f: Path(file).write_text(doc.signed_raw(), encoding="utf-8")
f.write(doc.signed_raw())
result = runner.invoke(cli, args=command, input=user_input) result = runner.invoke(cli, args=command, input=user_input)
if user_input == "yes\n" and not dry_run: if user_input == "yes\n" and not dry_run:
patched_send_bma_revoke.assert_called_once_with( patched_send_bma_revoke.assert_called_once_with(
...@@ -794,8 +791,7 @@ def test_revocation_cli_publish_send_errors( ...@@ -794,8 +791,7 @@ def test_revocation_cli_publish_send_errors(
# test publication # test publication
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
with open(file, "w", encoding="utf-8") as f: Path(file).write_text(REV_DOC.signed_raw(), encoding="utf-8")
f.write(REV_DOC.signed_raw())
result = runner.invoke(cli, args=command, input=user_input) result = runner.invoke(cli, args=command, input=user_input)
for expect in expected: for expect in expected:
assert expect in result.output assert expect in result.output
...@@ -974,16 +970,14 @@ def test_save_doc(path, rev_1, rev_2, pubkey, capsys, monkeypatch): ...@@ -974,16 +970,14 @@ def test_save_doc(path, rev_1, rev_2, pubkey, capsys, monkeypatch):
test_path = Path(path) test_path = Path(path)
revocation.save_doc(path, rev_1.signed_raw(), pubkey) revocation.save_doc(path, rev_1.signed_raw(), pubkey)
assert test_path.is_file() assert test_path.is_file()
with open(path, encoding="utf-8") as f: assert Path(path).read_text(encoding="utf-8") == rev_1.signed_raw()
assert f.read() == rev_1.signed_raw()
# test file is overwritten if confirm # test file is overwritten if confirm
monkeypatch.setattr(click, "confirm", value=conf_true) monkeypatch.setattr(click, "confirm", value=conf_true)
revocation.save_doc(path, rev_2.signed_raw(), pubkey) revocation.save_doc(path, rev_2.signed_raw(), pubkey)
expected_confirm = f"Revocation document file stored into `{path}` \ expected_confirm = f"Revocation document file stored into `{path}` \
for following public key: {gen_pubkey_checksum(pubkey)}" for following public key: {gen_pubkey_checksum(pubkey)}"
assert expected_confirm in capsys.readouterr().out assert expected_confirm in capsys.readouterr().out
with open(path, encoding="utf-8") as f: assert Path(path).read_text(encoding="utf-8") == rev_2.signed_raw()
assert f.read() == rev_2.signed_raw()
# test file is not overwritten if not confirm # test file is not overwritten if not confirm
monkeypatch.setattr(click, "confirm", value=conf_false) monkeypatch.setattr(click, "confirm", value=conf_false)
with pytest.raises(SystemExit) as pytest_exit: with pytest.raises(SystemExit) as pytest_exit:
...@@ -992,8 +986,7 @@ for following public key: {gen_pubkey_checksum(pubkey)}" ...@@ -992,8 +986,7 @@ for following public key: {gen_pubkey_checksum(pubkey)}"
assert pytest_exit.value.code == SUCCESS_EXIT_STATUS assert pytest_exit.value.code == SUCCESS_EXIT_STATUS
expected_confirm = "Ok, goodbye!" expected_confirm = "Ok, goodbye!"
assert expected_confirm in capsys.readouterr().out assert expected_confirm in capsys.readouterr().out
with open(path, encoding="utf-8") as f: assert Path(path).read_text(encoding="utf-8") == rev_2.signed_raw()
assert f.read() == rev_2.signed_raw()
# test verify_document # test verify_document
...@@ -1015,8 +1008,7 @@ def test_verify_document(doc, lookup, capsys, monkeypatch): ...@@ -1015,8 +1008,7 @@ def test_verify_document(doc, lookup, capsys, monkeypatch):
# test # test
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
with open(path, "w", encoding="utf-8") as f: Path(path).write_text(doc.signed_raw(), encoding="utf-8")
f.write(doc.signed_raw())
result = revocation.verify_document(path) result = revocation.verify_document(path)
display = capsys.readouterr().out display = capsys.readouterr().out
if len(lookup["results"]) > 1: if len(lookup["results"]) > 1:
...@@ -1054,8 +1046,7 @@ def test_verify_document_missing_id(doc, lookup, capsys, monkeypatch): ...@@ -1054,8 +1046,7 @@ def test_verify_document_missing_id(doc, lookup, capsys, monkeypatch):
# test # test
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
with open(path, "w", encoding="utf-8") as f: Path(path).write_text(doc.signed_raw(), encoding="utf-8")
f.write(doc.signed_raw())
with pytest.raises(SystemExit) as pytest_exit: with pytest.raises(SystemExit) as pytest_exit:
revocation.verify_document(path) revocation.verify_document(path)
assert pytest_exit.type == SystemExit assert pytest_exit.type == SystemExit
...@@ -1089,11 +1080,11 @@ def test_verify_document_sign_errors(doc, currency, monkeypatch): ...@@ -1089,11 +1080,11 @@ def test_verify_document_sign_errors(doc, currency, monkeypatch):
# test # test
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
with open(path, "w", encoding="utf-8") as f: file = Path(path)
if isinstance(doc, str): if isinstance(doc, str):
f.write(doc) file.write_text(doc, encoding="utf-8")
elif isinstance(doc, Revocation): elif isinstance(doc, Revocation):
f.write(doc.signed_raw()) file.write_text(doc.signed_raw(), encoding="utf-8")
with pytest.raises(SystemExit) as pytest_exit: with pytest.raises(SystemExit) as pytest_exit:
revocation.verify_document(path) revocation.verify_document(path)
assert pytest_exit.type == SystemExit assert pytest_exit.type == SystemExit
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment