assetmodel.py 11.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

28
29
import beat.core

30
from enum import Enum, unique
31

32
33
34
35
36
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import pyqtProperty
from PyQt5.QtCore import QStringListModel

37
from beat.core.schema import validate
38
from beat.cmdline import common
39

40
41
from ..utils import dataformat_basetypes

42

43
@unique
44
class AssetType(Enum):
45
46
    """All possible assets available on the BEAT platform"""

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    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

68
69
70
71
72
    @property
    def storage(self):
        mod = __import__(self.klass.__module__, fromlist=["Storage"])
        return getattr(mod, "Storage")

73
74
75
76
77
78
    @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))
79

80
81
82
83
84
    def can_create(self):
        """Returns whether a new asset can be created from scratch"""

        return self not in [self.UNKNOWN, self.EXPERIMENT]

85
86
87
88
89
    def can_fork(self):
        """Returns whether a new asset can be forked"""

        return self not in [self.UNKNOWN, self.DATABASE, self.PROTOCOLTEMPLATE]

90
91
92
93
94
95
96
97
98
99
100
101
    def split_count(self):
        """Returns the number of "/" that should be part of its name"""

        if self == self.UNKNOWN:
            return 0
        elif self == self.EXPERIMENT:
            return 5
        elif self not in [self.DATABASE, self.PROTOCOLTEMPLATE]:
            return 2
        else:
            return 1

102
    def validate(self, data):
103
104
105
106
107
        """Runs the schema validation and returns whether an asset is valid

        :param data str: asset content
        """

108
109
110
111
        if self == self.UNKNOWN:
            raise RuntimeError("Trying to validate unknown type")
        return validate(self.name.lower(), data)

112
113
114
115
116
117
118
119
120
121
122
123
    def create_new(self, prefix, name):
        """Create a new asset from a prototype

        :param prefix str: Path to the prefix
        :param name str: name of the asset
        """

        if self == self.UNKNOWN:
            raise RuntimeError("Trying to create an asset of unknown type")
        success = common.create(prefix, self.name.lower(), [name])
        return success == 0

124
125
126
127
128
129
130
131
132
133
134
135
136
137
    def create_new_version(self, prefix, name):
        """Create a new version of the asset

        :param prefix str: Path to the prefix
        :param name str: name of the asset
        """

        if self == self.UNKNOWN:
            raise RuntimeError(
                "Trying to create a new version of an asset of unknown type"
            )
        success = common.new_version(prefix, self.name.lower(), name)
        return success == 0

138
139
140
141
142
143
144
145
146
147
148
149
150
    def fork(self, prefix, source, destination):
        """Fork an asset

        :param prefix str: Path to the prefix
        :param source str: name of the original asset
        :param destination str: name of the new asset
        """

        if self == self.UNKNOWN:
            raise RuntimeError("Trying to fork an asset of unknown type")
        success = common.fork(prefix, self.name.lower(), source, destination)
        return success == 0

151
152
153
154
155
156
157
158
159
160
161
162
    def delete(self, prefix, name):
        """Delete an asset

        :param prefix str: Path to the prefix
        :param name str: name of the asset to delete
        """

        if self == self.UNKNOWN:
            raise RuntimeError("Trying to delete an asset of unknown type")
        success = common.delete_local(prefix, self.name.lower(), [name])
        return success == 0

163
    def declaration_path(self, prefix, name):
164
165
166
167
168
169
        """Returns the full path to the declaration file

        :param prefix str: Path to the prefix
        :param name str: name of the asset to delete
        """

170
171
172
        asset_folder = os.path.join(prefix, self.path)
        return os.path.join(asset_folder, "{}.json".format(name))

173

174
class AssetModel(QStringListModel):
175
176
177
178
179
180
181
182
183
184
    """The asset model present a list of available asset from a given type"""

    assetTypeChanged = pyqtSignal(AssetType)
    prefixPathChanged = pyqtSignal(str)

    def __init__(self, parent=None):
        """Constructor"""

        super(QStringListModel, self).__init__(parent)

185
        self.__latest_only = True
186
        self.__prefix_path = None
187
        self.__asset_type = AssetType.UNKNOWN
188

189
190
191
192
193
194
    def setLatestOnlyEnabled(self, enabled):
        if self.__latest_only == enabled:
            return
        self.__latest_only = enabled
        self.reload()

