aboutsummaryrefslogtreecommitdiff
path: root/src/hydrilla/item_infos.py
blob: c366ab59cf5fbc6e07a130f81b5ab4999e92d754 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
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
132
133
134
135
136
137
138
139
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
173
174
175
176
177
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# SPDX-License-Identifier: GPL-3.0-or-later

# Reading resources, mappings and other JSON documents from the filesystem.
#
# This file is part of Hydrilla&Haketilo
#
# Copyright (C) 2021, 2022 Wojtek Kosior
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
#
# I, Wojtek Kosior, thereby promise not to sue for violation of this
# file's license. Although I request that you do not make use this code
# in a proprietary program, I am not going to enforce this in court.

"""
.....
"""

# Enable using with Python 3.7.
from __future__ import annotations

import typing as t
import dataclasses as dc

from pathlib import Path, PurePath

from immutables import Map, MapMutation

from . import versions, json_instances
from .url_patterns import parse_pattern, ParsedUrl
from .exceptions import HaketiloException
from .translations import smart_gettext as _

VerTuple = t.Tuple[int, ...]

@dc.dataclass(frozen=True, unsafe_hash=True)
class ItemRef:
    """...."""
    identifier: str

RefObjs = t.Sequence[t.Mapping[str, t.Any]]

def make_item_refs_seq(ref_objs: RefObjs) -> tuple[ItemRef, ...]:
    """...."""
    return tuple(ItemRef(ref['identifier']) for ref in ref_objs)

def make_required_mappings(refs_objs: t.Any, schema_compat: int) \
    -> tuple[ItemRef, ...]:
    """...."""
    if schema_compat < 2:
        return ()

    return make_item_refs_seq(refs_objs)

@dc.dataclass(frozen=True, unsafe_hash=True)
class FileRef:
    """...."""
    name:   str
    sha256: str

def make_file_refs_seq(ref_objs: RefObjs) -> tuple[FileRef, ...]:
    """...."""
    return tuple(FileRef(ref['file'], ref['sha256']) for ref in ref_objs)

@dc.dataclass(frozen=True, unsafe_hash=True)
class GeneratedBy:
    """...."""
    name:    str
    version: t.Optional[str]

    @staticmethod
    def make(generated_obj: t.Optional[t.Mapping[str, t.Any]]) -> \
        t.Optional['GeneratedBy']:
        """...."""
        if generated_obj is None:
            return None

        return GeneratedBy(
            name    = generated_obj['name'],
            version = generated_obj.get('version')
        )

@dc.dataclass(frozen=True, unsafe_hash=True)
class ItemInfoBase:
    """...."""
    repository:        str                     # repository used in __hash__()
    source_name:       str                     = dc.field(hash=False)
    source_copyright:  tuple[FileRef, ...]     = dc.field(hash=False)
    version:           VerTuple                # version used in __hash__()
    identifier:        str                     # identifier used in __hash__()
    uuid:              t.Optional[str]         = dc.field(hash=False)
    long_name:         str                     = dc.field(hash=False)
    required_mappings: tuple[ItemRef, ...]     = dc.field(hash=False)
    generated_by:      t.Optional[GeneratedBy] = dc.field(hash=False)

    def path_relative_to_type(self) -> str:
        """
        Get a relative path to this item's JSON definition with respect to
        directory containing items of this type.
        """
        return f'{self.identifier}/{versions.version_string(self.version)}'

    def path(self) -> str:
        """
        Get a relative path to this item's JSON definition with respect to
        malcontent directory containing loadable items.
        """
        return f'{self.type_name}/{self.path_relative_to_type()}'

    @property
    def versioned_identifier(self):
        """...."""
        return f'{self.identifier}-{versions.version_string(self.version)}'

    @staticmethod
    def _get_base_init_kwargs(
            item_obj:      t.Mapping[str, t.Any],
            schema_compat: int,
            repository:    str
    ) -> t.Mapping[str, t.Any]:
        """...."""
        source_copyright = make_file_refs_seq(item_obj['source_copyright'])

        version = versions.normalize_version(item_obj['version'])

        required_mappings = make_required_mappings(
            item_obj.get('required_mappings', []),
            schema_compat
        )

        generated_by = GeneratedBy.make(item_obj.get('generated_by'))

        return Map(
            repository          = repository,
            source_name         = item_obj['source_name'],
            source_copyright    = source_copyright,
            version             = version,
            identifier          = item_obj['identifier'],
            uuid                = item_obj.get('uuid'),
            long_name           = item_obj['long_name'],
            required_mappings   = required_mappings,
            generated_by        = generated_by
        )

    # class property
    type_name = '!INVALID!'

InstanceOrPath = t.Union[Path, str, dict[str, t.Any]]

