Skip to content

Commit 82f8551

Browse files
jorgepilotoduposyl
andauthored
fix: enable pathlib linter (#589)
Co-authored-by: Sylvain <[email protected]>
1 parent d44806b commit 82f8551

File tree

5 files changed

+27
-22
lines changed

5 files changed

+27
-22
lines changed

doc/source/conf.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@
156156
# Common content for every RST file such us links
157157
rst_epilog = ""
158158
links_filepath = pathlib.Path(__file__).parent.absolute() / "links.rst"
159-
with open(links_filepath) as links_file:
160-
rst_epilog += links_file.read()
159+
rst_epilog += links_filepath.read_text(encoding="utf-8")
161160

162161
# -- Autosectionlabel configuration ------------------------------------------
163162
autosectionlabel_maxdepth = 6
@@ -279,9 +278,11 @@ def get_sha256_from_file(filepath: pathlib.Path):
279278
280279
"""
281280
sha256_hash = hashlib.sha256()
282-
with open(filepath, "rb") as file:
283-
while chunk := file.read(8192):
284-
sha256_hash.update(chunk)
281+
282+
file_bytes = filepath.read_bytes()
283+
for i in range(0, len(file_bytes), 8192):
284+
sha256_hash.update(file_bytes[i : i + 8192])
285+
285286
return sha256_hash.hexdigest()
286287

287288

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ select = [
105105
# "F", # pyflakes, see https://beta.ruff.rs/docs/rules/#pyflakes-f
106106
# "I", # isort, see https://beta.ruff.rs/docs/rules/#isort-i
107107
"N", # pep8-naming, see https://beta.ruff.rs/docs/rules/#pep8-naming-n
108-
# "PTH", # flake8-use-pathlib, https://beta.ruff.rs/docs/rules/#flake8-use-pathlib-pth
108+
"PTH", # flake8-use-pathlib, https://beta.ruff.rs/docs/rules/#flake8-use-pathlib-pth
109109
"TD", # flake8-todos, https://docs.astral.sh/ruff/rules/#flake8-todos-td
110110
]
111111
ignore = [

src/ansys/stk/core/stkdesktop.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
__all__ = ["STKDesktop", "STKDesktopApplication"]
88

99
import os
10+
import pathlib
1011
import socket
1112
import typing
1213
import atexit
@@ -209,12 +210,12 @@ def start_application(visible:bool=False, \
209210

210211
clsid_stk12application = "{7ADA6C22-FA34-4578-8BE8-65405A55EE15}"
211212
executable = read_registry_key(f"CLSID\\{clsid_stk12application}\\LocalServer32", silent_exception=True)
212-
if executable is None or not os.path.exists(executable):
213-
bin_dir = winreg_stk_binary_dir()
214-
if bin_dir is not None:
215-
executable = os.path.join(bin_dir, "UiApplication.exe")
213+
if executable is None or not pathlib.Path(executable).exists():
214+
bin_dir = pathlib.Path(winreg_stk_binary_dir()).resolve()
215+
if bin_dir.exists():
216+
executable = bin_dir / "AgUiApplication.exe"
216217
else:
217-
raise STKInitializationError(f"Could not find UiApplication.exe. Verify STK 12 installation.")
218+
raise STKInitializationError(f"Could not find AgUiApplication.exe. Verify STK 12 installation.")
218219
cmd_line = [f"{executable}", "/pers", "STK", "/grpcServer", "On", "/grpcHost", grpc_host, "/grpcPort", str(grpc_port)]
219220
if STKDesktop._disable_pop_ups:
220221
cmd_line.append("/Automation")

src/ansys/stk/core/stkengine/tkcontrols.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
__all__ = ["GlobeControl", "MapControl", "GfxAnalysisControl"]
88

99
import os
10+
import pathlib
1011
from tkinter import Frame
1112
if os.name == "nt":
1213
from ctypes import (CDLL, POINTER, WinDLL, WinError, c_char_p, cdll, create_unicode_buffer,
@@ -66,10 +67,10 @@ def _get_jni_core_path(self):
6667
if err != 0:
6768
errormsg += f" ({WinError(err)})"
6869
raise STKRuntimeError(errormsg)
69-
stkx_dll_path = c_path.value
70+
stkx_dll_path = pathlib.Path(c_path.value).resolve()
7071

71-
jni_core_dll_path = os.path.join(os.path.dirname(stkx_dll_path), "AgJNICore.dll")
72-
return jni_core_dll_path
72+
jni_core_dll_path = stkx_dll_path.parent / "AgJNICore.dll"
73+
return str(jni_core_dll_path)
7374
def create_container(self, progid):
7475
return self.AgPythonCreateContainer(LPVOID(None), LPVOID(None), LPCWSTR(progid))
7576
def attach_container(self, container, winid, display):

src/ansys/stk/core/stkruntime.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import atexit
1010
import os
11+
import pathlib
1112
import socket
1213

1314
# The subprocess module is needed to start the backend.
@@ -139,22 +140,23 @@ def start_application(grpc_host:str="localhost", \
139140
ld_env = os.getenv('LD_LIBRARY_PATH')
140141
if ld_env:
141142
for path in ld_env.split(':'):
142-
if os.path.exists(os.path.join(path, "stkruntime")):
143-
cmd_line = [os.path.join(path, "stkruntime"), "--grpcHost", grpc_host, "--grpcPort", str(grpc_port)]
143+
stkruntime_path = (pathlib.Path(path) / "stkruntime").resolve()
144+
if stkruntime_path.exists():
145+
cmd_line = [stkruntime_path, "--grpcHost", grpc_host, "--grpcPort", str(grpc_port)]
144146
if no_graphics:
145147
cmd_line.append("--noGraphics")
146148
break
147149
else:
148150
raise STKInitializationError("LD_LIBRARY_PATH not defined. Add STK bin directory to LD_LIBRARY_PATH before running.")
149151
else:
150152
clsid_stkxapplication = "{062AB565-B121-45B5-A9A9-B412CEFAB6A9}"
151-
stkx_dll_path = read_registry_key(f"CLSID\\{clsid_stkxapplication}\\InprocServer32", silent_exception=True)
152-
bin_dir, dll_name = (None, None) if stkx_dll_path is None else os.path.split(stkx_dll_path)
153-
if bin_dir is None or not os.path.exists(os.path.join(bin_dir, "STKRuntime.exe")):
154-
bin_dir = winreg_stk_binary_dir()
155-
if bin_dir is None:
153+
stkx_dll_registry_value = read_registry_key(f"CLSID\\{clsid_stkxapplication}\\InprocServer32", silent_exception=True)
154+
stkruntime_path = None if stkx_dll_registry_value is None else pathlib.Path(stkx_dll_registry_value).parent / "STKRuntime.exe"
155+
if stkruntime_path is None or not stkruntime_path.exists():
156+
stkruntime_path = pathlib.Path(winreg_stk_binary_dir()) / "STKRuntime.exe"
157+
if not stkruntime_path.exists():
156158
raise STKInitializationError(f"Could not find STKRuntime.exe. Verify STK installation.")
157-
cmd_line = [os.path.join(bin_dir, "STKRuntime.exe"), "/grpcHost", grpc_host, "/grpcPort", str(grpc_port)]
159+
cmd_line = [str(stkruntime_path.resolve()), "/grpcHost", grpc_host, "/grpcPort", str(grpc_port)]
158160
if no_graphics:
159161
cmd_line.append("/noGraphics")
160162

0 commit comments

Comments
 (0)