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} */ - async corpus_static_data(corpus) { - const fn_name = 'nopaque_corpus_static_data'; + async ext_corpus_static_data(corpus) { + const fn_name = 'ext_corpus_static_data'; const fn_args = {corpus: corpus}; return await this.#request(fn_name, fn_args); } @@ -640,8 +640,8 @@ cqi.api.APIClient = class APIClient { * @param {number=} per_page * @returns {Promise} */ - async corpus_paginate_corpus(corpus, page, per_page) { - const fn_name = 'nopaque_corpus_paginate_corpus'; + async ext_corpus_paginate_corpus(corpus, page, per_page) { + const fn_name = 'ext_corpus_paginate_corpus'; const fn_args = {corpus: corpus} if (page !== undefined) {fn_args.page = page;} if (per_page !== undefined) {fn_args.per_page = per_page;} @@ -655,8 +655,8 @@ cqi.api.APIClient = class APIClient { * @param {number=} per_page * @returns {Promise} */ - async cqp_paginate_subcorpus(subcorpus, context, page, per_page) { - const fn_name = 'nopaque_cqp_paginate_subcorpus'; + async ext_cqp_paginate_subcorpus(subcorpus, context, page, per_page) { + const fn_name = 'ext_cqp_paginate_subcorpus'; const fn_args = {subcorpus: subcorpus} if (context !== undefined) {fn_args.context = context;} if (page !== undefined) {fn_args.page = page;} @@ -670,8 +670,8 @@ cqi.api.APIClient = class APIClient { * @param {number=} context * @returns {Promise} */ - async cqp_partial_export_subcorpus(subcorpus, match_id_list, context) { - const fn_name = 'nopaque_cqp_partial_export_subcorpus'; + async ext_cqp_partial_export_subcorpus(subcorpus, match_id_list, context) { + const fn_name = 'ext_cqp_partial_export_subcorpus'; const fn_args = {subcorpus: subcorpus, match_id_list: match_id_list}; if (context !== undefined) {fn_args.context = context;} return await this.#request(fn_name, fn_args); @@ -682,8 +682,8 @@ cqi.api.APIClient = class APIClient { * @param {number=} context * @returns {Promise} */ - async cqp_export_subcorpus(subcorpus, context) { - const fn_name = 'nopaque_cqp_export_subcorpus'; + async ext_cqp_export_subcorpus(subcorpus, context) { + const fn_name = 'ext_cqp_export_subcorpus'; const fn_args = {subcorpus: subcorpus}; if (context !== undefined) {fn_args.context = context;} return await this.#request(fn_name, fn_args); diff --git a/app/static/js/cqi/models/corpora.js b/app/static/js/cqi/models/corpora.js index abc3c725..8128c47f 100644 --- a/app/static/js/cqi/models/corpora.js +++ b/app/static/js/cqi/models/corpora.js @@ -99,7 +99,7 @@ cqi.models.corpora.Corpus = class Corpus extends cqi.models.resource.Model { * @returns {cqi.status.StatusOk} */ async updateDb() { - return await this.client.api.corpus_update_db(this.apiName); + return await this.client.api.ext_corpus_update_db(this.apiName); } /** @@ -108,7 +108,7 @@ cqi.models.corpora.Corpus = class Corpus extends cqi.models.resource.Model { * @returns {Promise} */ async paginate(page, per_page) { - return await this.client.api.corpus_paginate_corpus(this.apiName, page, per_page); + return await this.client.api.ext_corpus_paginate_corpus(this.apiName, page, per_page); } }; @@ -122,16 +122,24 @@ cqi.models.corpora.CorpusCollection = class CorpusCollection extends cqi.models. * @returns {Promise} */ async _get(corpusName) { - return { + const returnValue = { api_name: corpusName, charset: await this.client.api.corpus_charset(corpusName), // full_name: await this.client.api.corpus_full_name(corpusName), // info: await this.client.api.corpus_info(corpusName), name: corpusName, properties: await this.client.api.corpus_properties(corpusName), - size: await this.client.api.cl_attribute_size(`${corpusName}.word`), - static_data: await this.client.api.corpus_static_data(corpusName), - } + size: await this.client.api.cl_attribute_size(`${corpusName}.word`) + }; + + /************************************************************************ + * NOTE: The following is not included in the CQi specification. * + ************************************************************************/ + /************************************************************************ + * Custom additions for nopaque * + ************************************************************************/ + returnValue.static_data = await this.client.api.ext_corpus_static_data(corpusName); + return returnValue; } /** diff --git a/app/static/js/cqi/models/subcorpora.js b/app/static/js/cqi/models/subcorpora.js index 0ba3e9cc..86e6cf67 100644 --- a/app/static/js/cqi/models/subcorpora.js +++ b/app/static/js/cqi/models/subcorpora.js @@ -100,7 +100,7 @@ cqi.models.subcorpora.Subcorpus = class Subcorpus extends cqi.models.resource.Mo * @returns {Promise} */ async paginate(context, page, perPage) { - return await this.client.api.cqp_paginate_subcorpus(this.apiName, context, page, perPage); + return await this.client.api.ext_cqp_paginate_subcorpus(this.apiName, context, page, perPage); } /** @@ -109,7 +109,7 @@ cqi.models.subcorpora.Subcorpus = class Subcorpus extends cqi.models.resource.Mo * @returns {Promise} */ async partialExport(matchIdList, context) { - return await this.client.api.cqp_partial_export_subcorpus(this.apiName, matchIdList, context); + return await this.client.api.ext_cqp_partial_export_subcorpus(this.apiName, matchIdList, context); } /** @@ -117,7 +117,7 @@ cqi.models.subcorpora.Subcorpus = class Subcorpus extends cqi.models.resource.Mo * @returns {Promise} */ async export(context) { - return await this.client.api.cqp_export_subcorpus(this.apiName, context); + return await this.client.api.ext_cqp_export_subcorpus(this.apiName, context); } };