assetmodel.py 10.1 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

Samuel GAIST's avatar
Samuel GAIST committed
28
29
from PyQt5.QtCore import QStringListModel
from PyQt5.QtCore import pyqtProperty
30
31
32
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import pyqtSlot

33
from ..decorators import frozen
34
from ..utils import dataformat_basetypes
35
from .asset import Asset
Samuel GAIST's avatar
Samuel GAIST committed
36
from .asset import AssetType
37

38

39
def enumerate_assets(prefix_path, asset_type, latest_only=False):
Samuel GAIST's avatar
Samuel GAIST committed
40
    """Enumerate the assets available in the given prefix for the given type"""
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

    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

    assets_list = []
    asset_folder = os.path.join(prefix_path, asset_type.path)

    if asset_type in [AssetType.DATABASE, AssetType.PROTOCOLTEMPLATE]:
        # These assets have no user associated with them
        asset_folders = [entry for entry in os.scandir(asset_folder) if entry.is_dir()]
        for asset_folder in asset_folders:
            json_files = _find_json_files(asset_folder)
            if json_files:
                if latest_only:
                    json_files = json_files[-1:]

                for json_file in json_files:
                    assets_list.append(
                        "{name}/{version}".format(
                            name=asset_folder.name, version=json_file.split(".")[0]
                        )
                    )
    else:
        # Assets belonging to a user
        asset_users = [entry for entry in os.scandir(asset_folder) if entry.is_dir()]

        for asset_user in asset_users:
            asset_folders = [
                entry for entry in os.scandir(asset_user) if entry.is_dir()
            ]
            for asset_folder in asset_folders:
                if 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")
                        ]:
                            assets_list.append(
                                "{experiment_path}/{name}".format(
                                    experiment_path=experiment_path,
                                    name=json_file.split(".")[0],
                                )
                            )
                else:
                    json_files = _find_json_files(asset_folder)
                    if json_files:
                        if 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],
                                )
                            )

    return sorted(assets_list)


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

    :param prefix_path str: path of the prefix
    :param asset_type AssetType: asset type to search in
    :param asset_name str: fully qualified asset name
    """

    asset = Asset(prefix_path, asset_type, asset_name)
    if not os.path.exists(asset.declaration_path):
        raise ValueError("Invalid asset {}".format(asset_name))
    return asset.declaration_path


class AbstractAssetModel(QStringListModel):
    """Base class for asset related models"""
132
133
134
135
136
137

    prefixPathChanged = pyqtSignal(str)

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

138
        super().__init__(parent)
139

140
141
142
143
144
145
146
147
148
149
150
        self._latest_only = True
        self._prefix_path = None

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

        raise NotImplementedError

    def lastestOnlyEnabled(self):
        return self._latest_only
151

152
    def setLatestOnlyEnabled(self, enabled):
153
        if self._latest_only == enabled:
154
            return
155
156

        self._latest_only = enabled
157
158
        self.reload()

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    latest_only_enabled = pyqtProperty(
        str, fget=lastestOnlyEnabled, fset=setLatestOnlyEnabled
    )

    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
    )

    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
        """

        raise NotImplementedError

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

200
        raise NotImplementedError
201

202

203
204
205
@frozen
class AssetModel(AbstractAssetModel):
    """The asset model present a list of available asset from a given type"""
206

207
    assetTypeChanged = pyqtSignal(AssetType)
208

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

212
213
214
        super().__init__(parent)

        self._asset_type = AssetType.UNKNOWN
215

216
217
218
    @property
    def asset_folder(self):
        """Reimpl"""
219

220
        return os.path.join(self.prefix_path, self.asset_type.path)
221
222
223
224
225
226
227

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

        :return: Asset type of this model
        """

228
        return self._asset_type
229
230
231
232
233
234
235

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

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

236
        if self._asset_type == type_:
237
238
            return

239
        self._asset_type = type_
240
241
242
243
244
245
246
        self.reload()
        self.assetTypeChanged.emit(type_)

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

247
248
    def json_path(self, asset_name):
        """Reimpl"""
249

250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
        return json_path(self.prefix_path, self.asset_type, asset_name)

    @pyqtSlot()
    def reload(self):
        """Reimpl"""

        if not self.prefix_path or self._asset_type == AssetType.UNKNOWN:
            return

        self.setStringList(
            enumerate_assets(
                self.prefix_path, self.asset_type, self.latest_only_enabled
            )
        )


@frozen
class DataFormatModel(AbstractAssetModel):
    """DataFormat specific asset model"""

    def __init__(self, parent=None):
        super().__init__(parent)

        self._full_list_enabled = False
        self.__asset_type = AssetType.DATAFORMAT

    @property
    def asset_folder(self):
        """Reimpl"""

        return os.path.join(self.prefix_path, self.__asset_type.path)

282
283
284
285
286
287
288
289
    def defaultFormat(self):
        """Returns a format used to setup a default value

        :returns: the default format
        """

        return self.index(0).data()

290
291
    def setFullListEnabled(self, enabled):
        """Enable/disable the full list mode
292

293
        :param bool enabled: enable the full list mode
294
295
        """

296
        if self._full_list_enabled == enabled:
297
298
            return

299
        self._full_list_enabled = enabled
300
301
        self.reload()

302
303
    def json_path(self, asset_name):
        """Reimpl"""
304

305
        return json_path(self.prefix_path, self.__asset_type, asset_name)
306

307
308
309
    @pyqtSlot()
    def reload(self):
        """Reimpl"""
310

311
312
313
314
315
316
317
318
        if not self.prefix_path:
            return

        assets = enumerate_assets(
            self.prefix_path, self.__asset_type, self.latest_only_enabled
        )
        if self._full_list_enabled:
            assets = dataformat_basetypes() + assets
319

320
        self.setStringList(assets)