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 PTH208 use of pathlib #5238

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions gui/wxpython/icons/grass_icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
__author__ = "Robert Szczepanek"

import os

from pathlib import Path
from core import globalvar

iconPath = os.path.join(globalvar.ICONDIR, "grass")

iconSet = {}

for icon in os.listdir(iconPath):
for icon in Path(iconPath).iterdir():
name, ext = os.path.splitext(icon)
if ext != ".png":
continue
Expand Down
30 changes: 21 additions & 9 deletions gui/wxpython/image2target/ii2t_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import shutil
import sys
from copy import copy
from pathlib import Path
from typing import TYPE_CHECKING

import wx
Expand Down Expand Up @@ -490,7 +491,10 @@ def OnLocation(self, event):
self.xylocation = event.GetString()

# create a list of valid mapsets
tmplist = os.listdir(os.path.join(self.grassdatabase, self.xylocation))
tmplist = [
p.name
for p in Path(os.path.join(self.grassdatabase, self.xylocation)).iterdir()
]
self.mapsetList = []
for item in tmplist:
if os.path.isdir(
Expand Down Expand Up @@ -711,11 +715,14 @@ def OnEnterPage(self, event: WizardEvent | None = None) -> None:
if os.path.isdir(
os.path.join(self.grassdatabase, self.xylocation, self.xymapset, "group")
):
tmplist = os.listdir(
os.path.join(
self.grassdatabase, self.xylocation, self.xymapset, "group"
)
)
tmplist = [
p.name
for p in Path(
os.path.join(
self.grassdatabase, self.xylocation, self.xymapset, "group"
)
).iterdir()
]
for item in tmplist:
if os.path.isdir(
os.path.join(
Expand Down Expand Up @@ -2579,9 +2586,14 @@ def __init__(
#
# get list of valid vector directories
#
vectlist = os.listdir(
os.path.join(self.grassdatabase, self.xylocation, self.xymapset, "vector")
)
vectlist = [
p.name
for p in Path(
os.path.join(
self.grassdatabase, self.xylocation, self.xymapset, "vector"
)
).iterdir()
]
for dir in vectlist:
if not os.path.isfile(
os.path.join(
Expand Down
8 changes: 4 additions & 4 deletions gui/wxpython/psmap/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2206,9 +2206,9 @@ def __init__(self, parent, id, vectors, tmpSettings):
gisbase = os.getenv("GISBASE")
self.symbolPath = os.path.join(gisbase, "etc", "symbol")
self.symbols = []
for dir in os.listdir(self.symbolPath):
for symbol in os.listdir(os.path.join(self.symbolPath, dir)):
self.symbols.append(os.path.join(dir, symbol))
for dir_path in Path(self.symbolPath).iterdir():
for symbol in Path(dir_path).iterdir():
self.symbols.append(os.path.join(dir_path.name, symbol.name))
self.patternPath = os.path.join(gisbase, "etc", "paint", "patterns")

# notebook
Expand Down Expand Up @@ -5929,7 +5929,7 @@ def OnDirChanged(self, event):
"""Image directory changed"""
path = self.imagePanel.image["dir"].GetValue()
try:
files = os.listdir(path)
files = [p.name for p in Path(path).iterdir()]
except OSError: # no such directory
files = []
imageList = []
Expand Down
12 changes: 4 additions & 8 deletions gui/wxpython/rlisetup/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import wx
import os
from pathlib import Path

from core import globalvar, gcmd
from grass.script.utils import try_remove
Expand Down Expand Up @@ -209,15 +210,10 @@ def _layout(self):
self.Layout()

def ListFiles(self):
"""Check the configuration files inside the path"""
# list of configuration file
# return all the configuration files in self.rlipath, check if there are
"""Return a sorted list of configuration files"""
# check in each configuration file if it is a real file, not a symbolic
# link or directory and doesn't add them
listfiles = [
rli_conf
for rli_conf in os.listdir(self.rlipath)
if os.path.isfile(os.path.join(self.rlipath, rli_conf))
]
listfiles = [p.name for p in Path(self.rlipath).iterdir() if p.is_file()]
return sorted(listfiles)

def OnClose(self, event):
Expand Down
9 changes: 4 additions & 5 deletions lib/init/testsuite/test_grass_tmp_mapset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
"""

import unittest
import os
import shutil
import subprocess
from grass.gunittest.utils import xfail_windows

from pathlib import Path

# Note that unlike rest of GRASS GIS, here we are using unittest package
# directly. The grass.gunittest machinery for mapsets is not needed here.
Expand All @@ -38,7 +37,7 @@ class TestTmpMapset(unittest.TestCase):
def setUp(self):
"""Creates a location used in the tests"""
subprocess.check_call([self.executable, "-c", "XY", self.location, "-e"])
self.subdirs = os.listdir(self.location)
self.subdirs = [p.name for p in Path(self.location).iterdir()]

def tearDown(self):
"""Deletes the location"""
Expand Down Expand Up @@ -101,9 +100,9 @@ def test_mapset_deleted(self):
subprocess.check_call(
[self.executable, "--tmp-mapset", self.location, "--exec", "g.proj", "-p"]
)
for directory in os.listdir(self.location):
for directory in Path(self.location).iterdir():
self.assertTrue(
directory in self.subdirs,
directory.name in self.subdirs,
msg="Directory {directory} should have been deleted".format(**locals()),
)

Expand Down
2 changes: 1 addition & 1 deletion man/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def copy_file(src, dst):


def get_files(man_dir, cls=None, ignore_gui=True, extension="html"):
for cmd in sorted(os.listdir(man_dir)):
for cmd in sorted([p.name for p in Path(man_dir).iterdir()]):
if (
cmd.endswith(f".{extension}")
and (cls in (None, "*") or cmd.startswith(cls + "."))
Expand Down
3 changes: 2 additions & 1 deletion man/build_class_graphical.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
)

import build_md
from pathlib import Path


graphical_index_style = """\
Expand Down Expand Up @@ -275,7 +276,7 @@ def main():
img_extensions = ["png", "jpg", "gif"]
img_patterns = ["*." + extension for extension in img_extensions]
imgs = []
for filename in sorted(os.listdir(html_dir)):
for filename in sorted([p.name for p in Path(html_dir).iterdir()]):
if file_matches(filename, img_patterns):
imgs.append(filename)

Expand Down
8 changes: 4 additions & 4 deletions man/build_manual_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def file_matches(filename: str, patterns: Iterable[str]):

def get_files(directory, patterns, exclude_patterns):
files = []
for filename in sorted(os.listdir(directory)):
for filename in sorted([p.name for p in Path(directory).iterdir()]):
if file_matches(filename, patterns):
if not file_matches(filename, exclude_patterns):
files.append(filename)
Expand Down Expand Up @@ -177,10 +177,10 @@ def main(ext):
)
img_files = {}

for filename in os.listdir(man_dir):
if filename in img_blacklist:
for filename in Path(man_dir).iterdir():
if filename.name in img_blacklist:
continue
if file_matches(filename, img_patterns):
if file_matches(filename.name, img_patterns):
width = image_width(Path(man_dir, filename))
if width is not None and width < MIN_IMAGE_WIDTH:
# Skip small images.
Expand Down
2 changes: 1 addition & 1 deletion man/build_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def copy_file(src, dst):


def rest_files(cls=None):
for cmd in sorted(os.listdir(rest_dir)):
for cmd in sorted([p.name for p in Path(rest_dir).iterdir()]):
if (
cmd.endswith(".txt")
and (cls in {None, "*"} or cmd.startswith(cls + "."))
Expand Down
24 changes: 5 additions & 19 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -283,52 +283,40 @@ ignore = [
"**.py" = ["PYI066"]
"*/testsuite/**.py" = ["PT009", "PT027"]
"gui/wxpython/iclass/statistics.py" = ["A005"]
"gui/wxpython/icons/grass_icons.py" = ["PTH208"]
"gui/wxpython/image2target/ii2t_manager.py" = ["PTH208", "SIM115"]
"gui/wxpython/image2target/ii2t_manager.py" = ["SIM115"]
"gui/wxpython/mapdisp/main.py" = ["SIM115"]
"gui/wxpython/modules/colorrules.py" = ["SIM115"]
"gui/wxpython/modules/mcalc_builder.py" = ["SIM115"]
"gui/wxpython/photo2image/ip2i_manager.py" = ["SIM115"]
"gui/wxpython/psmap/dialogs.py" = ["PTH208"]
"gui/wxpython/psmap/instructions.py" = ["SIM115"]
"gui/wxpython/rlisetup/frame.py" = ["PTH208"]
"gui/wxpython/tools/update_menudata.py" = ["SIM115"]
"gui/wxpython/vnet/vnet_data.py" = ["SIM115"]
"gui/wxpython/web_services/dialogs.py" = ["SIM115"]
"gui/wxpython/wxplot/profile.py" = ["A005", "SIM115"]
"lib/imagery/testsuite/test_imagery_signature_management.py" = ["SIM115"]
"lib/imagery/testsuite/test_imagery_sigsetfile.py" = ["FURB152"]
"lib/init/grass.py" = ["SIM115"]
"lib/init/testsuite/test_grass_tmp_mapset.py" = ["PTH208"]
"locale/grass_po_stats.py" = ["SIM115"]
"man/build.py" = ["PTH208"]
"man/build_class_graphical.py" = ["PTH208"]
"man/build_manual_gallery.py" = ["PTH208"]
"man/build_rest.py" = ["PTH208"]
"python/grass/__init__.py" = ["PYI056"]
"python/grass/exp*/tests/grass_script_mapset_session_test.py" = ["SIM117"]
"python/grass/exp*/tests/grass_script_tmp_mapset_session_test.py" = ["SIM117"]
"python/grass/gunittest/case.py" = ["PT009"]
"python/grass/gunittest/loader.py" = ["PTH208", "PYI024"]
"python/grass/gunittest/loader.py" = ["PYI024"]
"python/grass/gunittest/multireport.py" = ["PYI024"]
"python/grass/gunittest/testsu*/d*/s*/s*/subsub*/t*/test_segfaut.py" = ["B018"]
"python/grass/imaging/images2ims.py" = ["PTH208"]
"python/grass/jupyter/testsuite/interactivemap_test.py" = ["PGH004"]
"python/grass/jupyter/testsuite/map_test.py" = ["PGH004"]
"python/grass/pydispatch/signal.py" = ["A005"]
"python/grass/pygrass/gis/__init__.py" = ["PTH208"]
"python/grass/pygrass/modules/grid/grid.py" = ["PTH208", "SIM115"]
"python/grass/pygrass/modules/grid/grid.py" = ["SIM115"]
"python/grass/pygrass/modules/grid/testsuite/test_*_modules_grid_doctests.py" = ["F401"]
"python/grass/pygrass/modules/interface/env.py" = ["SIM115"]
"python/grass/pygrass/modules/testsuite/test_pygrass_modules_doctests.py" = ["F401"]
"python/grass/pygrass/raster/category.py" = ["FURB189"]
"python/grass/pygrass/raster/segment.py" = ["SIM115"]
"python/grass/pygrass/tests/*.py" = ["SIM115"]
"python/grass/pygrass/utils.py" = ["PTH208"]
"python/grass/pygrass/vector/geometry.py" = ["PYI024"]
"python/grass/pygrass/vector/testsuite/test_table.py" = ["PLW0108"]
"python/grass/script/array.py" = ["A005"]
"python/grass/script/core.py" = ["PTH208"]
"python/grass/script/utils.py" = ["FURB189"]
"python/grass/temporal/aggregation.py" = ["SIM115"]
"python/grass/temporal/register.py" = ["SIM115"]
Expand All @@ -337,7 +325,7 @@ ignore = [
"python/grass/temporal/temporal_algebra.py" = ["D300"]
"python/grass/temporal/temporal_operator.py" = ["D300"]
"python/grass/temporal/univar_statistics.py" = ["SIM115"]
"python/grass/utils/download.py" = ["PTH208", "SIM115"]
"python/grass/utils/download.py" = ["SIM115"]
"raster/r.*/testsuite/*.py" = ["SIM115"]
"raster/r.topidx/*.py" = ["SIM115"]
"scripts/d.correlate/d.correlate.py" = ["SIM115"]
Expand All @@ -346,10 +334,8 @@ ignore = [
"scripts/db.in.ogr/db.in.ogr.py" = ["SIM115"]
"scripts/db.test/db.test.py" = ["SIM115"]
"scripts/db.univar/db.univar.py" = ["SIM115"]
"scripts/g.download.project/g.download.project.py" = ["PTH208"]
"scripts/g.extension.all/g.extension.all.py" = ["SIM115"]
"scripts/g.extension/g.extension.py" = ["PTH208", "SIM115"]
"scripts/g.extension/testsuite/test_addons_modules.py" = ["PTH208"]
"scripts/g.extension/g.extension.py" = ["SIM115"]
"scripts/g.search.modules/g.search.modules.py" = ["SIM115"]
"scripts/i.in.spotvgt/i.in.spotvgt.py" = ["SIM115"]
"scripts/i.oif/i.oif*.py" = ["SIM115"]
Expand Down
4 changes: 2 additions & 2 deletions python/grass/gunittest/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import os
import re
import unittest
from pathlib import PurePath
from pathlib import PurePath, Path
from typing import TYPE_CHECKING

if TYPE_CHECKING:
Expand Down Expand Up @@ -118,7 +118,7 @@ def discover_modules(
dirs.remove(testsuite_dir) # do not recurse to testsuite
full = os.path.join(root, testsuite_dir)

files = os.listdir(full)
files = [p.name for p in Path(full).iterdir()]
if file_pattern:
files = fnmatch.filter(files, file_pattern)
if file_regexp:
Expand Down
7 changes: 4 additions & 3 deletions python/grass/imaging/images2ims.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import os
from operator import itemgetter
from string import digits
from pathlib import Path

try:
import numpy as np
Expand Down Expand Up @@ -208,10 +209,10 @@ def readIms(filename, asNumpy=True):
images = []

# Get all files in directory
for fname in os.listdir(dirname):
if fname.startswith(part1) and fname.endswith(part2):
for fname in Path(dirname).iterdir():
if fname.name.startswith(part1) and fname.name.endswith(part2):
# Get sequence number
nr = _getSequenceNumber(fname, part1, part2)
nr = _getSequenceNumber(fname.name, part1, part2)
# Get Pil image and store copy (to prevent keeping the file)
im = PIL.Image.open(os.path.join(dirname, fname))
images.append((im.copy(), nr))
Expand Down
14 changes: 7 additions & 7 deletions python/grass/pygrass/gis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/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
Expand All @@ -11,6 +10,7 @@
from grass.pygrass.errors import GrassError
from grass.script.utils import encode, decode
from grass.pygrass.utils import getenv
from pathlib import Path

test_vector_name = "Gis_test_vector"
test_raster_name = "Gis_test_raster"
Expand Down Expand Up @@ -187,8 +187,8 @@ def locations(self):
"""
return sorted(
[
loc
for loc in listdir(self.name)
loc.name
for loc in Path(self.name).iterdir()
if libgis.G_is_location(encode(join(self.name, loc)))
]
)
Expand Down Expand Up @@ -241,9 +241,9 @@ def __getitem__(self, mapset):
def __iter__(self):
lpath = self.path()
return (
m
for m in listdir(lpath)
if (isdir(join(lpath, m)) and is_valid(m, lpath, "MAPSET"))
m.name
for m in Path(lpath).iterdir()
if (m.is_dir() and is_valid(m.name, lpath, "MAPSET"))
)

def __len__(self):
Expand Down
6 changes: 3 additions & 3 deletions python/grass/pygrass/modules/grid/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from grass.pygrass.gis.region import Region
from grass.pygrass.modules import Module
from grass.pygrass.utils import get_mapset_raster, findmaps

from pathlib import Path
from grass.pygrass.modules.grid.split import (
split_region_tiles,
split_region_in_overlapping_tiles,
Expand Down Expand Up @@ -58,8 +58,8 @@ def copy_special_mapset_files(path_src, path_dst):
:param path_dst: the path to the new mapset
:type path_dst: str
"""
for fil in (fi for fi in os.listdir(path_src) if fi.isupper()):
sht.copy(os.path.join(path_src, fil), path_dst)
for fil in (fi for fi in Path(path_src).iterdir() if fi.isupper()):
sht.copy(os.path.join(path_src, fil.name), path_dst)


def copy_mapset(mapset, path):
Expand Down
Loading
Loading