195
196
197
198
    @pyqtSlot()
    def reload(self):
        """Loads the content regarding the asset property from the prefix"""

199
        if not self.__prefix_path or self.__asset_type == AssetType.UNKNOWN:
200
201
            return

202
203
204
205
206
207
208
209
210
211
212
213
        def _find_json_files(path):
            """Return all json files from folder sorted"""

            asset_items = os.scandir(path)
            json_files = sorted(
                [
                    item.name
                    for item in asset_items
                    if item.is_file() and item.name.endswith("json")
                ]
            )
            return json_files
214

215
        assets_list = []
216
217
218
219
220
221

        if self.asset_type in [AssetType.DATABASE, AssetType.PROTOCOLTEMPLATE]:
            # These assets have no user associated with them
            for asset_folder in os.scandir(self.asset_folder):
                json_files = _find_json_files(asset_folder)
                if json_files:
222
                    if self.__latest_only:
223
224
225
226
                        json_files = json_files[-1:]

                    for json_file in json_files:
                        assets_list.append(
227
                            "{name}/{version}".format(
228
                                name=asset_folder.name, version=json_file.split(".")[0]
229
                            )
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
                        )
        else:
            # Assets belonging to a user
            asset_users = os.scandir(self.asset_folder)

            for asset_user in asset_users:
                for asset_folder in os.scandir(asset_user):
                    if self.asset_type == AssetType.EXPERIMENT:
                        for root, dirs, files in os.walk(asset_folder, topdown=False):
                            if dirs:
                                continue
                            anchor = "experiments/"
                            position = root.index(anchor) + len(anchor)
                            experiment_path = root[position:]
                            for json_file in [
                                file for file in files if file.endswith("json")
                            ]:
247
                                assets_list.append(
248
249
250
251
252
253
254
255
                                    "{experiment_path}/{name}".format(
                                        experiment_path=experiment_path,
                                        name=json_file.split(".")[0],
                                    )
                                )
                    else:
                        json_files = _find_json_files(asset_folder)
                        if json_files:
256
257
258
259
260
261
262
263
264
265
                            if self.__latest_only:
                                json_files = json_files[-1:]

                            for json_file in json_files:
                                assets_list.append(
                                    "{user}/{name}/{version}".format(
                                        user=asset_user.name,
                                        name=asset_folder.name,
                                        version=json_file.split(".")[0],
                                    )
266
267
                                )

268
        assets_list = sorted(assets_list)
269
        if self.asset_type == AssetType.DATAFORMAT:
270
            assets_list = dataformat_basetypes() + assets_list
271

272
        self.setStringList(assets_list)
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322

    def assetType(self):
        """Returns the asset type of this model

        :return: Asset type of this model
        """

        return self.__asset_type

    def setAssetType(self, type_):
        """Set the asset type of this model

        :param AssetType type_: Asset type this model should show
        """

        if self.__asset_type == type_:
            return

        self.__asset_type = type_
        self.reload()
        self.assetTypeChanged.emit(type_)

    asset_type = pyqtProperty(
        AssetType, fget=assetType, fset=setAssetType, notify=assetTypeChanged
    )

    def prefixPath(self):
        """Returns the prefix path use by this model

        :return: the prefix path used
        """

        return self.__prefix_path

    def setPrefixPath(self, path):
        """Set the prefix path use by this model

        :param str path: Path to prefix
        """

        if self.__prefix_path == path:
            return

        self.__prefix_path = path
        self.reload()
        self.prefixPathChanged.emit(path)

    prefix_path = pyqtProperty(
        str, fget=prefixPath, fset=setPrefixPath, notify=prefixPathChanged
    )
323
324
325
326
327

    @property
    def asset_folder(self):
        """Returns the folder matching this model asset type"""

328
        return os.path.join(self.prefix_path, self.asset_type.path)
329
330
331
332
333
334
335

    def json_path(self, asset_name):
        """Returns the full path to the json file matching the asset given

        :param asset_name str: fully qualified asset name
        """

336
        asset_path = self.asset_type.declaration_path(self.prefix_path, asset_name)
337
338
339
        if not os.path.exists(asset_path):
            raise RuntimeError("Invalid asset {}".format(asset_name))
        return asset_path