Commit 24e87857 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[utils] Implement checks for prefix

The checks verify that:
- The prefix has all asset subfolders
- The mandatory items are available

Currently the only mandatory item is the
integers data format needed by the
algorithm prototype.

Both checks will generate the needed
elements if accepted.
parent 0bf57ddd
...@@ -29,8 +29,14 @@ ...@@ -29,8 +29,14 @@
Test the utils.py file Test the utils.py file
""" """
import os
import tempfile
from PyQt5 import QtCore from PyQt5 import QtCore
from PyQt5.QtWidgets import QMessageBox
from ..backend.asset import AssetType
from .. import utils from .. import utils
...@@ -44,3 +50,67 @@ def test_Qt_version_higher(): ...@@ -44,3 +50,67 @@ def test_Qt_version_higher():
def test_Qt_version_smaller(): def test_Qt_version_smaller():
assert not utils.is_Qt_equal_or_higher("12.0.0") assert not utils.is_Qt_equal_or_higher("12.0.0")
def test_check_prefix_folders_yes(monkeypatch):
monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.Yes)
with tempfile.TemporaryDirectory() as prefix_folder:
result, modified = utils.check_prefix_folders(prefix_folder)
assert result
assert modified
for asset_type in AssetType:
if asset_type == AssetType.UNKNOWN:
continue
assert os.path.exists(os.path.join(prefix_folder, asset_type.path))
result, modified = utils.check_prefix_folders(prefix_folder)
assert result
assert not modified
def test_check_prefix_folders_no(monkeypatch):
monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.No)
with tempfile.TemporaryDirectory() as prefix_folder:
result, modified = utils.check_prefix_folders(prefix_folder)
assert not result
assert not modified
assert os.listdir(prefix_folder) == []
def test_check_prefix_folders_with_prefix(monkeypatch, test_prefix):
result, modified = utils.check_prefix_folders(test_prefix)
assert result
assert not modified
def test_check_prefix_dataformats_yes(monkeypatch):
monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.Yes)
with tempfile.TemporaryDirectory() as prefix_folder:
result, modified = utils.check_prefix_dataformats(prefix_folder)
assert result
assert modified
asset_type = AssetType.DATAFORMAT
assert os.path.exists(
os.path.join(prefix_folder, asset_type.path, "user", "integers")
)
result, modified = utils.check_prefix_dataformats(prefix_folder)
assert result
assert not modified
def test_check_prefix_dataformats_no(monkeypatch):
monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.No)
with tempfile.TemporaryDirectory() as prefix_folder:
result, modified = utils.check_prefix_dataformats(prefix_folder)
assert not result
assert not modified
assert os.listdir(prefix_folder) == []
def test_check_prefix_dataformats_with_prefix(monkeypatch, test_prefix):
result, modified = utils.check_prefix_dataformats(test_prefix)
assert result
assert not modified
...@@ -34,9 +34,17 @@ import sys ...@@ -34,9 +34,17 @@ import sys
import logging import logging
import simplejson as json import simplejson as json
import pkg_resources import pkg_resources
import shutil
from packaging import version from packaging import version
from PyQt5 import QtCore from PyQt5 import QtCore
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtWidgets import QMessageBox
from beat.core.algorithm import load_algorithm_prototype
from .backend.asset import AssetType
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -114,3 +122,76 @@ def dataformat_basetypes(): ...@@ -114,3 +122,76 @@ def dataformat_basetypes():
def is_Qt_equal_or_higher(version_string): def is_Qt_equal_or_higher(version_string):
return version.parse(QtCore.QT_VERSION_STR) >= version.parse(version_string) return version.parse(QtCore.QT_VERSION_STR) >= version.parse(version_string)
def check_prefix_folders(prefix_path):
"""Check that all supported asset types have their containing folder
available
"""
modified = False
result = True
missing_folders = []
for asset_type in AssetType:
if asset_type is not AssetType.UNKNOWN:
path = os.path.join(prefix_path, asset_type.path)
if not os.path.exists(path):
missing_folders.append(path)
if missing_folders:
answer = QMessageBox.question(
None,
QCoreApplication.translate("utils", "Prefix incomplete"),
QCoreApplication.translate(
"utils",
"Your prefix is missing folders.\n" "Would you like to create them ?",
),
)
if answer == QMessageBox.Yes:
for folder in missing_folders:
os.makedirs(folder)
modified = True
else:
result = False
return result, modified
def check_prefix_dataformats(prefix_path):
"""Currently checks that the data format needed for the algorithm is
available
"""
modified = False
result = True
try:
load_algorithm_prototype(prefix_path)
except RuntimeError:
answer = QMessageBox.question(
None,
QCoreApplication.translate("utils", "Prefix incomplete"),
QCoreApplication.translate(
"utils",
"Your prefix is missing a mandatory data format.\n"
"Would you like to create it ?",
),
)
if answer == QMessageBox.Yes:
asset_type = AssetType.DATAFORMAT
integers_path = os.path.join("prefix", asset_type.path, "user", "integers")
integers_folder = pkg_resources.resource_filename(
"beat.core.test", integers_path
)
shutil.copytree(
integers_folder,
os.path.join(prefix_path, asset_type.path, "user", "integers"),
)
modified = True
else:
result = False
return result, modified
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