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

Model designer export as/run as python didn't catch newline in algorithm comments #50715

Closed
2 tasks done
WhitespaceQ opened this issue Oct 27, 2022 · 0 comments · Fixed by #53358
Closed
2 tasks done
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Modeller

Comments

@WhitespaceQ
Copy link

WhitespaceQ commented Oct 27, 2022

What is the bug or the crash?

When using model designer to export a model as a python script, if there is any algorithm that have a multi-line comment in Algorithm -> Comment the script won't work because the exporter fails to properly catch newline in the comment and will only commented out the first line only. This will throw a python SyntaxError.

Python script error:

An error has occurred while executing Python code: 

SyntaxError: invalid syntax 
Traceback (most recent call last):
  File "C:\PROGRA~1/QGIS32~1.11/apps/qgis-ltr/./python/plugins\processing\script\ScriptAlgorithmProvider.py", line 121, in loadAlgorithms
    alg = ScriptUtils.loadAlgorithm(moduleName, filePath)
  File "C:\PROGRA~1/QGIS32~1.11/apps/qgis-ltr/./python/plugins\processing\script\ScriptUtils.py", line 67, in loadAlgorithm
    spec.loader.exec_module(module)
  File "", line 851, in exec_module
  File "", line 988, in get_code
  File "", line 918, in source_to_code
  File "", line 228, in _call_with_frames_removed
  File "C:\Users\Asus\AppData\Roaming\QGIS\QGIS3\profiles\default\processing\scripts\EXPORTED_MODEL.py", line 32
    Uncommented newline
        ^
SyntaxError: invalid syntax


Python version: 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] 
QGIS version: 3.22.11-Białowieża Białowieża, 106d33d7 

Steps to reproduce the issue

  1. Create a simple model with one input and one algorithm
  2. Add a multi-line comment in the algorithm --> comment tab
  3. Export the model as a python script
  4. Try to run the exported script
    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        feedback = QgsProcessingMultiStepFeedback(1, model_feedback)
        results = {}
        outputs = {}

        # Points along geometry
        # This is a comment in line one
This is a comment in line two
        alg_params = {
            'DISTANCE': 1,
            'END_OFFSET': 0,
            'INPUT': parameters['test'],
            'START_OFFSET': 0,
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }

This behavior also occurs in model input comment

        self.addParameter(QgsProcessingParameterField('omit', 'OMITb', type=QgsProcessingParameterField.Any, parentLayerParameterName='before', allowMultiple=False, defaultValue=None))
        # This is a comment in line one
This is a comment in line two

Versions

QGIS version | 3.22.11-Białowieża | QGIS code revision | 106d33d7
-- | -- | -- | --
Qt version | 5.15.3
Python version | 3.9.5
GDAL/OGR version | 3.5.1
PROJ version | 9.1.0
EPSG Registry database version | v10.074 (2022-08-01)
GEOS version | 3.10.3-CAPI-1.16.1
SQLite version | 3.38.1
PDAL version | 2.4.3
PostgreSQL client version | 14.3
SpatiaLite version | 5.0.1
QWT version | 6.1.6
QScintilla2 version | 2.13.1
OS version | Windows 10 Version 2009
  |   |   |  
Active Python plugins
autoSaver | 2.6
batchvectorlayersaver | 0.9
CalculateGeometry | 0.6.8
FreehandRasterGeoreferencer | 0.8.3
latlontools | 3.6.6
profiletool | 4.2.2
qgsAzimuth | 0.9.15
QuickOSM | 2.1.1
quick_map_services | 0.19.29
SRTM-Downloader | 3.1.17
db_manager | 0.1.20
MetaSearch | 0.3.5
processing | 2.12.99
sagaprovider | 2.12.99

Supported QGIS version

  • I'm running a supported QGIS version according to the roadmap.

New profile

  • I tried with a new QGIS profile

Additional context

The script can be fixed by manually finding uncommented line in the exported script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Modeller
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants