Skip to content

Commit

Permalink
Use more pathlib instead of os module
Browse files Browse the repository at this point in the history
  • Loading branch information
AlisterH authored and nyalldawson committed Jul 6, 2023
1 parent 0420b15 commit 158b0e2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
6 changes: 3 additions & 3 deletions python/plugins/grassprovider/Grass7Algorithm.py
Expand Up @@ -142,14 +142,14 @@ def __init__(self, descriptionfile):
name = self.name().replace('.', '_')
self.module = None
try:
extpath = Path(self.descriptionFile).parents[1].joinpath('ext', name + '.py')
extpath = self.descriptionFile.parents[1].joinpath('ext', name + '.py')
# this check makes it a bit faster
if extpath.exists():
spec = importlib.util.spec_from_file_location('grassprovider.ext.' + name, extpath)
self.module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(self.module)
except Exception as e:
QgsMessageLog.logMessage(self.tr('Failed to load: {0}\n{1}').format(extpath, str(e)), 'Processing', Qgis.Critical)
QgsMessageLog.logMessage(self.tr('Failed to load: {0}\n{1}').format(extpath, e), 'Processing', Qgis.Critical)
pass

def createInstance(self):
Expand Down Expand Up @@ -207,7 +207,7 @@ def defineCharacteristicsFromFile(self):
"""
Create algorithm parameters and outputs from a text file.
"""
with open(self.descriptionFile) as lines:
with self.descriptionFile.open() as lines:
# First line of the file is the Grass algorithm name
line = lines.readline().strip('\n').strip()
self.grass7Name = line
Expand Down
22 changes: 11 additions & 11 deletions python/plugins/grassprovider/Grass7AlgorithmProvider.py
Expand Up @@ -20,6 +20,7 @@
__copyright__ = '(C) 2014, Victor Olaya'

import os
from pathlib import Path
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (Qgis,
QgsApplication,
Expand Down Expand Up @@ -87,17 +88,16 @@ def createAlgsList(self):
algs = []
folders = self.descriptionFolders
for folder in folders:
for descriptionFile in os.listdir(folder):
if descriptionFile.endswith('txt'):
try:
alg = Grass7Algorithm(os.path.join(folder, descriptionFile))
if alg.name().strip() != '':
algs.append(alg)
else:
QgsMessageLog.logMessage(self.tr('Could not open GRASS GIS 7 algorithm: {0}').format(descriptionFile), self.tr('Processing'), Qgis.Critical)
except Exception as e:
QgsMessageLog.logMessage(
self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(descriptionFile, str(e)), self.tr('Processing'), Qgis.Critical)
for descriptionFile in folder.glob('*.txt'):
try:
alg = Grass7Algorithm(descriptionFile)
if alg.name().strip() != '':
algs.append(alg)
else:
QgsMessageLog.logMessage(self.tr('Could not open GRASS GIS 7 algorithm: {0}').format(str(descriptionFile)), self.tr('Processing'), Qgis.Critical)
except Exception as e:
QgsMessageLog.logMessage(
self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(str(descriptionFile), str(e)), self.tr('Processing'), Qgis.Critical)
return algs

def loadAlgorithms(self):
Expand Down
13 changes: 7 additions & 6 deletions python/plugins/grassprovider/Grass7Utils.py
Expand Up @@ -25,6 +25,7 @@
import subprocess
import os
import sys
from pathlib import Path

from qgis.core import (Qgis,
QgsApplication,
Expand Down Expand Up @@ -256,12 +257,12 @@ def grassPath():
@staticmethod
def userDescriptionFolder():
"""
Creates and returns a directory for users to create additional algorithm descriptions
or modified versions of stock algorithm descriptions shipped with QGIS.
Creates and returns a directory for users to create additional algorithm descriptions.
Or modified versions of stock algorithm descriptions shipped with QGIS.
"""
folder = os.path.join(userFolder(), 'grassaddons', 'description')
mkdir(folder)
return os.path.abspath(folder)
folder = Path(userFolder(), 'grassaddons', 'description')
folder.mkdir(parents=True, exist_ok=True)
return folder

@staticmethod
def grassDescriptionFolders():
Expand All @@ -270,7 +271,7 @@ def grassDescriptionFolders():
Note that the provider will load from these in sequence, so we put the userDescriptionFolder first
to allow users to create modified versions of stock algorithms shipped with QGIS.
"""
return [Grass7Utils.userDescriptionFolder(), os.path.join(os.path.dirname(__file__), 'description')]
return [Grass7Utils.userDescriptionFolder(), Path(__file__).parent.joinpath('description')]

@staticmethod
def getWindowsCodePage():
Expand Down

0 comments on commit 158b0e2

Please sign in to comment.