Commit f496515e authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[widgets][algorithmeditor] Add helper method to update algorithm code

This will update the code from a python based algorithm
when changing type or making it an analyzer.
parent 17dc2805
......@@ -40,6 +40,7 @@ from ..widgets.algorithmeditor import ALGORITHM_TYPE
from ..widgets.algorithmeditor import DEFAULT_SCHEMA_VERSION
from ..widgets.algorithmeditor import DEFAULT_API_VERSION
from ..widgets.algorithmeditor import migrate_to_api_v2
from ..widgets.algorithmeditor import update_code
from .conftest import sync_prefix
from .conftest import prefix
......@@ -97,6 +98,38 @@ class TestHelperMethods:
assert v2_declaration["api_version"] == 2
assert v2_declaration["type"] == "legacy"
@pytest.mark.parametrize(
["algorithm_name", "new_type", "field_to_add"],
[
("autonomous/add/1", "legacy", None),
("autonomous/add/1", "sequential", None),
("sequential/add/1", "autonomous", None),
("sequential/add/1", "loop_user", None),
("sequential/add/1", "loop", None),
("autonomous/loop/1", "sequential", None),
("autonomous/loop/1", "sequential", "results"),
("autonomous/add/1", "sequential", "results"),
],
)
def test_code_update(self, test_prefix, algorithm_name, new_type, field_to_add):
asset = Asset(test_prefix, AssetType.ALGORITHM, algorithm_name)
with open(asset.code_path, "rt") as code_file:
original_code = code_file.read()
declaration = asset.declaration
declaration["type"] = new_type
if field_to_add:
declaration[field_to_add] = None
asset.declaration = declaration
update_code(asset)
with open(asset.code_path, "rt") as code_file:
updated_code = code_file.read()
assert updated_code != original_code
class TestPropertyEditor:
"""Test that the algorithm properties editor works as expected"""
......
......@@ -24,6 +24,7 @@
###############################################################################
import os
import re
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import pyqtSlot
......@@ -96,6 +97,49 @@ def migrate_to_api_v2(asset):
return status, new_asset
def update_code(asset):
declaration = asset.declaration
if declaration.get("language") == "python":
alg_type = declaration.get("type")
is_analyzer = "results" in declaration
with open(asset.code_path, "rt") as code_file:
lines = code_file.readlines()
process_line = None
validate_line = None
for index, line in enumerate(lines):
if "def process" in line:
process_line = index
if "def validate" in line:
validate_line = index
if process_line:
if is_analyzer:
lines[process_line] = re.sub(
r"^(\s*).*(\s*)$",
r"\g<1>{}\g<2>".format(ANALYZER_PROCESS_METHOD_MAP[alg_type]),
lines[process_line],
)
elif alg_type in ALGORITHM_PROCESS_METHOD_MAP:
lines[process_line] = re.sub(
r"^(\s*).*(\s*)$",
r"\g<1>{}\g<2>".format(ALGORITHM_PROCESS_METHOD_MAP[alg_type]),
lines[process_line],
)
elif alg_type != "loop":
lines.append(" {}\n".format(ALGORITHM_PROCESS_METHOD_MAP[alg_type]))
lines.append(" pass\n")
if alg_type == "loop" and not validate_line:
lines.append(" {}\n".format(LOOP_VALIDATE_METHOD))
lines.append(" pass\n")
with open(asset.code_path, "wt") as code_file:
code_file.write("".join(lines))
class PropertyEditor(QWidget):
dataChanged = pyqtSignal()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment