Skip to content

Commit

Permalink
style: fix PTH123
Browse files Browse the repository at this point in the history
  • Loading branch information
patrit committed Nov 18, 2023
1 parent 2302541 commit 0f1b58f
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 27 deletions.
1 change: 0 additions & 1 deletion gitopscli/commands/sync_apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
from dataclasses import dataclass
from pathlib import Path # noqa: F401

from gitopscli.appconfig_api.app_tenant_config import create_app_tenant_config_from_repo
from gitopscli.appconfig_api.root_repo import create_root_repo
Expand Down
8 changes: 4 additions & 4 deletions gitopscli/git_api/git_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ def __get_repo(self) -> Repo:
raise GitOpsException("Repository not cloned yet!")

def __create_credentials_file(self, username: str, password: str) -> str:
file_path = f"{self.__tmp_dir}/credentials.sh"
with open(file_path, "w", encoding=locale.getpreferredencoding(do_setlocale=False)) as text_file:
file_path = Path(f"{self.__tmp_dir}/credentials.sh")
with file_path.open("w", encoding=locale.getpreferredencoding(do_setlocale=False)) as text_file:
text_file.write("#!/bin/sh\n")
text_file.write(f"echo username='{username}'\n")
text_file.write(f"echo password='{password}'\n")
Path(file_path).chmod(0o700)
return file_path
file_path.chmod(0o700)
return str(file_path)
5 changes: 3 additions & 2 deletions gitopscli/io_api/yaml_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import locale
from io import StringIO
from pathlib import Path
from typing import Any

from jsonpath_ng.exceptions import JSONPathError
Expand All @@ -15,15 +16,15 @@ class YAMLException(Exception): # noqa: N818


def yaml_file_load(file_path: str) -> Any:
with open(file_path, encoding=locale.getpreferredencoding(do_setlocale=False)) as stream:
with Path(file_path).open(encoding=locale.getpreferredencoding(do_setlocale=False)) as stream:
try:
return YAML_INSTANCE.load(stream)
except YAMLError as ex:
raise YAMLException(f"Error parsing YAML file: {file_path}") from ex


def yaml_file_dump(yaml: Any, file_path: str) -> None:
with open(file_path, "w+", encoding=locale.getpreferredencoding(do_setlocale=False)) as stream:
with Path(file_path).open("w+", encoding=locale.getpreferredencoding(do_setlocale=False)) as stream:
YAML_INSTANCE.dump(yaml, stream)


Expand Down
6 changes: 0 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,3 @@ ignore = [
"ANN", # https://docs.astral.sh/ruff/rules/#flake8-annotations-ann
"PT009" # https://docs.astral.sh/ruff/rules/pytest-unittest-assertion/
]
# the following exclusions have been introduced to prevent huge changes
# feel free to remove them and fix the code
"gitopscli/git_api/git_repo.py" = ["PTH123"]
"gitopscli/io_api/yaml_util.py" = ["PTH123"]
"tests/git_api/test_git_repo.py" = ["PTH123"]
"tests/io_api/test_yaml_util.py" = ["PTH123"]
2 changes: 0 additions & 2 deletions tests/commands/test_sync_apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import os
import posixpath
import unittest
from pathlib import Path
from unittest.mock import call, patch
Expand Down Expand Up @@ -41,7 +40,6 @@ def setUp(self):
self.os_mock = patcher.start()
self.mock_manager.attach_mock(self.os_mock, "os")

self.os_mock.path.join.side_effect = posixpath.join # tests are designed to emulate posix env
self.os_mock.listdir.return_value = ["my-app"]

patcher_path = patch("gitopscli.appconfig_api.app_tenant_config.Path", spec_set=Path)
Expand Down
20 changes: 10 additions & 10 deletions tests/git_api/test_git_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __create_tmp_dir(self):

def __read_file(self, filename):
self.assertTrue(filename)
with open(filename) as input_stream:
with Path(filename).open() as input_stream:
return input_stream.read()

