mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-16 00:05:45 -04:00
Use more pathlib instead of os module
This commit is contained in:
parent
0420b158ef
commit
158b0e2bae
@ -142,14 +142,14 @@ class Grass7Algorithm(QgsProcessingAlgorithm):
|
||||
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):
|
||||
@ -207,7 +207,7 @@ class Grass7Algorithm(QgsProcessingAlgorithm):
|
||||
"""
|
||||
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
|
||||
|
@ -20,6 +20,7 @@ __date__ = 'April 2014'
|
||||
__copyright__ = '(C) 2014, Victor Olaya'
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from qgis.PyQt.QtCore import QCoreApplication
|
||||
from qgis.core import (Qgis,
|
||||
QgsApplication,
|
||||
@ -87,17 +88,16 @@ class Grass7AlgorithmProvider(QgsProcessingProvider):
|
||||
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):
|
||||
|
@ -25,6 +25,7 @@ import shlex
|
||||
import subprocess
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from qgis.core import (Qgis,
|
||||
QgsApplication,
|
||||
@ -256,12 +257,12 @@ class Grass7Utils:
|
||||
@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():
|
||||
@ -270,7 +271,7 @@ class Grass7Utils:
|
||||
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():
|
||||
|
Loading…
x
Reference in New Issue
Block a user