Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: Fix os-stat (PTH116) by replacing os.stat() calls with pathlib #4412

Merged
merged 3 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion gui/wxpython/core/watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import os
import time

from pathlib import Path

import wx
from wx.lib.newevent import NewEvent

Expand Down Expand Up @@ -59,7 +62,7 @@ def on_modified(self, event):
not event.is_directory
and os.path.basename(event.src_path) == self.rcfile_name
):
timestamp = os.stat(event.src_path).st_mtime
timestamp = Path(event.src_path).stat().st_mtime
if timestamp - self.modified_time < 0.5:
return
self.modified_time = timestamp
Expand Down
5 changes: 3 additions & 2 deletions gui/wxpython/gui_core/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import os
import sys

from pathlib import Path

try:
import pwd

Expand Down Expand Up @@ -2425,8 +2427,7 @@ def LoadData(self):

for mapset in self.parent.all_mapsets_ordered:
index = self.InsertItem(self.GetItemCount(), mapset)
mapsetPath = os.path.join(locationPath, mapset)
stat_info = os.stat(mapsetPath)
stat_info = Path(locationPath, mapset).stat()
if havePwd:
try:
self.SetItem(index, 1, "%s" % pwd.getpwuid(stat_info.st_uid)[0])
Expand Down
3 changes: 2 additions & 1 deletion imagery/i.gensig/testsuite/test_i_gensig.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import stat
import ctypes
import shutil
from pathlib import Path

from grass.pygrass import utils
from grass.pygrass.gis import Mapset
Expand Down Expand Up @@ -107,7 +108,7 @@ def test_creation(self):
)

# File must be present
sig_stat = os.stat(f"{self.sig_dir1}/sig")
sig_stat = Path(self.sig_dir1, "sig").stat()
self.assertTrue(stat.S_ISREG(sig_stat.st_mode))

# Compare values within sig file
Expand Down
10 changes: 5 additions & 5 deletions lib/imagery/testsuite/test_imagery_sigfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
for details
"""

import os
import stat
import ctypes
import shutil
from pathlib import Path

from grass.gunittest.case import TestCase
from grass.gunittest.main import test
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_roundtrip_signature_v1_norgb_one_label(self):

# Write signatures to file
p_new_sigfile = I_fopen_signature_file_new(self.sig_name)
sig_stat = os.stat(f"{self.sig_dir}/sig")
sig_stat = Path(self.sig_dir, "sig").stat()
self.assertTrue(stat.S_ISREG(sig_stat.st_mode))
I_write_signatures(p_new_sigfile, ctypes.byref(So))
self.libc.fclose(p_new_sigfile)
Expand Down Expand Up @@ -136,7 +136,7 @@ def test_broken_signature_v1_norgb(self):

# Write signatures to file
p_new_sigfile = I_fopen_signature_file_new(self.sig_name)
sig_stat = os.stat(f"{self.sig_dir}/sig")
sig_stat = Path(self.sig_dir, "sig").stat()
self.assertTrue(stat.S_ISREG(sig_stat.st_mode))
I_write_signatures(p_new_sigfile, ctypes.byref(So))
self.libc.fclose(p_new_sigfile)
Expand Down Expand Up @@ -188,7 +188,7 @@ def test_roundtrip_signature_v1_norgb_two_labelss(self):

# Write signatures to file
p_new_sigfile = I_fopen_signature_file_new(self.sig_name)
sig_stat = os.stat(f"{self.sig_dir}/sig")
sig_stat = Path(self.sig_dir, "sig").stat()
self.assertTrue(stat.S_ISREG(sig_stat.st_mode))
I_write_signatures(p_new_sigfile, ctypes.byref(So))
self.libc.fclose(p_new_sigfile)
Expand Down Expand Up @@ -277,7 +277,7 @@ def test_roundtrip_signature_v2_norgb_two_labels_oclass(self):

# Write signatures to file
p_new_sigfile = I_fopen_signature_file_new(self.sig_name)
sig_stat = os.stat(f"{self.sig_dir}/sig")
sig_stat = Path(self.sig_dir, "sig").stat()
self.assertTrue(stat.S_ISREG(sig_stat.st_mode))
I_write_signatures(p_new_sigfile, ctypes.byref(So))
self.libc.fclose(p_new_sigfile)
Expand Down
8 changes: 4 additions & 4 deletions lib/imagery/testsuite/test_imagery_sigsetfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
for details
"""

import os
import stat
import ctypes
import shutil
from pathlib import Path

from grass.gunittest.case import TestCase
from grass.gunittest.main import test
Expand Down Expand Up @@ -84,7 +84,7 @@ def test_roundtrip_sigset_v1_one_label(self):

# Write signatures to file
p_new_sigfile = I_fopen_sigset_file_new(self.sig_name)
sig_stat = os.stat(f"{self.sig_dir}/sig")
sig_stat = Path(self.sig_dir, "sig").stat()
self.assertTrue(stat.S_ISREG(sig_stat.st_mode))
I_WriteSigSet(p_new_sigfile, ctypes.byref(So))
self.libc.fclose(p_new_sigfile)
Expand Down Expand Up @@ -139,7 +139,7 @@ def test_read_fail_sigset_v1_one_label(self):

# Write signatures to file
p_new_sigfile = I_fopen_sigset_file_new(self.sig_name)
sig_stat = os.stat(f"{self.sig_dir}/sig")
sig_stat = Path(self.sig_dir, "sig").stat()
self.assertTrue(stat.S_ISREG(sig_stat.st_mode))
I_WriteSigSet(p_new_sigfile, ctypes.byref(So))
self.libc.fclose(p_new_sigfile)
Expand Down Expand Up @@ -183,7 +183,7 @@ def test_roundtrip_sigset_v1_two_labels(self):

# Write signatures to file
p_new_sigfile = I_fopen_sigset_file_new(self.sig_name)
sig_stat = os.stat(f"{self.sig_dir}/sig")
sig_stat = Path(self.sig_dir, "sig").stat()
self.assertTrue(stat.S_ISREG(sig_stat.st_mode))
I_WriteSigSet(p_new_sigfile, ctypes.byref(So))
self.libc.fclose(p_new_sigfile)
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ ignore = [
"PTH111", # os-path-expanduser
"PTH112", # os-path-isdir
"PTH113", # os-path-isfile
"PTH116", # os-stat
"PTH117", # os-path-isabs
"PTH118", # os-path-join
"PTH119", # os-path-basename
Expand Down
2 changes: 1 addition & 1 deletion python/grass/app/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def lock_mapset(mapset_path, force_lock_removal, message_callback):
raise MapsetLockingException(_("Path '{}' doesn't exist").format(mapset_path))
if not os.access(mapset_path, os.W_OK):
error = _("Path '{}' not accessible.").format(mapset_path)
stat_info = os.stat(mapset_path)
stat_info = Path(mapset_path).stat()
mapset_uid = stat_info.st_uid
if mapset_uid != os.getuid():
error = "{error}\n{detail}".format(
Expand Down
2 changes: 1 addition & 1 deletion python/grass/grassdb/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def is_current_user_mapset_owner(mapset_path):
# Mapset needs to be owned by user.
if sys.platform == "win32":
return True
stat_info = os.stat(mapset_path)
stat_info = Path(mapset_path).stat()
mapset_uid = stat_info.st_uid
return mapset_uid == os.getuid()

Expand Down
Loading