summaryrefslogtreecommitdiff
path: root/src/hydrilla/proxy/state_impl
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2022-09-09 11:44:59 +0200
committerWojtek Kosior <koszko@koszko.org>2022-09-28 14:03:18 +0200
commitbeb163cfd15b93bf664b6a0590e114d4432e3ef9 (patch)
tree4a98def490dabb5f4df927a7a79504eb2ab66c9c /src/hydrilla/proxy/state_impl
parent45e5cf8dc3ca936e2db8e7e45689d0a3331aad43 (diff)
downloadhaketilo-hydrilla-beb163cfd15b93bf664b6a0590e114d4432e3ef9.tar.gz
haketilo-hydrilla-beb163cfd15b93bf664b6a0590e114d4432e3ef9.zip
[proxy] allow prompting the user when a package suitable for current site is found; add proxy's options page
This commit also causes uncached responses to be forced on all HTTP request to external servers. This is needed to make sure that changes Haketilo makes to HTTP response headers are always picked up by the browser. The drawback is that this increases network traffic causing a performance hit. We might optimize this in the future.
Diffstat (limited to 'src/hydrilla/proxy/state_impl')
-rw-r--r--src/hydrilla/proxy/state_impl/base.py3
-rw-r--r--src/hydrilla/proxy/state_impl/concrete_state.py53
-rw-r--r--src/hydrilla/proxy/state_impl/repos.py5
3 files changed, 39 insertions, 22 deletions
diff --git a/src/hydrilla/proxy/state_impl/base.py b/src/hydrilla/proxy/state_impl/base.py
index 0559a42..75d733f 100644
--- a/src/hydrilla/proxy/state_impl/base.py
+++ b/src/hydrilla/proxy/state_impl/base.py
@@ -140,10 +140,9 @@ class HaketiloStateWithFields(st.HaketiloState):
"""...."""
store_dir: Path
connection: sqlite3.Connection
+ settings: st.HaketiloGlobalSettings
current_cursor: t.Optional[sqlite3.Cursor] = None
- #settings: st.HaketiloGlobalSettings
-
secret: bytes = dc.field(default_factory=(lambda: secrets.token_bytes(16)))
policy_tree: PolicyTree = PolicyTree()
diff --git a/src/hydrilla/proxy/state_impl/concrete_state.py b/src/hydrilla/proxy/state_impl/concrete_state.py
index 8bd25a9..3611db1 100644
--- a/src/hydrilla/proxy/state_impl/concrete_state.py
+++ b/src/hydrilla/proxy/state_impl/concrete_state.py
@@ -56,10 +56,24 @@ from . import _operations
here = Path(__file__).resolve().parent
-@dc.dataclass(frozen=True, unsafe_hash=True)
-class ConcreteRepoIterationRef(st.RepoIterationRef):
- pass
-
+def load_settings(cursor: sqlite3.Cursor) -> st.HaketiloGlobalSettings:
+ cursor.execute(
+ '''
+ SELECT
+ default_allow_scripts, repo_refresh_seconds, mapping_use_mode
+ FROM
+ general
+ '''
+ )
+
+ (default_allow_scripts, repo_refresh_seconds,
+ mapping_use_mode), = cursor.fetchall()
+
+ return st.HaketiloGlobalSettings(
+ default_allow_scripts = default_allow_scripts,
+ repo_refresh_seconds = repo_refresh_seconds,
+ mapping_use_mode = st.MappingUseMode(mapping_use_mode)
+ )
@dc.dataclass
class ConcreteHaketiloState(base.HaketiloStateWithFields):
@@ -233,9 +247,6 @@ class ConcreteHaketiloState(base.HaketiloStateWithFields):
def repo_store(self) -> st.RepoStore:
return repos.ConcreteRepoStore(self)
- def get_repo_iteration(self, repo_iteration_id: str) -> st.RepoIterationRef:
- return ConcreteRepoIterationRef(repo_iteration_id)
-
def mapping_store(self) -> st.MappingStore:
return items.ConcreteMappingStore(self)
@@ -255,11 +266,8 @@ class ConcreteHaketiloState(base.HaketiloStateWithFields):
return self.secret
def get_settings(self) -> st.HaketiloGlobalSettings:
- return st.HaketiloGlobalSettings(
- mapping_use_mode = st.MappingUseMode.AUTO,
- default_allow_scripts = False,
- repo_refresh_seconds = 0
- )
+ with self.lock:
+ return self.settings
def update_settings(
self,
@@ -268,7 +276,18 @@ class ConcreteHaketiloState(base.HaketiloStateWithFields):
default_allow_scripts: t.Optional[bool] = None,
repo_refresh_seconds: t.Optional[int] = None
) -> None:
- raise NotImplementedError()
+ with self.cursor(transaction=True) as cursor:
+ def set_opt(col_name: str, val: t.Union[bool, int, str]) -> None:
+ cursor.execute(f'UPDATE general SET {col_name} = ?;', (val,))
+
+ if mapping_use_mode is not None:
+ set_opt('mapping_use_mode', mapping_use_mode.value)
+ if default_allow_scripts is not None:
+ set_opt('default_allow_scripts', default_allow_scripts)
+ if repo_refresh_seconds is not None:
+ set_opt('repo_refresh_seconds', repo_refresh_seconds)
+
+ self.settings = load_settings(cursor)
@staticmethod
def make(store_dir: Path) -> 'ConcreteHaketiloState':
@@ -277,7 +296,11 @@ class ConcreteHaketiloState(base.HaketiloStateWithFields):
isolation_level = None,
check_same_thread = False
)
+
+ global_settings = load_settings(connection.cursor())
+
return ConcreteHaketiloState(
- store_dir = store_dir,
- connection = connection
+ store_dir = store_dir,
+ connection = connection,
+ settings = global_settings
)
diff --git a/src/hydrilla/proxy/state_impl/repos.py b/src/hydrilla/proxy/state_impl/repos.py
index 8a3fe64..383d147 100644
--- a/src/hydrilla/proxy/state_impl/repos.py
+++ b/src/hydrilla/proxy/state_impl/repos.py
@@ -363,8 +363,3 @@ class ConcreteRepoStore(st.RepoStore):
result.append(make_repo_display_info(ref, *rest))
return result
-
-
-@dc.dataclass(frozen=True, unsafe_hash=True)
-class ConcreteRepoIterationRef(st.RepoIterationRef):
- state: base.HaketiloStateWithFields