Skip to content

Commit fb0165c

Browse files
echoixninsbl
andauthored
CQ: Fix Ruff PTH110: os.path.exists() should be replaced by Path.exists() (#6524)
* CQ: Fix Ruff PTH110: `os.path.exists()` should be replaced by `Path.exists()` Ruff rule: https://docs.astral.sh/ruff/rules/os-path-exists/ * Update grass.py, grass_script_create_project_pack_test.py, d.polar.py, r_pack_test.py, create_python_init_file.py, globalvar.py, lib/init/grass.py Co-authored-by: Stefan Blumentrath <[email protected]> * Update gui/wxpython/wxplot/profile.py * Update profile.py * Apply suggestions from code review for os.path.join * r.pack: Revert changes for a test that returns a permission error instead of checking if exists or not * Add typing to python/grass/tools/tests/conftest.py for tmp_path_factory * Remove wrapping pathlib.Path objects when they are already given in argument as a pathlib.Path in python/grass/tools/tests/grass_tools_session_tools_pack_test.py --------- Co-authored-by: Stefan Blumentrath <[email protected]>
1 parent da1fd60 commit fb0165c

File tree

102 files changed

+383
-340
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+383
-340
lines changed

display/d.mon/render_cmd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def render(cmd, mapfile):
7373
grass.run_command(cmd[0], env=env, **cmd[1])
7474
# display driver can generate a blank map file unnecessarily for
7575
# non-rendering modules; delete it
76-
if cmd[0] in non_rendering_modules and os.path.exists(mapfile):
76+
if cmd[0] in non_rendering_modules and Path(mapfile).exists():
7777
remove_mapfile(mapfile)
7878

7979
except CalledModuleError as e:
@@ -198,7 +198,7 @@ def read_stdin(cmd):
198198
render(cmd, mapfile)
199199

200200
update_cmd_file(os.path.join(path, "cmd"), cmd, mapfile)
201-
if cmd[0] == "d.erase" and os.path.exists(legfile):
201+
if cmd[0] == "d.erase" and Path(legfile).exists():
202202
os.remove(legfile)
203203

204204
sys.exit(0)

gui/wxpython/animation/data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
@author Anna Petrasova <kratochanna gmail.com>
1717
"""
1818

19-
import os
2019
import copy
20+
from pathlib import Path
2121

2222
from grass.script.utils import parse_key_val
2323
from grass.script import core as gcore
@@ -134,7 +134,7 @@ def SetWorkspaceFile(self, fileName):
134134
if fileName == "":
135135
raise ValueError(_("No workspace file selected."))
136136

137-
if not os.path.exists(fileName):
137+
if not Path(fileName).exists():
138138
raise OSError(_("File %s not found") % fileName)
139139
self._workspaceFile = fileName
140140

gui/wxpython/animation/dialogs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ def OnRemove(self, event):
14431443
def OnExport(self, event):
14441444
for decor in self.decorations:
14451445
if decor["name"] == "image":
1446-
if not os.path.exists(decor["file"]):
1446+
if not Path(decor["file"]).exists():
14471447
if decor["file"]:
14481448
GError(
14491449
parent=self, message=_("File %s not found.") % decor["file"]
@@ -1456,7 +1456,7 @@ def OnExport(self, event):
14561456

14571457
if self.formatChoice.GetSelection() == 0:
14581458
name = self.dirBrowse.GetValue()
1459-
if not os.path.exists(name):
1459+
if not Path(name).exists():
14601460
if name:
14611461
GError(parent=self, message=_("Directory %s not found.") % name)
14621462
else:
@@ -1553,7 +1553,7 @@ def _export_file_validation(self, filebrowsebtn, file_path, file_postfix):
15531553
file_path += file_postfix
15541554

15551555
base_dir = os.path.dirname(file_path)
1556-
if not os.path.exists(base_dir):
1556+
if not Path(base_dir).exists():
15571557
GError(
15581558
parent=self,
15591559
message=file_path_does_not_exist_err_message.format(
@@ -1562,7 +1562,7 @@ def _export_file_validation(self, filebrowsebtn, file_path, file_postfix):
15621562
)
15631563
return False
15641564

1565-
if os.path.exists(file_path):
1565+
if Path(file_path).exists():
15661566
overwrite_dlg = wx.MessageDialog(
15671567
self.GetParent(),
15681568
message=_(

gui/wxpython/animation/provider.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import wx
2626
import tempfile
2727
from multiprocessing import Process, Queue
28+
from pathlib import Path
2829

2930
from core.gcmd import GException, DecodeString
3031
from core.settings import UserSettings
@@ -175,7 +176,7 @@ def _dryRender(self, uniqueCmds, regions, force):
175176
filename = GetFileFromCmd(self._tempDir, cmd, region)
176177
if (
177178
not force
178-
and os.path.exists(filename)
179+
and Path(filename).exists()
179180
and self._mapFilesPool.GetSize(HashCmd(cmd, region))
180181
== (self.imageWidth, self.imageHeight)
181182
):
@@ -378,7 +379,7 @@ def Render(self, cmdList, regions, regionFor3D, bgcolor, force, nprocs):
378379
filename = GetFileFromCmd(self._tempDir, cmd, region)
379380
if (
380381
not force
381-
and os.path.exists(filename)
382+
and Path(filename).exists()
382383
and self._mapFilesPool.GetSize(HashCmd(cmd, region))
383384
== (self.imageWidth, self.imageHeight)
384385
):
@@ -791,7 +792,7 @@ def __init__(self, tempDir):
791792
def __call__(self):
792793
import shutil
793794

794-
if os.path.exists(self._tempDir):
795+
if Path(self._tempDir).exists():
795796
try:
796797
shutil.rmtree(self._tempDir)
797798
Debug.msg(5, "CleanUp: removed directory {t}".format(t=self._tempDir))
@@ -901,7 +902,7 @@ def test():
901902
mapFilesPool = MapFilesPool()
902903

903904
tempDir = "/tmp/test"
904-
if os.path.exists(tempDir):
905+
if Path(tempDir).exists():
905906
shutil.rmtree(tempDir)
906907
os.mkdir(tempDir)
907908
# comment this line to keep the directory after program ends

gui/wxpython/core/gconsole.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import codecs
3333
import locale
34+
from pathlib import Path
3435

3536
import wx
3637
from wx.lib.newevent import NewEvent
@@ -607,11 +608,11 @@ def load_source(modname, filename):
607608
if sys.platform == "win32":
608609
pyFile += ".py"
609610
pyPath = os.path.join(os.environ["GISBASE"], "scripts", pyFile)
610-
if not os.path.exists(pyPath):
611+
if not Path(pyPath).exists():
611612
pyPath = os.path.join(
612613
os.environ["GRASS_ADDON_BASE"], "scripts", pyFile
613614
)
614-
if not os.path.exists(pyPath):
615+
if not Path(pyPath).exists():
615616
GError(
616617
parent=self._guiparent,
617618
message=_("Module <%s> not found.") % command[0],

gui/wxpython/core/globalvar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def UpdateGRASSAddOnCommands(eList=None):
200200
nCmd = 0
201201
pathList = os.getenv("PATH", "").split(os.pathsep)
202202
for path in addonPath.split(os.pathsep):
203-
if not os.path.exists(path) or not os.path.isdir(path):
203+
if not Path(path).exists() or not Path(path).is_dir():
204204
continue
205205

206206
# check if addon is in the path

gui/wxpython/core/render.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def IsHidden(self):
276276

277277
def IsRendered(self) -> bool:
278278
"""!Check if layer was rendered (if the image file exists)"""
279-
return bool(os.path.exists(self.mapfile))
279+
return bool(Path(self.mapfile).exists())
280280

281281
def SetType(self, ltype):
282282
"""Set layer type"""
@@ -531,7 +531,7 @@ def _init(self):
531531
self.layers = []
532532

533533
# re-render from scratch
534-
if os.path.exists(self.Map.mapfile):
534+
if Path(self.Map.mapfile).exists():
535535
os.remove(self.Map.mapfile)
536536

537537
def _checkRenderedSizes(self, env, layers):

gui/wxpython/core/settings.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -891,9 +891,9 @@ def ReadSettingsFile(self, settings=None):
891891
if settings is None:
892892
settings = self.userSettings
893893

894-
if os.path.exists(self.filePath):
894+
if Path(self.filePath).exists():
895895
self._readFile(settings)
896-
elif os.path.exists(self.legacyFilePath):
896+
elif Path(self.legacyFilePath).exists():
897897
self._readLegacyFile(settings)
898898

899899
# set environment variables
@@ -982,7 +982,7 @@ def SaveToFile(self, settings=None):
982982
settings = self.userSettings
983983

984984
dirPath = GetSettingsPath()
985-
if not os.path.exists(dirPath):
985+
if not Path(dirPath).exists():
986986
try:
987987
os.mkdir(dirPath)
988988
except OSError:

gui/wxpython/core/toolboxes.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ def GetSettingsPath():
5757

5858
def _getUserToolboxesFile():
5959
userToolboxesFile = os.path.join(GetSettingsPath(), "toolboxes", "toolboxes.xml")
60-
if not os.path.exists(userToolboxesFile):
60+
if not Path(userToolboxesFile).exists():
6161
userToolboxesFile = None
6262
return userToolboxesFile
6363

6464

6565
def _getUserMainMenuFile():
6666
userMainMenuFile = os.path.join(GetSettingsPath(), "toolboxes", "main_menu.xml")
67-
if not os.path.exists(userMainMenuFile):
67+
if not Path(userMainMenuFile).exists():
6868
userMainMenuFile = None
6969
return userMainMenuFile
7070

@@ -96,7 +96,7 @@ def toolboxesOutdated():
9696
"""Removes auto-generated menudata.xml
9797
to let gui regenerate it next time it starts."""
9898
path = os.path.join(GetSettingsPath(), "toolboxes", "menudata.xml")
99-
if os.path.exists(path):
99+
if Path(path).exists():
100100
try_remove(path)
101101

102102

@@ -118,7 +118,7 @@ def getMenudataFile(userRootFile, newFile, fallback):
118118

119119
distributionRootFile = os.path.join(WXGUIDIR, "xml", userRootFile)
120120
userRootFile = os.path.join(GetSettingsPath(), "toolboxes", userRootFile)
121-
if not os.path.exists(userRootFile):
121+
if not Path(userRootFile).exists():
122122
userRootFile = None
123123

124124
# always create toolboxes directory if does not exist yet
@@ -133,7 +133,7 @@ def getMenudataFile(userRootFile, newFile, fallback):
133133
# when any of main_menu.xml or toolboxes.xml are changed,
134134
# generate new menudata.xml
135135

136-
if os.path.exists(menudataFile):
136+
if Path(menudataFile).exists():
137137
# remove menu file when there is no main_menu and toolboxes
138138
if not _getUserToolboxesFile() and (not userRootFile):
139139
os.remove(menudataFile)
@@ -217,7 +217,7 @@ def _setupToolboxes():
217217
"""Create 'toolboxes' directory if doesn't exist."""
218218
basePath = GetSettingsPath()
219219
path = os.path.join(basePath, "toolboxes")
220-
if not os.path.exists(basePath):
220+
if not Path(basePath).exists():
221221
return None
222222

223223
if _createPath(path):
@@ -227,7 +227,7 @@ def _setupToolboxes():
227227

228228
def _createPath(path):
229229
"""Creates path (for toolboxes) if it doesn't exist'"""
230-
if not os.path.exists(path):
230+
if not Path(path).exists():
231231
try:
232232
os.mkdir(path)
233233
except OSError as e:

gui/wxpython/core/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import re
2323
import inspect
2424
import operator
25+
from pathlib import Path
2526
from string import digits
2627
from typing import TYPE_CHECKING
2728

@@ -826,7 +827,7 @@ def StoreEnvVariable(key, value=None, envFile=None):
826827
# read env file
827828
environ = {}
828829
lineSkipped = []
829-
if os.path.exists(envFile):
830+
if Path(envFile).exists():
830831
try:
831832
with open(envFile) as fd:
832833
for line in fd:

0 commit comments

Comments
 (0)