aboutsummaryrefslogtreecommitdiff
path: root/src/hydrilla/proxy/state_impl
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2022-09-02 13:51:25 +0200
committerWojtek Kosior <koszko@koszko.org>2022-09-28 12:54:55 +0200
commit5cdb4c29ea9e736ea5a52483c2e62009c4c7d27e (patch)
tree0ccdec4cff3fb7b507a085a81322d2cfce1288d8 /src/hydrilla/proxy/state_impl
parentb533fa5c5bdd88fa1ea4980ff0c1828d692e5319 (diff)
downloadhaketilo-hydrilla-5cdb4c29ea9e736ea5a52483c2e62009c4c7d27e.tar.gz
haketilo-hydrilla-5cdb4c29ea9e736ea5a52483c2e62009c4c7d27e.zip
[proxy] allow packages to be frozen and unfrozen in the web UI
This commit also makes some minor changes and fixes in other parts of the web UI.
Diffstat (limited to 'src/hydrilla/proxy/state_impl')
-rw-r--r--src/hydrilla/proxy/state_impl/items.py230
1 files changed, 120 insertions, 110 deletions
diff --git a/src/hydrilla/proxy/state_impl/items.py b/src/hydrilla/proxy/state_impl/items.py
index 919223b..5f2f274 100644
--- a/src/hydrilla/proxy/state_impl/items.py
+++ b/src/hydrilla/proxy/state_impl/items.py
@@ -37,10 +37,34 @@ import sqlite3
import typing as t
import dataclasses as dc
+from contextlib import contextmanager
+
from ... import item_infos
from .. import state as st
from . import base
+
+def _get_parent_item_id(cursor: sqlite3.Cursor, version_id: str) -> str:
+ cursor.execute(
+ '''
+ SELECT
+ item_id
+ FROM
+ item_versions
+ WHERE
+ item_version_id = ?;
+ ''',
+ (version_id,)
+ )
+
+ rows = cursor.fetchall()
+ if rows == []:
+ raise st.MissingItemError()
+
+ (item_id,), = rows
+
+ return str(item_id)
+
def _set_installed_status(cursor: sqlite3.Cursor, id: str, new_status: str) \
-> None:
cursor.execute(
@@ -213,41 +237,56 @@ class ConcreteMappingRef(st.MappingRef):
self.state.recompute_dependencies([int(self.id)])
- def get_version_display_infos(self) \
- -> t.Sequence[st.MappingVersionDisplayInfo]:
+ def get_display_info(self) -> st.RichMappingDisplayInfo:
with self.state.cursor() as cursor:
cursor.execute(
'''
SELECT
- ive.item_version_id,
- ive.definition,
- ive.repo,
- ive.repo_iteration,
- ive.installed,
- ive.active,
- ive.is_orphan,
- ive.is_local,
- ms.frozen,
- ms.enabled
+ i.identifier,
+ ms.enabled, ms.frozen
FROM
- item_versions_extra AS ive
- JOIN mapping_statuses AS ms USING (item_id)
+ items AS i
+ JOIN mapping_statuses AS ms USING (item_id)
WHERE
- ive.item_id = ?;
+ item_id = ?;
''',
(self.id,)
)
rows = cursor.fetchall()
- if rows == []:
- raise st.MissingItemError()
+ if rows == []:
+ raise st.MissingItemError()
- result = []
+ (identifier, enabled_status, frozen_status), = rows
+
+ cursor.execute(
+ '''
+ SELECT
+ item_version_id,
+ definition,
+ repo,
+ repo_iteration,
+ installed,
+ active,
+ is_orphan,
+ is_local
+ FROM
+ item_versions_extra
+ WHERE
+ item_id = ?;
+ ''',
+ (self.id,)
+ )
+
+ rows = cursor.fetchall()
+
+ version_infos = []
+
+ active_info: t.Optional[st.MappingVersionDisplayInfo] = None
for (item_version_id, definition, repo, repo_iteration,
- installed_status, active_status, is_orphan, is_local,
- frozen_status, enabled_status) in rows:
+ installed_status, active_status, is_orphan, is_local) in rows:
ref = ConcreteMappingVersionRef(str(item_version_id), self.state)
item_info = item_infos.MappingInfo.load(
@@ -256,19 +295,28 @@ class ConcreteMappingRef(st.MappingRef):
repo_iteration
)
- display_info = st.MappingVersionDisplayInfo(
+ version_display_info = st.MappingVersionDisplayInfo(
ref = ref,
info = item_info,
installed = st.InstalledStatus(installed_status),
active = st.ActiveStatus(active_status),
is_orphan = is_orphan,
- is_local = is_local,
- mapping_enabled = st.EnabledStatus(enabled_status),
- mapping_frozen = st.FrozenStatus.make(frozen_status)
+ is_local = is_local
)
- result.append(display_info)
- return sorted(result, key=(lambda di: di.info))
+ version_infos.append(version_display_info)
+
+ if active_status in ('R', 'A'):
+ active_info = version_display_info
+
+ return st.RichMappingDisplayInfo(
+ ref = self,
+ identifier = identifier,
+ enabled = st.EnabledStatus(enabled_status),
+ frozen = st.FrozenStatus.make(frozen_status),
+ active_version = active_info,
+ all_versions = sorted(version_infos, key=(lambda vi: vi.info))
+ )
@dc.dataclass(frozen=True)
@@ -338,9 +386,7 @@ class ConcreteMappingStore(st.MappingStore):
installed = st.InstalledStatus(installed_status),
active = st.ActiveStatus(active_status),
is_orphan = is_orphan,
- is_local = is_local,
- mapping_enabled = st.EnabledStatus(enabled_status),
- mapping_frozen = st.FrozenStatus.make(frozen_status)
+ is_local = is_local
)
display_info = st.MappingDisplayInfo(
@@ -366,56 +412,27 @@ class ConcreteMappingVersionRef(st.MappingVersionRef):
def uninstall(self) -> t.Optional['ConcreteMappingVersionRef']:
return _uninstall_version(self)
+ @contextmanager
+ def _mapping_ref(self) -> t.Iterator[ConcreteMappingRef]:
+ with self.state.cursor(transaction=True) as cursor:
+ mapping_id = _get_parent_item_id(cursor, self.id)
+ yield ConcreteMappingRef(mapping_id, self.state)
+
def update_mapping_status(
self,
enabled: st.EnabledStatus,
frozen: t.Optional[st.FrozenStatus] = None
) -> None:
- with self.state.cursor(transaction=True) as cursor:
- cursor.execute(
- 'SELECT item_id FROM item_versions WHERE item_version_id = ?;',
- (self.id,)
- )
-
- rows = cursor.fetchall()
-
- if rows == []:
- raise st.MissingItemError()
-
- (mapping_id,), = rows
-
- mapping_ref = ConcreteMappingRef(str(mapping_id), self.state)
-
+ with self._mapping_ref() as mapping_ref:
id_to_pass: t.Optional[str] = self.id
if enabled.value != 'E' or frozen is None or frozen.value == 'N':
id_to_pass = None
mapping_ref.update_status(enabled, frozen, id_to_pass)
- def get_all_version_display_infos(self) \
- -> t.Sequence[st.MappingVersionDisplayInfo]:
- with self.state.cursor() as cursor:
- cursor.execute(
- '''
- SELECT
- item_id
- FROM
- item_versions
- WHERE
- item_version_id = ?;
- ''',
- (self.id,)
- )
-
- rows = cursor.fetchall()
- if rows == []:
- raise st.MissingItemError()
-
- (mapping_id,), = rows
-
- mapping_ref = ConcreteMappingRef(str(mapping_id), self.state)
-
- return mapping_ref.get_version_display_infos()
+ def get_item_display_info(self) -> st.RichMappingDisplayInfo:
+ with self._mapping_ref() as mapping_ref:
+ return mapping_ref.get_display_info()
@dc.dataclass(frozen=True)
@@ -430,35 +447,42 @@ class ConcreteMappingVersionStore(st.MappingVersionStore):
class ConcreteResourceRef(st.ResourceRef):
state: base.HaketiloStateWithFields = dc.field(hash=False, compare=False)
- def get_version_display_infos(self) \
- -> t.Sequence[st.ResourceVersionDisplayInfo]:
+ def get_display_info(self) -> st.RichResourceDisplayInfo:
with self.state.cursor() as cursor:
cursor.execute(
+ 'SELECT identifier FROM items WHERE item_id = ?;',
+ (self.id,)
+ )
+
+ rows = cursor.fetchall()
+
+ if rows == []:
+ raise st.MissingItemError()
+
+ (identifier,), = rows
+
+ cursor.execute(
'''
SELECT
- ive.item_version_id,
- ive.definition,
- ive.repo,
- ive.repo_iteration,
- ive.installed,
- ive.active,
- ive.is_orphan,
- ive.is_local
+ item_version_id,
+ definition,
+ repo,
+ repo_iteration,
+ installed,
+ active,
+ is_orphan,
+ is_local
FROM
- item_versions_extra AS ive
- JOIN items AS i USING (item_id)
+ item_versions_extra
WHERE
- i.type = 'R' AND ive.item_id = ?;
+ item_id = ?;
''',
(self.id,)
)
rows = cursor.fetchall()
- if rows == []:
- raise st.MissingItemError()
-
- result = []
+ version_infos = []
for (item_version_id, definition, repo, repo_iteration,
installed_status, active_status, is_orphan, is_local) in rows:
@@ -478,9 +502,14 @@ class ConcreteResourceRef(st.ResourceRef):
is_orphan = is_orphan,
is_local = is_local
)
- result.append(display_info)
- return sorted(result, key=(lambda di: di.info))
+ version_infos.append(display_info)
+
+ return st.RichResourceDisplayInfo(
+ ref = self,
+ identifier = identifier,
+ all_versions = sorted(version_infos, key=(lambda vi: vi.info))
+ )
@dc.dataclass(frozen=True)
@@ -518,30 +547,11 @@ class ConcreteResourceVersionRef(st.ResourceVersionRef):
def uninstall(self) -> t.Optional['ConcreteResourceVersionRef']:
return _uninstall_version(self)
- def get_all_version_display_infos(self) \
- -> t.Sequence[st.ResourceVersionDisplayInfo]:
+ def get_item_display_info(self) -> st.RichResourceDisplayInfo:
with self.state.cursor() as cursor:
- cursor.execute(
- '''
- SELECT
- item_id
- FROM
- item_versions
- WHERE
- item_version_id = ?;
- ''',
- (self.id,)
- )
-
- rows = cursor.fetchall()
- if rows == []:
- raise st.MissingItemError()
-
- (resource_id,), = rows
-
- resource_ref = ConcreteResourceRef(str(resource_id), self.state)
-
- return resource_ref.get_version_display_infos()
+ resource_id = _get_parent_item_id(cursor, self.id)
+ resource_ref = ConcreteResourceRef(resource_id, self.state)
+ return resource_ref.get_display_info()
@dc.dataclass(frozen=True)