QGIS/python/plugins/grassprovider/parsed_description.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

133 lines
4.7 KiB
Python
Raw Normal View History

"""
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
import re
2024-12-01 08:15:14 +01:00
from dataclasses import dataclass, field
from pathlib import Path
2024-12-01 08:15:14 +01:00
from typing import Optional, List, Dict
@dataclass
class ParsedDescription:
"""
Results of parsing a description file
"""
2024-12-01 08:15:14 +01:00
GROUP_ID_REGEX = re.compile(r"^[^\s(]+")
grass_command: Optional[str] = None
short_description: Optional[str] = None
name: Optional[str] = None
display_name: Optional[str] = None
group: Optional[str] = None
group_id: Optional[str] = None
ext_path: Optional[str] = None
2024-12-01 08:15:14 +01:00
hardcoded_strings: list[str] = field(default_factory=list)
param_strings: list[str] = field(default_factory=list)
2024-12-01 08:15:14 +01:00
def as_dict(self) -> dict:
"""
Returns a JSON serializable dictionary representing the parsed
description
"""
return {
2024-12-01 08:15:14 +01:00
"name": self.name,
"display_name": self.display_name,
"command": self.grass_command,
"short_description": self.short_description,
"group": self.group,
"group_id": self.group_id,
"ext_path": self.ext_path,
"hardcoded_strings": self.hardcoded_strings,
"parameters": self.param_strings,
}
@staticmethod
2024-12-01 08:15:14 +01:00
def from_dict(description: dict) -> "ParsedDescription":
"""
Parses a dictionary as a description and returns the result
"""
from qgis.PyQt.QtCore import QCoreApplication
result = ParsedDescription()
2024-12-01 08:15:14 +01:00
result.name = description.get("name")
result.display_name = description.get("display_name")
result.grass_command = description.get("command")
result.short_description = QCoreApplication.translate(
2024-12-01 08:15:14 +01:00
"GrassAlgorithm", description.get("short_description")
)
2024-12-01 08:15:14 +01:00
result.group = QCoreApplication.translate(
"GrassAlgorithm", description.get("group")
)
result.group_id = description.get("group_id")
result.ext_path = description.get("ext_path")
result.hardcoded_strings = description.get("hardcoded_strings", [])
result.param_strings = description.get("parameters", [])
return result
@staticmethod
def parse_description_file(
2024-12-01 08:15:14 +01:00
description_file: Path, translate: bool = True
) -> "ParsedDescription":
"""
Parses a description file and returns the result
"""
result = ParsedDescription()
with description_file.open() as lines:
# First line of the file is the Grass algorithm name
2024-12-01 08:15:14 +01:00
line = lines.readline().strip("\n").strip()
result.grass_command = line
# Second line if the algorithm name in Processing
2024-12-01 08:15:14 +01:00
line = lines.readline().strip("\n").strip()
result.short_description = line
if " - " not in line:
result.name = result.grass_command
else:
2024-12-01 08:15:14 +01:00
result.name = line[: line.find(" ")].lower()
if translate:
from qgis.PyQt.QtCore import QCoreApplication
2024-12-01 08:15:14 +01:00
result.short_description = QCoreApplication.translate(
2024-12-01 08:15:14 +01:00
"GrassAlgorithm", line
)
else:
result.short_description = line
result.display_name = result.name
# Read the grass group
2024-12-01 08:15:14 +01:00
line = lines.readline().strip("\n").strip()
if translate:
from qgis.PyQt.QtCore import QCoreApplication
2024-12-01 08:15:14 +01:00
result.group = QCoreApplication.translate("GrassAlgorithm", line)
else:
result.group = line
2024-12-01 08:15:14 +01:00
result.group_id = (
ParsedDescription.GROUP_ID_REGEX.search(line).group(0).lower()
)
# Then you have parameters/output definition
2024-12-01 08:15:14 +01:00
line = lines.readline().strip("\n").strip()
while line != "":
line = line.strip("\n").strip()
if line.startswith("Hardcoded"):
result.hardcoded_strings.append(line[len("Hardcoded|") :])
result.param_strings.append(line)
2024-12-01 08:15:14 +01:00
line = lines.readline().strip("\n").strip()
return result