From 1cd9540e5b36a4e6be6305e8a62b1dad7de594e5 Mon Sep 17 00:00:00 2001
From: Patrick Jentsch
Date: Fri, 30 Jun 2023 15:36:45 +0200
Subject: [PATCH] Improve cqi extension structure
---
app/corpora/cqi_over_sio/cqi.py | 26 +++++-------------
.../{extensions/__init__.py => extensions.py} | 27 +++++++++++++------
.../cqi_over_sio/{extensions => }/utils.py | 3 ++-
app/static/js/cqi/api/client.js | 24 ++++++++---------
app/static/js/cqi/models/corpora.js | 20 +++++++++-----
app/static/js/cqi/models/subcorpora.js | 6 ++---
6 files changed, 56 insertions(+), 50 deletions(-)
rename app/corpora/cqi_over_sio/{extensions/__init__.py => extensions.py} (93%)
rename app/corpora/cqi_over_sio/{extensions => }/utils.py (99%)
diff --git a/app/corpora/cqi_over_sio/cqi.py b/app/corpora/cqi_over_sio/cqi.py
index 9c7b4e90..62a7f63b 100644
--- a/app/corpora/cqi_over_sio/cqi.py
+++ b/app/corpora/cqi_over_sio/cqi.py
@@ -8,17 +8,11 @@ from typing import Callable, Dict, List
from app import socketio
from app.decorators import socketio_login_required
from . import NAMESPACE as ns
-from .extensions import (
- corpus_update_db,
- corpus_static_data,
- corpus_paginate_corpus,
- cqp_paginate_subcorpus,
- cqp_partial_export_subcorpus,
- cqp_export_subcorpus,
-)
+from .extensions import CQI_EXTENSION_FUNCTION_NAMES
+from . import extensions as extensions_module
-CQI_API_FUNCTIONS: List[str] = [
+CQI_FUNCTION_NAMES: List[str] = [
'ask_feature_cl_2_3',
'ask_feature_cqi_1_0',
'ask_feature_cqp_2_3',
@@ -64,14 +58,6 @@ CQI_API_FUNCTIONS: List[str] = [
'ctrl_ping',
'ctrl_user_abort'
]
-CQI_NOPAQUE_FUNCTIONS: Dict[str, Callable] = {
- 'nopaque_corpus_update_db': corpus_update_db,
- 'nopaque_corpus_static_data': corpus_static_data,
- 'nopaque_corpus_paginate_corpus': corpus_paginate_corpus,
- 'nopaque_cqp_paginate_subcorpus': cqp_paginate_subcorpus,
- 'nopaque_cqp_partial_export_subcorpus': cqp_partial_export_subcorpus,
- 'nopaque_cqp_export_subcorpus': cqp_export_subcorpus,
-}
@socketio.on('cqi', namespace=ns)
@@ -88,11 +74,11 @@ def cqi_over_sio(fn_data):
cqi_client_lock: Lock = session['cqi_over_sio']['cqi_client_lock']
except KeyError:
return {'code': 424, 'msg': 'Failed Dependency'}
- if fn_name in CQI_API_FUNCTIONS:
+ if fn_name in CQI_FUNCTION_NAMES:
fn: Callable = getattr(cqi_client.api, fn_name)
- elif fn_name in CQI_NOPAQUE_FUNCTIONS:
+ elif fn_name in CQI_EXTENSION_FUNCTION_NAMES:
fn_args['cqi_client'] = cqi_client
- fn: Callable = CQI_NOPAQUE_FUNCTIONS[fn_name]
+ fn: Callable = getattr(extensions_module, fn_name)
else:
return {'code': 400, 'msg': 'Bad Request'}
for param in signature(fn).parameters.values():
diff --git a/app/corpora/cqi_over_sio/extensions/__init__.py b/app/corpora/cqi_over_sio/extensions.py
similarity index 93%
rename from app/corpora/cqi_over_sio/extensions/__init__.py
rename to app/corpora/cqi_over_sio/extensions.py
index 09f9210a..33e00bc0 100644
--- a/app/corpora/cqi_over_sio/extensions/__init__.py
+++ b/app/corpora/cqi_over_sio/extensions.py
@@ -1,17 +1,28 @@
from collections import Counter
from cqi import CQiClient
+from cqi.models.corpora import Corpus
from cqi.status import StatusOk
from flask import session
-from typing import Dict
+from typing import Dict, List
import json
import math
import os
from app import db
from app.models import Corpus
-from .utils import lookups_by_cpos, export_subcorpus, partial_export_subcorpus
+from .utils import lookups_by_cpos, partial_export_subcorpus, export_subcorpus
-def corpus_update_db(cqi_client: CQiClient, corpus: str):
+CQI_EXTENSION_FUNCTION_NAMES: List[str] = [
+ 'ext_corpus_update_db',
+ 'ext_corpus_static_data',
+ 'ext_corpus_paginate_corpus',
+ 'ext_cqp_paginate_subcorpus',
+ 'ext_cqp_partial_export_subcorpus',
+ 'ext_cqp_export_subcorpus',
+]
+
+
+def ext_corpus_update_db(cqi_client: CQiClient, corpus: str):
db_corpus = Corpus.query.get(session['cqi_over_sio']['corpus_id'])
cqi_corpus = cqi_client.corpora.get(corpus)
db_corpus.num_tokens = cqi_corpus.size
@@ -19,7 +30,7 @@ def corpus_update_db(cqi_client: CQiClient, corpus: str):
return StatusOk()
-def corpus_static_data(cqi_client: CQiClient, corpus: str) -> Dict:
+def ext_corpus_static_data(cqi_client: CQiClient, corpus: str) -> Dict:
db_corpus = Corpus.query.get(session['cqi_over_sio']['corpus_id'])
static_corpus_data_file = os.path.join(db_corpus.path, 'cwb', 'static.json')
if os.path.exists(static_corpus_data_file):
@@ -125,7 +136,7 @@ def corpus_static_data(cqi_client: CQiClient, corpus: str) -> Dict:
return static_corpus_data
-def corpus_paginate_corpus(
+def ext_corpus_paginate_corpus(
cqi_client: CQiClient,
corpus: str,
page: int = 1,
@@ -170,7 +181,7 @@ def corpus_paginate_corpus(
return payload
-def cqp_paginate_subcorpus(
+def ext_cqp_paginate_subcorpus(
cqi_client: CQiClient,
subcorpus: str,
context: int = 50,
@@ -218,7 +229,7 @@ def cqp_paginate_subcorpus(
return payload
-def cqp_partial_export_subcorpus(
+def ext_cqp_partial_export_subcorpus(
cqi_client: CQiClient,
subcorpus: str,
match_id_list: list,
@@ -231,7 +242,7 @@ def cqp_partial_export_subcorpus(
return cqi_subcorpus_partial_export
-def cqp_export_subcorpus(
+def ext_cqp_export_subcorpus(
cqi_client: CQiClient,
subcorpus: str,
context: int = 50
diff --git a/app/corpora/cqi_over_sio/extensions/utils.py b/app/corpora/cqi_over_sio/utils.py
similarity index 99%
rename from app/corpora/cqi_over_sio/extensions/utils.py
rename to app/corpora/cqi_over_sio/utils.py
index 83892092..121c3233 100644
--- a/app/corpora/cqi_over_sio/extensions/utils.py
+++ b/app/corpora/cqi_over_sio/utils.py
@@ -1,6 +1,7 @@
-from typing import Dict, List
from cqi.models.corpora import Corpus
from cqi.models.subcorpora import Subcorpus
+from typing import Dict, List
+from app.models import Corpus
def lookups_by_cpos(corpus: Corpus, cpos_list: List[int]) -> Dict:
diff --git a/app/static/js/cqi/api/client.js b/app/static/js/cqi/api/client.js
index f3c41715..6e18a37e 100644
--- a/app/static/js/cqi/api/client.js
+++ b/app/static/js/cqi/api/client.js
@@ -617,8 +617,8 @@ cqi.api.APIClient = class APIClient {
* @param {string} corpus
* @returns {Promise}
*/
- async corpus_update_db(corpus) {
- const fn_name = 'nopaque_corpus_update_db';
+ async ext_corpus_update_db(corpus) {
+ const fn_name = 'ext_corpus_update_db';
const fn_args = {corpus: corpus};
let payload = await this.#request(fn_name, fn_args);
return new cqi.status.lookup[payload.code]();
@@ -628,8 +628,8 @@ cqi.api.APIClient = class APIClient {
* @param {string} corpus
* @returns {Promise