diff options
Diffstat (limited to 'src/hydrilla/item_infos.py')
-rw-r--r-- | src/hydrilla/item_infos.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/hydrilla/item_infos.py b/src/hydrilla/item_infos.py index 9a87b40..0e10871 100644 --- a/src/hydrilla/item_infos.py +++ b/src/hydrilla/item_infos.py @@ -484,3 +484,69 @@ class CategorizedItemInfo(Categorizable, t.Generic[CategorizedType, CategoryKeyT def is_empty(self) -> bool: """....""" return len(self.items) == 0 + + +VersionedType = t.TypeVar('VersionedType', ResourceInfo, MappingInfo) + +class VersionedItemInfo( + CategorizedItemInfo[VersionedType, versions.VerTuple], + t.Generic[VersionedType] +): + """Stores data of multiple versions of given resource/mapping.""" + SelfType = t.TypeVar('SelfType', bound='VersionedItemInfo[VersionedType]') + + def register(self: 'SelfType', item_info: VersionedType) -> 'SelfType': + """ + Make item info queryable by version. Perform sanity checks for uuid. + """ + return self._update(item_info.version, lambda old_info: item_info) + + def unregister(self: 'SelfType', version: versions.VerTuple) -> 'SelfType': + """....""" + return self._update(version, lambda old_info: None) + + @property + def newest_version(self) -> versions.VerTuple: + """....""" + assert not self.is_empty() + + return max(self.items.keys()) + + @property + def newest_info(self) -> VersionedType: + """Find and return info of the newest version of item.""" + return self.items[self.newest_version] + + def get_by_ver(self, ver: t.Sequence[int]) -> t.Optional[VersionedType]: + """ + Find and return info of the specified version of the item (or None if + absent). + """ + return self.items.get(versions.normalize(ver)) + + def get_all(self) -> t.Iterable[VersionedType]: + """Generate item info for all its versions, from oldest to newest.""" + return [self.items[version] for version in sorted(self.items.keys())] + +VersionedResourceInfo = VersionedItemInfo[ResourceInfo] +VersionedMappingInfo = VersionedItemInfo[MappingInfo] + + +VersionedItemInfoMap = Map[str, VersionedItemInfo] +VersionedResourceInfoMap = Map[str, VersionedResourceInfo] +VersionedMappingInfoMap = Map[str, VersionedMappingInfo] + +def register_in_map( + map: Map[str, VersionedItemInfo[VersionedType]], + info: VersionedType +) -> Map[str, VersionedItemInfo[VersionedType]]: + versioned_info = map.get(info.identifier, VersionedItemInfo()) + + return map.set(info.identifier, versioned_info.register(info)) + +def all_map_infos( + map: Map[str, VersionedItemInfo[VersionedType]] +) -> t.Iterator[VersionedType]: + for versioned_info in map.values(): + for item_info in versioned_info.get_all(): + yield item_info |