mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-17 00:09:36 -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('.', '_')
|
name = self.name().replace('.', '_')
|
||||||
self.module = None
|
self.module = None
|
||||||
try:
|
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
|
# this check makes it a bit faster
|
||||||
if extpath.exists():
|
if extpath.exists():
|
||||||
spec = importlib.util.spec_from_file_location('grassprovider.ext.' + name, extpath)
|
spec = importlib.util.spec_from_file_location('grassprovider.ext.' + name, extpath)
|
||||||
self.module = importlib.util.module_from_spec(spec)
|
self.module = importlib.util.module_from_spec(spec)
|
||||||
spec.loader.exec_module(self.module)
|
spec.loader.exec_module(self.module)
|
||||||
except Exception as e:
|
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
|
pass
|
||||||
|
|
||||||
def createInstance(self):
|
def createInstance(self):
|
||||||
@ -207,7 +207,7 @@ class Grass7Algorithm(QgsProcessingAlgorithm):
|
|||||||
"""
|
"""
|
||||||
Create algorithm parameters and outputs from a text file.
|
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
|
# First line of the file is the Grass algorithm name
|
||||||
line = lines.readline().strip('\n').strip()
|
line = lines.readline().strip('\n').strip()
|
||||||
self.grass7Name = line
|
self.grass7Name = line
|
||||||
|
@ -20,6 +20,7 @@ __date__ = 'April 2014'
|
|||||||
__copyright__ = '(C) 2014, Victor Olaya'
|
__copyright__ = '(C) 2014, Victor Olaya'
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
from pathlib import Path
|
||||||
from qgis.PyQt.QtCore import QCoreApplication
|
from qgis.PyQt.QtCore import QCoreApplication
|
||||||
from qgis.core import (Qgis,
|
from qgis.core import (Qgis,
|
||||||
QgsApplication,
|
QgsApplication,
|
||||||
@ -87,17 +88,16 @@ class Grass7AlgorithmProvider(QgsProcessingProvider):
|
|||||||
algs = []
|
algs = []
|
||||||
folders = self.descriptionFolders
|
folders = self.descriptionFolders
|
||||||
for folder in folders:
|
for folder in folders:
|
||||||
for descriptionFile in os.listdir(folder):
|
for descriptionFile in folder.glob('*.txt'):
|
||||||
if descriptionFile.endswith('txt'):
|
try:
|
||||||
try:
|
alg = Grass7Algorithm(descriptionFile)
|
||||||
alg = Grass7Algorithm(os.path.join(folder, descriptionFile))
|
if alg.name().strip() != '':
|
||||||
if alg.name().strip() != '':
|
algs.append(alg)
|
||||||
algs.append(alg)
|
else:
|
||||||
else:
|
QgsMessageLog.logMessage(self.tr('Could not open GRASS GIS 7 algorithm: {0}').format(str(descriptionFile)), self.tr('Processing'), Qgis.Critical)
|
||||||
QgsMessageLog.logMessage(self.tr('Could not open GRASS GIS 7 algorithm: {0}').format(descriptionFile), self.tr('Processing'), Qgis.Critical)
|
except Exception as e:
|
||||||
except Exception as e:
|
QgsMessageLog.logMessage(
|
||||||
QgsMessageLog.logMessage(
|
self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(str(descriptionFile), str(e)), self.tr('Processing'), Qgis.Critical)
|
||||||
self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(descriptionFile, str(e)), self.tr('Processing'), Qgis.Critical)
|
|
||||||
return algs
|
return algs
|
||||||
|
|
||||||
def loadAlgorithms(self):
|
def loadAlgorithms(self):
|
||||||
|
@ -25,6 +25,7 @@ import shlex
|
|||||||
import subprocess
|
import subprocess
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from qgis.core import (Qgis,
|
from qgis.core import (Qgis,
|
||||||
QgsApplication,
|
QgsApplication,
|
||||||
@ -256,12 +257,12 @@ class Grass7Utils:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def userDescriptionFolder():
|
def userDescriptionFolder():
|
||||||
"""
|
"""
|
||||||
Creates and returns a directory for users to create additional algorithm descriptions
|
Creates and returns a directory for users to create additional algorithm descriptions.
|
||||||
or modified versions of stock algorithm descriptions shipped with QGIS.
|
Or modified versions of stock algorithm descriptions shipped with QGIS.
|
||||||
"""
|
"""
|
||||||
folder = os.path.join(userFolder(), 'grassaddons', 'description')
|
folder = Path(userFolder(), 'grassaddons', 'description')
|
||||||
mkdir(folder)
|
folder.mkdir(parents=True, exist_ok=True)
|
||||||
return os.path.abspath(folder)
|
return folder
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def grassDescriptionFolders():
|
def grassDescriptionFolders():
|
||||||
@ -270,7 +271,7 @@ class Grass7Utils:
|
|||||||
Note that the provider will load from these in sequence, so we put the userDescriptionFolder first
|
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.
|
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
|
@staticmethod
|
||||||
def getWindowsCodePage():
|
def getWindowsCodePage():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user