diff --git a/gui/wxpython/datacatalog/tree.py b/gui/wxpython/datacatalog/tree.py index 2a3e8ed4fba..5f19af81cd7 100644 --- a/gui/wxpython/datacatalog/tree.py +++ b/gui/wxpython/datacatalog/tree.py @@ -23,6 +23,7 @@ import re import copy from multiprocessing import Process, Queue, cpu_count +from pathlib import Path import wx @@ -354,7 +355,7 @@ def _getValidSavedGrassDBs(self): dbs = UserSettings.Get( group="datacatalog", key="grassdbs", subkey="listAsString" ) - return [db for db in dbs.split(",") if os.path.isdir(db)] + return [db for db in dbs.split(",") if Path(db).is_dir()] def _saveGrassDBs(self): """Save current grass dbs in tree to settings""" diff --git a/gui/wxpython/image2target/ii2t_gis_set.py b/gui/wxpython/image2target/ii2t_gis_set.py index be7f6a46345..d31d9eaa48e 100644 --- a/gui/wxpython/image2target/ii2t_gis_set.py +++ b/gui/wxpython/image2target/ii2t_gis_set.py @@ -313,7 +313,7 @@ def _set_properties(self, version, revision): # set database if not self.gisdbase: # sets an initial path for gisdbase if nothing in GISRC - if os.path.isdir(os.getenv("HOME")): + if Path(os.getenv("HOME")).is_dir(): self.gisdbase = os.getenv("HOME") else: self.gisdbase = str(Path.cwd()) @@ -331,7 +331,7 @@ def _set_properties(self, version, revision): location = self.GetRCValue("LOCATION_NAME") if location == "": return - if not os.path.isdir(os.path.join(self.gisdbase, location)): + if not Path(os.path.join(self.gisdbase, location)).is_dir(): location = None # list of locations diff --git a/gui/wxpython/image2target/ii2t_manager.py b/gui/wxpython/image2target/ii2t_manager.py index b2dd81c9ef3..0999cd26bf5 100644 --- a/gui/wxpython/image2target/ii2t_manager.py +++ b/gui/wxpython/image2target/ii2t_manager.py @@ -489,7 +489,7 @@ def OnLocation(self, event): self.mapsetList = [] for item in tmplist: if ( - os.path.isdir(os.path.join(self.grassdatabase, self.xylocation, item)) + Path(self.grassdatabase, self.xylocation, item).is_dir() and Path(self.grassdatabase, self.xylocation, item, "WIND").exists() ): if item != "PERMANENT": diff --git a/gui/wxpython/lmgr/workspace.py b/gui/wxpython/lmgr/workspace.py index 7f4d2bcc7c4..2d46d0d0943 100644 --- a/gui/wxpython/lmgr/workspace.py +++ b/gui/wxpython/lmgr/workspace.py @@ -208,7 +208,7 @@ def Load(self, filename): self.lmgr.SetSize(gxwXml.layerManager["size"]) if gxwXml.layerManager["cwd"]: self.lmgr.cwdPath = gxwXml.layerManager["cwd"] - if os.path.isdir(self.lmgr.cwdPath): + if Path(self.lmgr.cwdPath).is_dir(): os.chdir(self.lmgr.cwdPath) # diff --git a/lib/init/grass.py b/lib/init/grass.py index 0c67498c447..564ef8fb939 100755 --- a/lib/init/grass.py +++ b/lib/init/grass.py @@ -381,12 +381,12 @@ def create_grass_config_dir() -> str: except (RuntimeError, NotADirectoryError) as e: fatal(f"{e}") - if not os.path.isdir(directory): + if not Path(directory).is_dir(): try: Path(directory).mkdir(parents=True) except OSError as e: # Can happen as a race condition - if e.errno != errno.EEXIST or not os.path.isdir(directory): + if e.errno != errno.EEXIST or not Path(directory).is_dir(): fatal( _( "Failed to create configuration directory '{}' with error: {}" @@ -700,7 +700,7 @@ def cannot_create_location_reason(gisdbase: StrPath, location: str) -> str: return _( "Unable to create new project <{location}> because <{path}> is a file." ).format(**locals()) - if os.path.isdir(path): + if Path(path).is_dir(): return _( "Unable to create new project <{location}> because" " the directory <{path}>" @@ -851,7 +851,7 @@ def set_mapset( " because <{path}> is a file." ).format(mapset=mapset, path=path) ) - elif os.path.isdir(path): + elif Path(path).is_dir(): # not a valid mapset, but dir exists, assuming # broken/incomplete mapset warning( @@ -2181,7 +2181,7 @@ def main() -> None: GRASS_VERSION_MAJOR = runtime_paths.version_major GRASS_VERSION_GIT = runtime_paths.grass_version_git gisbase = runtime_paths.gisbase - if not os.path.isdir(gisbase): + if not Path(gisbase).is_dir(): gisbase = get_install_path(gisbase) # Set the main prefix again. # See also grass.script.setup.setup_runtime_env. diff --git a/pyproject.toml b/pyproject.toml index 609c86d9002..0dbc7549763 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -214,7 +214,6 @@ ignore = [ "PTH107", # os-remove "PTH108", # os-unlink "PTH111", # os-path-expanduser - "PTH112", # os-path-isdir "PTH117", # os-path-isabs "PTH118", # os-path-join "PTH119", # os-path-basename diff --git a/python/grass/app/runtime.py b/python/grass/app/runtime.py index 2f72088a1e9..24ce4c060d0 100644 --- a/python/grass/app/runtime.py +++ b/python/grass/app/runtime.py @@ -157,7 +157,7 @@ def get_grass_config_dir_for_version(major_version, minor_version, *, env): ) raise RuntimeError(msg) - if not os.path.isdir(config_dir): + if not Path(config_dir).is_dir(): msg = ( f"The {env_dirname} variable points to directory which does" " not exist, ask your operating system support" diff --git a/python/grass/app/tests/grass_app_resource_paths_test.py b/python/grass/app/tests/grass_app_resource_paths_test.py index 6cd5dde7c73..c6d9299c83b 100644 --- a/python/grass/app/tests/grass_app_resource_paths_test.py +++ b/python/grass/app/tests/grass_app_resource_paths_test.py @@ -1,4 +1,3 @@ -import os from pathlib import Path import pytest @@ -39,4 +38,4 @@ def test_value_not_empty(name): def test_value_is_directory(name): value = getattr(resource_paths, name) assert Path(value).exists() - assert os.path.isdir(value) + assert Path(value).is_dir() diff --git a/python/grass/pygrass/gis/__init__.py b/python/grass/pygrass/gis/__init__.py index e9f7469a4fe..efc09af359f 100644 --- a/python/grass/pygrass/gis/__init__.py +++ b/python/grass/pygrass/gis/__init__.py @@ -1,10 +1,11 @@ #!/usr/bin/env python3 from os import listdir -from os.path import join, isdir +from os.path import join import shutil import ctypes as ct import fnmatch +from pathlib import Path import grass.lib.gis as libgis @@ -243,7 +244,7 @@ def __iter__(self): return ( m for m in listdir(lpath) - if (isdir(join(lpath, m)) and is_valid(m, lpath, "MAPSET")) + if (Path(lpath, m).is_dir() and is_valid(m, lpath, "MAPSET")) ) def __len__(self): diff --git a/python/grass/pygrass/modules/grid/grid.py b/python/grass/pygrass/modules/grid/grid.py index d80790cfca8..a177fd76c83 100644 --- a/python/grass/pygrass/modules/grid/grid.py +++ b/python/grass/pygrass/modules/grid/grid.py @@ -139,7 +139,7 @@ def get_mapset(gisrc_src, gisrc_dst): mdst, ldst, gdst = read_gisrc(gisrc_dst) path_src = os.path.join(gsrc, lsrc, msrc) path_dst = os.path.join(gdst, ldst, mdst) - if not os.path.isdir(path_dst): + if not Path(path_dst).is_dir(): Path(path_dst).mkdir(parents=True) copy_special_mapset_files(path_src, path_dst) src = Mapset(msrc, lsrc, gsrc) @@ -273,7 +273,7 @@ def copy_rasters(rasters, gisrc_src, gisrc_dst, processes, region=None): # change gisdbase to dst env["GISRC"] = gisrc_dst rupck(input=file_dst, output=rast_clean, overwrite=True, env_=env) - os.remove(file_dst) + Path(file_dst).unlink() def copy_vectors(vectors, gisrc_src, gisrc_dst): @@ -307,7 +307,7 @@ def copy_vectors(vectors, gisrc_src, gisrc_dst): # change gisdbase to dst env["GISRC"] = gisrc_dst vupck(input=file_dst, output=vect, overwrite=True, env_=env) - os.remove(file_dst) + Path(file_dst).unlink() def get_cmd(cmdd): @@ -395,7 +395,7 @@ def cmd_exe(args): # run the grass command sub.Popen(get_cmd(cmd), shell=shell, env=env).wait() # remove temp GISRC - os.remove(gisrc_dst) + Path(gisrc_dst).unlink() class GridModule: @@ -539,7 +539,7 @@ def __init__( def __del__(self): if self.gisrc_dst: # remove GISRC file - os.remove(self.gisrc_dst) + Path(self.gisrc_dst).unlink() def clean_location(self, location=None): """Remove all created mapsets. @@ -730,7 +730,7 @@ def _clean(self): gisdbase, location = os.path.split(self.move) self.clean_location(Location(location, gisdbase)) # rm temporary gis_rc - os.remove(self.gisrc_dst) + Path(self.gisrc_dst).unlink() self.gisrc_dst = None sht.rmtree(os.path.join(self.move, "PERMANENT")) sht.rmtree(os.path.join(self.move, self.mset.name)) diff --git a/python/grass/pygrass/utils.py b/python/grass/pygrass/utils.py index 9a040a961dd..344e4237ea4 100644 --- a/python/grass/pygrass/utils.py +++ b/python/grass/pygrass/utils.py @@ -1,6 +1,7 @@ import fnmatch import itertools import os +from pathlib import Path from sqlite3 import OperationalError import grass.lib.gis as libgis @@ -39,7 +40,7 @@ def findfiles(dirpath, match=None): res = [] for f in sorted(os.listdir(dirpath)): abspath = os.path.join(dirpath, f) - if os.path.isdir(abspath): + if Path(abspath).is_dir(): res.extend(findfiles(abspath, match)) if match: diff --git a/python/grass/script/setup.py b/python/grass/script/setup.py index c1faaf82b6b..d202cd15582 100644 --- a/python/grass/script/setup.py +++ b/python/grass/script/setup.py @@ -148,7 +148,7 @@ def ask_executable(arg): ).stdout.strip() # Directory was provided as a parameter. - if path and os.path.isdir(path): + if path and Path(path).is_dir(): return os.fspath(path) # Executable was provided as parameter. @@ -156,12 +156,12 @@ def ask_executable(arg): # The path was provided by the user and it is an executable # (on path or provided with full path), so raise exception on failure. path_from_executable = ask_executable(path) - if os.path.isdir(path_from_executable): + if Path(path_from_executable).is_dir(): return path_from_executable # GISBASE is set from the outside or already set. env_gisbase = os.environ.get("GISBASE") - if env_gisbase and os.path.isdir(env_gisbase): + if env_gisbase and Path(env_gisbase).is_dir(): return env_gisbase # Executable provided in environment (name is from grass-session). @@ -170,7 +170,7 @@ def ask_executable(arg): grass_bin = os.environ.get("GRASSBIN") if grass_bin and shutil.which(grass_bin): path_from_executable = ask_executable(grass_bin) - if os.path.isdir(path_from_executable): + if Path(path_from_executable).is_dir(): return path_from_executable # Derive the path from path to this file (Python module). @@ -191,7 +191,7 @@ def ask_executable(arg): grass_bin = "grass" if grass_bin and shutil.which(grass_bin): path_from_executable = ask_executable(grass_bin) - if os.path.isdir(path_from_executable): + if Path(path_from_executable).is_dir(): return path_from_executable # We fallback to whatever was provided. This may help trace the issue @@ -227,7 +227,7 @@ def setup_runtime_env(gisbase=None, *, env=None): runtime_paths = RuntimePaths(env=env, prefix=gisbase) gisbase = runtime_paths.gisbase - if not os.path.isdir(gisbase): + if not Path(gisbase).is_dir(): gisbase = get_install_path(gisbase) # Set the main prefix again. # See also the main grass executable code. diff --git a/python/grass/script/utils.py b/python/grass/script/utils.py index 232b1286de2..5180356c034 100644 --- a/python/grass/script/utils.py +++ b/python/grass/script/utils.py @@ -433,21 +433,22 @@ def alphanum_key(actual_key): def get_lib_path(modname, libname=None): """Return the path of the libname contained in the module.""" from os import getenv - from os.path import isdir, join, sep + from os.path import join, sep - if isdir(join(getenv("GISBASE"), "etc", modname)): + if Path(getenv("GISBASE"), "etc", modname).is_dir(): path = join(os.getenv("GISBASE"), "etc", modname) elif ( getenv("GRASS_ADDON_BASE") and libname - and isdir(join(getenv("GRASS_ADDON_BASE"), "etc", modname, libname)) + and Path(getenv("GRASS_ADDON_BASE"), "etc", modname, libname).is_dir() ) or ( getenv("GRASS_ADDON_BASE") - and isdir(join(getenv("GRASS_ADDON_BASE"), "etc", modname)) + and Path(getenv("GRASS_ADDON_BASE"), "etc", modname).is_dir() ): path = join(getenv("GRASS_ADDON_BASE"), "etc", modname) - elif getenv("GRASS_ADDON_BASE") and isdir( - join(getenv("GRASS_ADDON_BASE"), modname, modname) + elif ( + getenv("GRASS_ADDON_BASE") + and Path(getenv("GRASS_ADDON_BASE"), modname, modname).is_dir() ): path = join(os.getenv("GRASS_ADDON_BASE"), modname, modname) else: diff --git a/python/grass/utils/download.py b/python/grass/utils/download.py index 2e7c1546b0e..670d5403b8e 100644 --- a/python/grass/utils/download.py +++ b/python/grass/utils/download.py @@ -127,7 +127,7 @@ def _move_extracted_files(extract_dir, target_dir, files): debug("_move_extracted_files({})".format(locals())) if len(files) == 1: actual_path = os.path.join(extract_dir, files[0]) - if os.path.isdir(actual_path): + if Path(actual_path).is_dir(): shutil.copytree(actual_path, target_dir) else: shutil.copy(actual_path, target_dir) @@ -135,7 +135,7 @@ def _move_extracted_files(extract_dir, target_dir, files): Path(target_dir).mkdir(exist_ok=True) for file_name in files: actual_file = os.path.join(extract_dir, file_name) - if os.path.isdir(actual_file): + if Path(actual_file).is_dir(): # Choice of copy tree function: # shutil.copytree() fails when subdirectory exists. # However, distutils.copy_tree() may fail to create directories before diff --git a/scripts/g.download.project/g.download.project.py b/scripts/g.download.project/g.download.project.py index 1f88103301d..27e2b09a660 100644 --- a/scripts/g.download.project/g.download.project.py +++ b/scripts/g.download.project/g.download.project.py @@ -70,7 +70,7 @@ def find_location_in_directory(path, recurse=0): """ assert recurse >= 0 full_paths = [os.path.join(path, i) for i in os.listdir(path)] - candidates = sorted([i for i in full_paths if os.path.isdir(i)]) + candidates = sorted([i for i in full_paths if Path(i).is_dir()]) for candidate in candidates: if is_location_valid(candidate): return candidate diff --git a/scripts/g.extension/g.extension.py b/scripts/g.extension/g.extension.py index 3d49d5cd2a1..3c5e75c6001 100644 --- a/scripts/g.extension/g.extension.py +++ b/scripts/g.extension/g.extension.py @@ -1751,7 +1751,7 @@ def move_extracted_files(extract_dir, target_dir, files): Path(target_dir).mkdir(exist_ok=True) for file_name in files: actual_file = os.path.join(extract_dir, file_name) - if os.path.isdir(actual_file): + if Path(actual_file).is_dir(): # shutil.copytree() replaced by copy_tree() because # shutil's copytree() fails when subdirectory exists copy_tree(actual_file, os.path.join(target_dir, file_name)) @@ -1947,7 +1947,7 @@ def download_source_code( " Please report this to the grass-user mailing list." ).format(source) ) - assert os.path.isdir(directory) + assert Path(directory).is_dir() return directory, url @@ -2286,7 +2286,7 @@ def remove_extension_std(name, force=False): # remove module libraries under GRASS_ADDONS/etc/{name}/* libpath = os.path.join(options["prefix"], "etc", name) - if os.path.isdir(libpath): + if Path(libpath).is_dir(): gs.verbose(libpath) if force: shutil.rmtree(libpath) @@ -2372,7 +2372,7 @@ def create_dir(path): NOOP for existing directory. """ - if os.path.isdir(path): + if Path(path).is_dir(): return try: @@ -2722,7 +2722,7 @@ def resolve_source_code(url=None, name=None, branch=None, fork=False): return "official_fork", url # Handle local URLs - if os.path.isdir(url): + if Path(url).is_dir(): return "dir", os.path.abspath(url) if Path(url).exists(): if url.endswith(".zip"): diff --git a/scripts/v.db.reconnect.all/v.db.reconnect.all.py b/scripts/v.db.reconnect.all/v.db.reconnect.all.py index eab02276260..55d4d75b1fc 100755 --- a/scripts/v.db.reconnect.all/v.db.reconnect.all.py +++ b/scripts/v.db.reconnect.all/v.db.reconnect.all.py @@ -62,7 +62,7 @@ # substitute variables (gisdbase, location_name, mapset) -def substitute_db(database): +def substitute_db(database) -> str: gisenv = gs.gisenv() tmpl = string.Template(database) @@ -76,22 +76,22 @@ def substitute_db(database): # create database if doesn't exist -def create_db(driver, database): - subst_database = substitute_db(database) +def create_db(driver, database) -> bool: + subst_database: str = substitute_db(database) if driver == "dbf": - path = subst_database + path = Path(subst_database) # check if destination directory exists - if not os.path.isdir(path): + if not path.is_dir(): # create dbf database - Path(path).mkdir(parents=True) + path.mkdir(parents=True) return True return False if driver == "sqlite": - path = os.path.dirname(subst_database) + path = Path(subst_database).parent # check if destination directory exists - if not os.path.isdir(path): - Path(path).mkdir(parents=True) + if not path.is_dir(): + path.mkdir(parents=True) if ( subst_database