@dc.dataclass(frozen=True, unsafe_hash=True)
class ResourceInfo(ItemInfoBase):
    """...."""
    revision:     int                 = dc.field(hash=False)
    dependencies: tuple[ItemRef, ...] = dc.field(hash=False)
    scripts:      tuple[FileRef, ...] = dc.field(hash=False)

    @property
    def versioned_identifier(self):
        """...."""
        return f'{super().versioned_identifier()}-{self.revision}'

    @staticmethod
    def make(
            item_obj:      t.Mapping[str, t.Any],
            schema_compat: int,
            repository:    str
    ) -> 'ResourceInfo':
        """...."""
        base_init_kwargs = ItemInfoBase._get_base_init_kwargs(
            item_obj,
            schema_compat,
            repository
        )

        return ResourceInfo(
            **base_init_kwargs,

            revision     = item_obj['revision'],
            dependencies = make_item_refs_seq(item_obj.get('dependencies', [])),
            scripts      = make_file_refs_seq(item_obj.get('scripts', [])),
        )

    @staticmethod
    def load(instance_or_path: 'InstanceOrPath', repository: str) \
        -> 'ResourceInfo':
        """...."""
        return _load_item_info(ResourceInfo, instance_or_path, repository)

    # class property
    type_name = 'resource'

def make_payloads(payloads_obj: t.Mapping[str, t.Any]) \
    -> t.Mapping[ParsedUrl, ItemRef]:
    """...."""
    mapping: list[tuple[ParsedUrl, ItemRef]] = []

    for pattern, ref_obj in payloads_obj.items():
        ref = ItemRef(ref_obj['identifier'])
        mapping.extend((parsed, ref) for parsed in parse_pattern(pattern))

    return Map(mapping)

@dc.dataclass(frozen=True, unsafe_hash=True)
class MappingInfo(ItemInfoBase):
    """...."""
    payloads: t.Mapping[ParsedUrl, ItemRef] = dc.field(hash=False)

    @staticmethod
    def make(
            item_obj:      t.Mapping[str, t.Any],
            schema_compat: int,
            repository:    str
    ) -> 'MappingInfo':
        """...."""
        base_init_kwargs = ItemInfoBase._get_base_init_kwargs(
            item_obj,
            schema_compat,
            repository
        )

        return MappingInfo(
            **base_init_kwargs,

            payloads = make_payloads(item_obj.get('payloads', {}))
        )

    @staticmethod
    def load(instance_or_path: 'InstanceOrPath', repository: str) \
        -> 'MappingInfo':
        """...."""
        return _load_item_info(MappingInfo, instance_or_path, repository)

    # class property
    type_name = 'mapping'


LoadedType = t.TypeVar('LoadedType', ResourceInfo, MappingInfo)

def _load_item_info(
        info_type:        t.Type[LoadedType],
        instance_or_path: InstanceOrPath,
        repository:       str
) -> LoadedType:
    """Read, validate and autocomplete a mapping/resource description."""
    instance = json_instances.read_instance(instance_or_path)

    schema_fmt = f'api_{info_type.type_name}_description-{{}}.schema.json'

    schema_compat = json_instances.validate_instance(instance, schema_fmt)

    # We know from successful validation that instance is a dict.
    return info_type.make(
        t.cast('dict[str, t.Any]', instance),
        schema_compat,
        repository
    )


VersionedType = t.TypeVar('VersionedType', ResourceInfo, MappingInfo)

@dc.dataclass(frozen=True)
class VersionedItemInfo(t.Generic[VersionedType]):
    """Stores data of multiple versions of given resource/mapping."""
    uuid:         t.Optional[str]              = None
    identifier:   str                          = '<dummy>'
    _by_version:  Map[VerTuple, VersionedType] = Map()
    _initialized: bool                         = False

    def register(self, item_info: VersionedType) -> 'VersionedInfoSelfType':
        """
        Make item info queryable by version. Perform sanity checks for uuid.
        """
        identifier = item_info.identifier
        if self._initialized:
            assert identifier == self.identifier

        if self.uuid is not None:
            uuid: t.Optional[str] = self.uuid
            if item_info.uuid is not None and self.uuid != item_info.uuid:
                raise HaketiloException(_('uuid_mismatch_{identifier}')
                                        .format(identifier=identifier))
        else:
            uuid = item_info.uuid

        by_version = self._by_version.set(item_info.version, item_info)

        return VersionedItemInfo(
            identifier   = identifier,
            uuid         = uuid,
            _by_version  = by_version,
            _initialized = True
        )

    def unregister(self, version: VerTuple) -> 'VersionedInfoSelfType':
        """...."""
        try:
            by_version = self._by_version.delete(version)
        except KeyError:
            by_version = self._by_version

        return dc.replace(self, _by_version=by_version)

    def is_empty(self) -> bool:
        """...."""
        return len(self._by_version) == 0

    def newest_version(self) -> VerTuple:
        """...."""
        assert not self.is_empty()

        return max(self._by_version.keys())

    def get_newest(self) -> VersionedType:
        """Find and return info of the newest version of item."""
        newest = self._by_version[self.newest_version()]
        assert newest is not None
        return newest

    def get_by_ver(self, ver: t.Iterable[int]) -> t.Optional[VersionedType]:
        """
        Find and return info of the specified version of the item (or None if
        absent).
        """
        return self._by_version.get(tuple(ver))

    def get_all(self) -> t.Iterator[VersionedType]:
        """Generate item info for all its versions, from oldest ot newest."""
        for version in sorted(self._by_version.keys()):
            yield self._by_version[version]

# Below we define 1 type used by recursively-typed VersionedItemInfo.
VersionedInfoSelfType = VersionedItemInfo[VersionedType]