From 5fefb11ffd50dcda826cd5a256c8b3f650221050 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 23 Aug 2022 09:26:16 +0200 Subject: introduce package installation state enum --- src/hydrilla/proxy/state_impl/mappings.py | 178 ++++++++++++++++++++++-------- 1 file changed, 135 insertions(+), 43 deletions(-) (limited to 'src/hydrilla/proxy/state_impl/mappings.py') diff --git a/src/hydrilla/proxy/state_impl/mappings.py b/src/hydrilla/proxy/state_impl/mappings.py index cce2a36..e5b324d 100644 --- a/src/hydrilla/proxy/state_impl/mappings.py +++ b/src/hydrilla/proxy/state_impl/mappings.py @@ -41,29 +41,28 @@ from . import base @dc.dataclass(frozen=True, unsafe_hash=True) -class ConcreteMappingVersionRef(st.MappingVersionRef): - """....""" +class ConcreteMappingRef(st.MappingRef): state: base.HaketiloStateWithFields = dc.field(hash=False, compare=False) - def update_status(self, new_status: st.EnabledStatus) -> None: - """....""" - assert new_status != st.EnabledStatus.AUTO_ENABLED - raise NotImplementedError() - - def get_display_info(self) -> st.MappingDisplayInfo: + def get_version_display_infos(self) \ + -> t.Sequence[st.MappingVersionDisplayInfo]: with self.state.cursor() as cursor: cursor.execute( ''' SELECT - enabled, + item_version_id, definition, repo, repo_iteration, - is_orphan + installed, + is_active, + is_orphan, + is_local, + enabled FROM mapping_display_infos WHERE - item_version_id = ?; + item_id = ?; ''', (self.id,) ) @@ -73,61 +72,154 @@ class ConcreteMappingVersionRef(st.MappingVersionRef): if rows == []: raise st.MissingItemError() - (status_letter, definition, repo, repo_iteration, is_orphan), = rows + result = [] - item_info = item_infos.MappingInfo.load( - definition, - repo, - repo_iteration - ) + for (item_version_id, definition, repo, repo_iteration, installed, + is_active, is_orphan, is_local, status_letter) in rows: + ref = ConcreteMappingVersionRef(str(item_version_id), self.state) - status = st.EnabledStatus(status_letter) + item_info = item_infos.MappingInfo.load( + definition, + repo, + repo_iteration + ) - return st.MappingDisplayInfo(self, item_info, status, is_orphan) + display_info = st.MappingVersionDisplayInfo( + ref = ref, + info = item_info, + installed = st.InstalledStatus(installed), + is_active = is_active, + is_orphan = is_orphan, + is_local = is_local, + mapping_enabled = st.EnabledStatus(status_letter) + ) + result.append(display_info) + + return sorted(result, key=(lambda di: di.info)) @dc.dataclass(frozen=True) -class ConcreteMappingVersionStore(st.MappingVersionStore): +class ConcreteMappingStore(st.MappingStore): state: base.HaketiloStateWithFields - def get(self, id: str) -> st.MappingVersionRef: - return ConcreteMappingVersionRef(id, self.state) + def get(self, id: str) -> st.MappingRef: + return ConcreteMappingRef(id, self.state) - def get_display_infos(self) -> t.Iterable[st.MappingDisplayInfo]: + def get_display_infos(self) -> t.Sequence[st.MappingDisplayInfo]: + with self.state.cursor() as cursor: + cursor.execute( + ''' + SELECT DISTINCT + item_id, + identifier, + CASE WHEN enabled IN ('A', 'E') THEN item_version_id + ELSE NULL END, + CASE WHEN enabled IN ('A', 'E') THEN definition + ELSE NULL END, + CASE WHEN enabled IN ('A', 'E') THEN repo + ELSE NULL END, + CASE WHEN enabled IN ('A', 'E') THEN repo_iteration + ELSE NULL END, + enabled + FROM + mapping_display_infos + WHERE + is_active OR item_version_id IS NULL; + ''' + ) + + rows = cursor.fetchall() + + result = [] + + for (item_id, identifier, item_version_id, definition, repo, + repo_iteration, status_letter) in rows: + ref = ConcreteMappingRef(str(item_id), self.state) + + version_ref: t.Optional[st.MappingVersionRef] = None + item_info: t.Optional[item_infos.MappingInfo] = None + + if item_version_id is not None: + active_version_ref = ConcreteMappingVersionRef( + id = str(item_version_id), + state = self.state + ) + item_info = item_infos.MappingInfo.load( + definition, + repo, + repo_iteration + ) + + display_info = st.MappingDisplayInfo( + ref = ref, + identifier = identifier, + enabled = st.EnabledStatus(status_letter), + active_version_ref = active_version_ref, + active_version_info = item_info + ) + + result.append(display_info) + + return result + +@dc.dataclass(frozen=True, unsafe_hash=True) +class ConcreteMappingVersionRef(st.MappingVersionRef): + state: base.HaketiloStateWithFields = dc.field(hash=False, compare=False) + + def update_status(self, new_status: st.EnabledStatus) -> None: + """....""" + assert new_status != st.EnabledStatus.AUTO_ENABLED + raise NotImplementedError() + + def get_display_info(self) -> st.MappingVersionDisplayInfo: with self.state.cursor() as cursor: cursor.execute( ''' SELECT - enabled, - item_version_id, definition, repo, repo_iteration, - is_orphan + installed, + is_orphan, + is_active, + is_local, + enabled FROM - mapping_display_infos; - ''' + mapping_display_infos + WHERE + item_version_id = ?; + ''', + (self.id,) ) - all_rows = cursor.fetchall() + rows = cursor.fetchall() - result = [] + if rows == []: + raise st.MissingItemError() - for row in all_rows: - (status_letter, item_version_id, definition, repo, repo_iteration, - is_orphan) = row + (definition, repo, repo_iteration, installed, is_orphan, is_active, + is_local, status_letter), = rows - ref = ConcreteMappingVersionRef(str(item_version_id), self.state) + item_info = item_infos.MappingInfo.load( + definition, + repo, + repo_iteration + ) - item_info = item_infos.MappingInfo.load( - definition, - repo, - repo_iteration - ) + return st.MappingVersionDisplayInfo( + ref = self, + info = item_info, + installed = st.InstalledStatus(installed), + is_active = is_active, + is_orphan = is_orphan, + is_local = is_local, + mapping_enabled = st.EnabledStatus(status_letter) + ) - status = st.EnabledStatus(status_letter) - info = st.MappingDisplayInfo(ref, item_info, status, is_orphan) - result.append(info) +@dc.dataclass(frozen=True) +class ConcreteMappingVersionStore(st.MappingVersionStore): + state: base.HaketiloStateWithFields - return sorted(result, key=(lambda di: di.info)) + def get(self, id: str) -> st.MappingVersionRef: + return ConcreteMappingVersionRef(id, self.state) -- cgit v1.2.3