Commit 0b139605 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[backend][assetmodel] Refactor AssetType for code simplification

This update makes better use of the Enum class capabilities
and reduces the code to something simpler and clearer.
parent 2963817b
......@@ -25,6 +25,8 @@
import os
import beat.core
from enum import Enum, unique
from PyQt5.QtCore import pyqtSignal
......@@ -42,65 +44,33 @@ from ..utils import dataformat_basetypes
class AssetType(Enum):
"""All possible assets available on the BEAT platform"""
(
UNKNOWN,
ALGORITHM,
DATABASE,
DATAFORMAT,
EXPERIMENT,
LIBRARY,
PLOTTER,
PLOTTERPARAMETER,
PROTOCOLTEMPLATE,
TOOLCHAIN,
) = range(10)
@classmethod
def path(cls, type_):
path = "unknown"
if type_ == cls.ALGORITHM:
path = "algorithms"
elif type_ == cls.DATABASE:
path = "databases"
elif type_ == cls.DATAFORMAT:
path = "dataformats"
elif type_ == cls.EXPERIMENT:
path = "experiments"
elif type_ == cls.LIBRARY:
path = "libraries"
elif type_ == cls.PLOTTER:
path = "plotters"
elif type_ == cls.PLOTTERPARAMETER:
path = "plotterparameters"
elif type_ == cls.PROTOCOLTEMPLATE:
path = "protocoltemplates"
elif type_ == cls.TOOLCHAIN:
path = "toolchains"
return path
@classmethod
def from_path(cls, path):
if path == "algorithms":
return cls.ALGORITHM
elif path == "databases":
return cls.DATABASE
elif path == "dataformats":
return cls.DATAFORMAT
elif path == "experiments":
return cls.EXPERIMENT
elif path == "libraries":
return cls.LIBRARY
elif path == "plotters":
return cls.PLOTTER
elif path == "plotterparameters":
return cls.PLOTTERPARAMETER
elif path == "protocoltemplates":
return cls.PROTOCOLTEMPLATE
elif path == "toolchains":
return cls.TOOLCHAIN
else:
raise RuntimeError("Unknown asset path {}".format(path))
UNKNOWN = ("unknown", None)
ALGORITHM = ("algorithms", beat.core.algorithm.Algorithm)
DATABASE = ("databases", beat.core.database.Database)
DATAFORMAT = ("dataformats", beat.core.dataformat.DataFormat)
EXPERIMENT = ("experiments", beat.core.experiment.Experiment)
LIBRARY = ("libraries", beat.core.library.Library)
PLOTTER = ("plotters", beat.core.plotter.Plotter)
PLOTTERPARAMETER = (
"plotterparameters",
beat.core.plotterparameter.Plotterparameter,
)
PROTOCOLTEMPLATE = (
"protocoltemplates",
beat.core.protocoltemplate.ProtocolTemplate,
)
TOOLCHAIN = ("toolchains", beat.core.toolchain.Toolchain)
def __init__(self, path, klass):
self.path = path
self.klass = klass
@staticmethod
def from_path(path):
for asset_type in AssetType:
if asset_type.path == path:
return asset_type
raise RuntimeError("Unknown asset path {}".format(path))
def can_create(self):
"""Returns whether a new asset can be created from scratch"""
......@@ -325,7 +295,7 @@ class AssetModel(QStringListModel):
def asset_folder(self):
"""Returns the folder matching this model asset type"""
return os.path.join(self.prefix_path, AssetType.path(self.asset_type))
return os.path.join(self.prefix_path, self.asset_type.path)
def json_path(self, asset_name):
"""Returns the full path to the json file matching the asset given
......
......@@ -71,7 +71,7 @@ class TestAssetWidget:
"""Test that the AssetWidget works correctly"""
def get_asset_path(self, prefix, asset_type, asset_name):
return os.path.join(prefix, AssetType.path(asset_type), asset_name + ".json")
return os.path.join(prefix, asset_type.path, asset_name + ".json")
def test_matching_editor(
self, qtbot, test_prefix, beat_context, asset_type_prefix_entry_map
......
Markdown is supported
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