Skip to content
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
18 changes: 13 additions & 5 deletions gui/wxpython/core/menutree.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@
@author Anna Petrasova <kratochanna gmail.com>
"""

from __future__ import annotations

import os
import sys
import copy
import xml.etree.ElementTree as ET

from typing import TYPE_CHECKING

import wx

from core.treemodel import TreeModel, ModuleNode
Expand All @@ -50,8 +54,12 @@
if not os.getenv("GISBASE"):
sys.exit("GRASS is not running. Exiting...")

if TYPE_CHECKING:
from typing_extensions import Writer

# TODO: change the system to remove strange derived classes


class MenuTreeModelBuilder:
"""Abstract menu data class"""

Expand All @@ -69,7 +77,7 @@ def __init__(self, filename, expandAddons=True, message_handler=GError):
message_handler(message)
clearToolboxMessages()

self.model = TreeModel(ModuleNode)
self.model: TreeModel = TreeModel(ModuleNode)
self._createModel(xmlTree)

def _createModel(self, xmlTree):
Expand Down Expand Up @@ -152,7 +160,7 @@ def PrintTree(self, fh):
for child in self.model.root.children:
printTree(node=child, fh=fh)

def PrintStrings(self, fh):
def PrintStrings(self, fh: Writer[str]):
"""Print menu strings to file (used for localization)

:param fh: file descriptor
Expand Down Expand Up @@ -186,15 +194,15 @@ def printTree(node, fh, indent=0):
printTree(node=child, fh=fh, indent=indent + 2)


def printStrings(node, fh):
def printStrings(node, fh: Writer[str]):
# node.label - with module in brackets
# node.data['label'] - without module in brackets
if node.label and not node.data:
fh.write(" _(%r),\n" % str(node.label))
if node.data:
if "label" in node.data and node.data["label"]:
if node.data.get("label"):
fh.write(" _(%r),\n" % str(node.data["label"]))
if "description" in node.data and node.data["description"]:
if node.data.get("description"):
fh.write(" _(%r),\n" % str(node.data["description"]))
for child in node.children:
printStrings(node=child, fh=fh)
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def GetLayerNameFromCmd(dcmd, fullyQualified=False, param=None, layerType=None):
if p == "layer":
continue
dcmd[i] = p + "=" + v
if i in mapsets and mapsets[i]:
if mapsets.get(i):
dcmd[i] += "@" + mapsets[i]

maps = []
Expand Down
12 changes: 9 additions & 3 deletions gui/wxpython/gmodeler/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
@author Ondrej Pesek <pesej.ondrek gmail.com>
"""

from __future__ import annotations
from typing import TYPE_CHECKING

import os
import getpass
import copy
Expand Down Expand Up @@ -54,6 +57,9 @@

from grass.script import task as gtask

if TYPE_CHECKING:
from typing_extensions import Writer


class Model:
"""Class representing the model"""
Expand Down Expand Up @@ -1192,8 +1198,8 @@ def _processComments(self):
class WriteModelFile:
"""Generic class for writing model file"""

def __init__(self, fd, model):
self.fd = fd
def __init__(self, fd: Writer[str], model: Model):
self.fd = fd # Should be io.Writer when Python 3.14 is possible to use
self.model = model
self.properties = model.GetProperties()
self.variables = model.GetVariables()
Expand Down Expand Up @@ -1272,7 +1278,7 @@ def _properties(self):
% (" " * self.indent, self.properties["author"])
)

if "overwrite" in self.properties and self.properties["overwrite"]:
if self.properties.get("overwrite"):
self.fd.write('%s<flag name="overwrite" />\n' % (" " * self.indent))
self.indent -= 4
self.fd.write("%s</properties>\n" % (" " * self.indent))
Expand Down
4 changes: 3 additions & 1 deletion gui/wxpython/gmodeler/panels.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
@author Python exports Ondrej Pesek <pesej.ondrek gmail.com>
"""

from __future__ import annotations

import os
import time
import stat
Expand Down Expand Up @@ -147,7 +149,7 @@ def __init__(
)
self.canvas.SetCursor(self.cursors["default"])

self.model = Model(giface=self._giface, canvas=self.canvas)
self.model: Model = Model(giface=self._giface, canvas=self.canvas)

self.variablePanel = VariablePanel(parent=self)

