Commit 60413c85 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[backend] Add Asset class

This class will encapsulate the representation of an asset.

This will make it easier to move things around and get information
related to an asset.
parent 59fcfc89
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.editor module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
from .assetmodel import AssetType
class Asset:
"""Class encapsulating an asset"""
def __init__(self, prefix, asset_type, asset_name):
self.prefix = prefix
self.type = asset_type
self.name = asset_name
def __eq__(self, other):
"""Comparison operator"""
if isinstance(other, Asset):
return (
self.type == other.type
and self.name == other.name
and self.prefix == other.prefix
)
return False
def __repr__(self):
"""Representation"""
return f"{self.type}: {self.name}"
@staticmethod
def from_path(prefix, path):
"""Builds an asset based on a full path with the given prefix"""
asset_path = path[len(prefix) :] # noqa
asset_str = asset_path.split("/")[1]
asset_type = AssetType.from_path(asset_str)
asset_name = "/".join(asset_path.split("/")[2:]).split(".")[0]
return Asset(prefix, asset_type, asset_name)
@property
def declaration_path(self):
"""Returns the full path to the declaration file"""
if self.type == AssetType.UNKNOWN:
raise RuntimeError("Trying to get declaration of unknown type")
storage = self.type.storage(self.prefix, self.name)
return storage.json.path
@property
def documentation_path(self):
"""Returns the full path to the documentation file"""
if self.type == AssetType.UNKNOWN:
raise RuntimeError("Trying to get documentation of unknown type")
storage = self.type.storage(self.prefix, self.name)
return storage.doc.path
@property
def code_path(self):
"""Returns the full path to the code file"""
if self.type == AssetType.UNKNOWN:
raise RuntimeError("Trying to get code of unknown type")
if not self.type.has_code():
return None
storage = self.type.storage(self.prefix, self.name)
return storage.code.path
def delete(self):
"""Deletes the asset pointed to by this object"""
return self.type.delete(self.prefix, self.name)
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.editor module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
import os
import tempfile
import shutil
from ..backend.asset import Asset
from ..backend.assetmodel import AssetType
class TestAsset:
"""Test that the asset works correctly"""
existing_asset_name_map = {
AssetType.ALGORITHM: "autonomous/add/1",
AssetType.DATABASE: "simple/1",
AssetType.DATAFORMAT: "user/complexes/1",
AssetType.EXPERIMENT: "user/user/double/1/double",
AssetType.LIBRARY: "user/sum/1",
AssetType.PLOTTER: "user/scatter/1",
AssetType.PLOTTERPARAMETER: "plot/config/1",
AssetType.PROTOCOLTEMPLATE: "double/1",
AssetType.TOOLCHAIN: "user/double/1",
AssetType.UNKNOWN: None,
}
def test_from_path(self, test_prefix, asset_type):
asset_name = self.existing_asset_name_map[asset_type]
full_path = os.path.join(test_prefix, asset_type.path, asset_name)
asset = Asset.from_path(test_prefix, full_path)
assert asset.prefix == test_prefix
assert asset.type == asset_type
assert asset.name == asset_name
def test_declaration_path(self, test_prefix, asset_type):
asset_name = self.existing_asset_name_map[asset_type]
asset = Asset(test_prefix, asset_type, asset_name)
assert os.path.exists(asset.declaration_path)
def test_documentation_path(self, test_prefix, asset_type):
asset_name = self.existing_asset_name_map[asset_type]
asset = Asset(test_prefix, asset_type, asset_name)
assert os.path.exists(asset.documentation_path)
def test_code_path(self, test_prefix, asset_type):
asset_name = self.existing_asset_name_map[asset_type]
asset = Asset(test_prefix, asset_type, asset_name)
if asset_type.has_code():
assert os.path.exists(asset.code_path)
else:
assert asset.code_path is None
def test_delete(self, test_prefix, asset_type):
with tempfile.TemporaryDirectory(suffix=".prefix") as prefix:
tmp_prefix = os.path.join(prefix, "prefix")
shutil.copytree(test_prefix, tmp_prefix)
asset_name = self.existing_asset_name_map[asset_type]
asset = Asset(test_prefix, asset_type, asset_name)
result = asset.delete()
assert result
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