def __create_origin(self):
Expand All @@ -39,14 +39,14 @@ def __create_origin(self):
repo.config_writer().set_value("user", "name", git_user).release()
repo.config_writer().set_value("user", "email", git_email).release()

with open(f"{repo_dir}/README.md", "w") as readme:
with Path(f"{repo_dir}/README.md").open("w") as readme:
readme.write("master branch readme")
repo.git.add("--all")
repo.git.commit("-m", "initial commit", "--author", f"{git_user} <{git_email}>")

repo.create_head("xyz").checkout()

with open(f"{repo_dir}/README.md", "w") as readme:
with Path(f"{repo_dir}/README.md").open("w") as readme:
readme.write("xyz branch readme")
repo.git.add("--all")
repo.git.commit("-m", "xyz brach commit", "--author", f"{git_user} <{git_email}>")
Expand Down Expand Up @@ -204,9 +204,9 @@ def test_commit(self, logging_mock):
testee.clone()
logging_mock.reset_mock()

with open(testee.get_full_file_path("foo.md"), "w") as outfile:
with Path(testee.get_full_file_path("foo.md")).open("w") as outfile:
outfile.write("new file")
with open(testee.get_full_file_path("README.md"), "w") as outfile:
with Path(testee.get_full_file_path("README.md")).open("w") as outfile:
outfile.write("new content")

commit_hash = testee.commit(
Expand Down Expand Up @@ -237,9 +237,9 @@ def test_commit_with_custom_author(self, logging_mock):
testee.clone()
logging_mock.reset_mock()

with open(testee.get_full_file_path("foo.md"), "w") as outfile:
with Path(testee.get_full_file_path("foo.md")).open("w") as outfile:
outfile.write("new file")
with open(testee.get_full_file_path("README.md"), "w") as outfile:
with Path(testee.get_full_file_path("README.md")).open("w") as outfile:
outfile.write("new content")

commit_hash = testee.commit(
Expand Down Expand Up @@ -318,7 +318,7 @@ def test_push(self, logging_mock):
with GitRepo(self.__mock_repo_api) as testee:
testee.clone()

with open(testee.get_full_file_path("foo.md"), "w") as readme:
with Path(testee.get_full_file_path("foo.md")).open("w") as readme:
readme.write("new file")
util_repo = Repo(testee.get_full_file_path("."))
util_repo.git.add("--all")
Expand Down Expand Up @@ -367,7 +367,7 @@ def test_push_unknown_branch(self, logging_mock):
@patch("gitopscli.git_api.git_repo.logging")
def test_push_commit_hook_error_reason_is_shown(self, logging_mock):
repo_dir = self.__origin.working_dir
with open(f"{repo_dir}/.git/hooks/pre-receive", "w") as pre_receive_hook:
with Path(f"{repo_dir}/.git/hooks/pre-receive").open("w") as pre_receive_hook:
pre_receive_hook.write('echo >&2 "we reject this push"; exit 1')
Path(
f"{repo_dir}/.git/hooks/pre-receive",
Expand All @@ -378,7 +378,7 @@ def test_push_commit_hook_error_reason_is_shown(self, logging_mock):
with GitRepo(self.__mock_repo_api) as testee:
testee.clone()

with open(testee.get_full_file_path("foo.md"), "w") as readme:
with Path(testee.get_full_file_path("foo.md")).open("w") as readme:
readme.write("new file")
util_repo = Repo(testee.get_full_file_path("."))
util_repo.git.add("--all")
Expand Down
4 changes: 2 additions & 2 deletions tests/io_api/test_yaml_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def _create_tmp_file_path(self):

def _create_file(self, content):
path = self._create_tmp_file_path()
with open(path, "w") as stream:
with Path(path).open("w") as stream:
stream.write(content)
return path

def _read_file(self, path):
with open(path) as stream:
with Path(path).open() as stream:
return stream.read()

def test_yaml_file_load(self):
Expand Down

0 comments on commit 0f1b58f

Please sign in to comment.