assetmodel.py 7.87 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
from enum import Enum, unique
29

30
31
32
33
34
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import pyqtProperty
from PyQt5.QtCore import QStringListModel

35
36
from beat.core.schema import validate

37
38
from ..utils import dataformat_basetypes

39

40
@unique
41
class AssetType(Enum):
42
43
    """All possible assets available on the BEAT platform"""

44
    (
45
46
47
48
49
50
51
52
        UNKNOWN,
        ALGORITHM,
        DATABASE,
        DATAFORMAT,
        EXPERIMENT,
        LIBRARY,
        PLOTTER,
        PLOTTERPARAMETERS,
53
        PROTOCOLTEMPLATE,
54
        TOOLCHAIN,
55
    ) = range(10)
56
57
58

    @classmethod
    def path(cls, type_):
59
        path = "unknown"
60
        if type_ == cls.ALGORITHM:
61
            path = "algorithms"
62
        elif type_ == cls.DATABASE:
63
            path = "databases"
64
        elif type_ == cls.DATAFORMAT:
65
            path = "dataformats"
66
        elif type_ == cls.EXPERIMENT:
67
            path = "experiments"
68
        elif type_ == cls.LIBRARY:
69
            path = "libraries"
70
        elif type_ == cls.PLOTTER:
71
            path = "plotters"
72
        elif type_ == cls.PLOTTERPARAMETERS:
73
            path = "plotterparameters"
74
75
        elif type_ == cls.PROTOCOLTEMPLATE:
            path = "protocoltemplates"
76
        elif type_ == cls.TOOLCHAIN:
77
78
79
80
            path = "toolchains"

        return path

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    @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.PLOTTERPARAMETERS
97
98
        elif path == "protocoltemplates":
            return cls.PROTOCOLTEMPLATE
99
100
101
102
103
        elif path == "toolchains":
            return cls.TOOLCHAIN
        else:
            raise RuntimeError("Unknown asset path {}".format(path))

104
105
106
107
108
    def validate(self, data):
        if self == self.UNKNOWN:
            raise RuntimeError("Trying to validate unknown type")
        return validate(self.name.lower(), data)

109

110
class AssetModel(QStringListModel):
111
112
113
114
115
116
117
118
119
120
    """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)

121
        self.__prefix_path = None
122
        self.__asset_type = AssetType.UNKNOWN
123
124
125
126
127

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

128
        if not self.__prefix_path or self.__asset_type == AssetType.UNKNOWN:
129
130
131
132
133
134
135
136
137
138
            return

        asset_folder = os.path.join(
            self.__prefix_path, AssetType.path(self.__asset_type)
        )
        asset_users = os.scandir(asset_folder)

        latest_assets_list = []
        for asset_user in asset_users:
            for asset_folder in os.scandir(asset_user):
139
                if self.asset_type == AssetType.EXPERIMENT:
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
                    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")
                        ]:
                            latest_assets_list.append(
                                "{experiment_path}/{name}".format(
                                    experiment_path=experiment_path,
                                    name=json_file.split(".")[0],
                                )
                            )
                else:
                    asset_items = os.scandir(asset_folder)
                    json_files = sorted(
                        [
                            item.name
                            for item in asset_items
                            if item.is_file() and item.name.endswith("json")
                        ]
                    )
                    if json_files:
                        latest_assets_list.append(
                            "{user}/{name}/{version}".format(
                                user=asset_user.name,
                                name=asset_folder.name,
                                version=json_files[-1].split(".")[0],
                            )
                        )

173
174
        latest_assets_list = sorted(latest_assets_list)
        if self.asset_type == AssetType.DATAFORMAT:
175
            latest_assets_list = dataformat_basetypes() + latest_assets_list
176
177

        self.setStringList(latest_assets_list)
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

    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
    )