Expand Down
10 changes: 4 additions & 6 deletions gui/wxpython/image2target/ii2t_gis_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,8 @@ def RenameMapset(self, event):
)
else:
try:
os.rename(
os.path.join(self.gisdbase, location, mapset),
os.path.join(self.gisdbase, location, newmapset),
Path(self.gisdbase, location, mapset).rename(
Path(self.gisdbase, location, newmapset)
)
self.OnSelectLocation(None)
self.lbmapsets.SetSelection(self.listOfMapsets.index(newmapset))
Expand Down Expand Up @@ -767,9 +766,8 @@ def RenameLocation(self, event):
)
else:
try:
os.rename(
os.path.join(self.gisdbase, location),
os.path.join(self.gisdbase, newlocation),
Path(self.gisdbase, location).rename(
Path(self.gisdbase, newlocation)
)
self.UpdateLocations(self.gisdbase)
self.lblocations.SetSelection(
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/rlisetup/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def OnRename(self, event):
if dlg.ShowModal() == wx.ID_OK:
res = dlg.GetValue()
newname = "%s%s%s" % (self.rlipath, os.sep, res)
os.rename(os.path.join(self.rlipath, confile), newname)
Path(self.rlipath, confile).rename(newname)
self.listfiles = self.ListFiles()
self.listfileBox.Clear()
self.listfileBox.Set(self.listfiles)
Expand Down
2 changes: 1 addition & 1 deletion man/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def replace_file(name):
os.remove(name)
except OSError:
pass
os.rename(temp, name)
Path(temp).rename(name)


def copy_file(src, dst):
Expand Down
2 changes: 1 addition & 1 deletion man/build_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def replace_file(name):
os.remove(name)
except OSError:
pass
os.rename(temp, name)
Path(temp).rename(name)


def copy_file(src, dst):
Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ ignore = [
"PLW3201", # bad-dunder-method-name
"PTH100", # os-path-abspath
"PTH101", # os-chmod
"PTH104", # os-rename
"PTH106", # os-rmdir
"PTH107", # os-remove
"PTH108", # os-unlink
Expand All @@ -282,7 +281,6 @@ ignore = [
"RUF005", # collection-literal-concatenation
"RUF012", # mutable-class-default
"RUF015", # unnecessary-iterable-allocation-for-first-element
"RUF019", # unnecessary-key-check
"RUF050", # unnecessary-if
"RUF059", # unused-unpacked-variable
"RUF067", # non-empty-init-module
Expand Down
8 changes: 3 additions & 5 deletions python/grass/grassdb/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ def rename_mapset(database, location, old_name, new_name):
"""Rename mapset from *old_name* to *new_name*"""
if old_name == "PERMANENT":
raise ValueError(_("Mapset PERMANENT cannot be renamed"))
location_path = os.path.join(database, location)
os.rename(
os.path.join(location_path, old_name), os.path.join(location_path, new_name)
)
location_path = Path(database, location)
(location_path / old_name).rename(location_path / new_name)


def rename_location(database, old_name, new_name):
"""Rename location from *old_name* to *new_name*"""
os.rename(os.path.join(database, old_name), os.path.join(database, new_name))
Path(database, old_name).rename(Path(database, new_name))


class MapsetPath:
Expand Down
2 changes: 1 addition & 1 deletion python/grass/gunittest/reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def replace_in_file(file_path, pattern, repl) -> None:
os.remove(file_path)
# replace old file by new file
# TODO: this can fail in some (random) cases on MS Windows
os.rename(tmp_file_path, file_path)
Path(tmp_file_path).rename(file_path)


class NoopFileAnonymizer:
Expand Down
14 changes: 13 additions & 1 deletion python/grass/pygrass/modules/interface/flag.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from grass.pygrass.modules.interface.docstring import docstring_property
from grass.pygrass.modules.interface import read


if TYPE_CHECKING:
from xml.etree.ElementTree import Element
Comment thread
echoix marked this conversation as resolved.
Dismissed


class Flag:
"""The Flag object store all information about a flag of module.

Expand All @@ -21,7 +29,11 @@
True
"""

def __init__(self, xflag=None, diz=None):
def __init__(
self,
xflag: Element[str] | None = None,
diz: dict[str, str | bool | None] | None = None,
):
self.value = False
diz = read.element2dict(xflag) if xflag is not None else diz
self.name = diz["name"]
Expand Down
8 changes: 5 additions & 3 deletions python/grass/pygrass/modules/interface/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,9 @@ def __init__(self, cmd, *args, **kargs):
#
# extract parameters from the xml
#
self.params_list = [Parameter(p) for p in tree.findall("parameter")]
self.params_list: list[Parameter] = [
Parameter(xparameter=p) for p in tree.findall("parameter")
]
self.inputs = TypeDict(Parameter)
self.outputs = TypeDict(Parameter)
self.required = []
Expand All @@ -604,7 +606,7 @@ def __init__(self, cmd, *args, **kargs):
#
# extract flags from the xml
#
flags_list = [Flag(f) for f in tree.findall("flag")]
flags_list = [Flag(xflag=f) for f in tree.findall("flag")]
self.flags = TypeDict(Flag)
for flag in flags_list:
self.flags[flag.name] = flag
Expand All @@ -620,7 +622,7 @@ def __init__(self, cmd, *args, **kargs):
self.stdin = None
self.stdout_ = None
self.stderr_ = None
diz = {
diz: dict[str, str | bool | list | tuple | None] = {
"name": "stdin",
"required": False,
"multiple": False,
Expand Down
16 changes: 13 additions & 3 deletions python/grass/pygrass/modules/interface/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
@author: pietro
"""

from __future__ import annotations
import re
from typing import TYPE_CHECKING


from grass.pygrass.modules.interface.docstring import docstring_property
from grass.pygrass.modules.interface.read import GETTYPE, element2dict, DOC

if TYPE_CHECKING:
from xml.etree.ElementTree import Element
Comment thread
echoix marked this conversation as resolved.
Dismissed


def _check_value(param, value):
"""Function to check the correctness of a value and
Expand Down Expand Up @@ -153,7 +159,11 @@
...
""" # noqa: E501

def __init__(self, xparameter=None, diz=None):
def __init__(
self,
xparameter: Element[str] | None = None,
diz: dict[str, str | bool | float | list | tuple | None] | None = None,
):
self._value = None
self._rawvalue = None
self.min = None
Expand Down Expand Up @@ -201,7 +211,7 @@
#
# default
#
if "default" in diz and diz["default"]:
if diz.get("default"):
if self.multiple or self.keydescvalues:
self.default = [self.type(v) for v in diz["default"].split(",")]
else:
Expand All @@ -214,7 +224,7 @@
#
# gisprompt
#
if "gisprompt" in diz and diz["gisprompt"]:
if diz.get("gisprompt"):
self.typedesc = diz["gisprompt"].get("prompt", "")
self.input = diz["gisprompt"]["age"] != "new"
else:
Expand Down
Loading
Loading