Improve cqi extension structure

This commit is contained in:
Patrick Jentsch 2023-06-30 15:36:45 +02:00
parent 912bd7da07
commit 1cd9540e5b
6 changed files with 56 additions and 50 deletions

View File

@ -8,17 +8,11 @@ from typing import Callable, Dict, List
from app import socketio from app import socketio
from app.decorators import socketio_login_required from app.decorators import socketio_login_required
from . import NAMESPACE as ns from . import NAMESPACE as ns
from .extensions import ( from .extensions import CQI_EXTENSION_FUNCTION_NAMES
corpus_update_db, from . import extensions as extensions_module
corpus_static_data,
corpus_paginate_corpus,
cqp_paginate_subcorpus,
cqp_partial_export_subcorpus,
cqp_export_subcorpus,
)
CQI_API_FUNCTIONS: List[str] = [ CQI_FUNCTION_NAMES: List[str] = [
'ask_feature_cl_2_3', 'ask_feature_cl_2_3',
'ask_feature_cqi_1_0', 'ask_feature_cqi_1_0',
'ask_feature_cqp_2_3', 'ask_feature_cqp_2_3',
@ -64,14 +58,6 @@ CQI_API_FUNCTIONS: List[str] = [
'ctrl_ping', 'ctrl_ping',
'ctrl_user_abort' '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) @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'] cqi_client_lock: Lock = session['cqi_over_sio']['cqi_client_lock']
except KeyError: except KeyError:
return {'code': 424, 'msg': 'Failed Dependency'} 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) 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_args['cqi_client'] = cqi_client
fn: Callable = CQI_NOPAQUE_FUNCTIONS[fn_name] fn: Callable = getattr(extensions_module, fn_name)
else: else:
return {'code': 400, 'msg': 'Bad Request'} return {'code': 400, 'msg': 'Bad Request'}
for param in signature(fn).parameters.values(): for param in signature(fn).parameters.values():

View File

@ -1,17 +1,28 @@
from collections import Counter from collections import Counter
from cqi import CQiClient from cqi import CQiClient
from cqi.models.corpora import Corpus
from cqi.status import StatusOk from cqi.status import StatusOk
from flask import session from flask import session
from typing import Dict from typing import Dict, List
import json import json
import math import math
import os import os
from app import db from app import db
from app.models import Corpus 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']) db_corpus = Corpus.query.get(session['cqi_over_sio']['corpus_id'])
cqi_corpus = cqi_client.corpora.get(corpus) cqi_corpus = cqi_client.corpora.get(corpus)
db_corpus.num_tokens = cqi_corpus.size db_corpus.num_tokens = cqi_corpus.size
@ -19,7 +30,7 @@ def corpus_update_db(cqi_client: CQiClient, corpus: str):
return StatusOk() 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']) db_corpus = Corpus.query.get(session['cqi_over_sio']['corpus_id'])
static_corpus_data_file = os.path.join(db_corpus.path, 'cwb', 'static.json') static_corpus_data_file = os.path.join(db_corpus.path, 'cwb', 'static.json')
if os.path.exists(static_corpus_data_file): 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 return static_corpus_data
def corpus_paginate_corpus( def ext_corpus_paginate_corpus(
cqi_client: CQiClient, cqi_client: CQiClient,
corpus: str, corpus: str,
page: int = 1, page: int = 1,
@ -170,7 +181,7 @@ def corpus_paginate_corpus(
return payload return payload
def cqp_paginate_subcorpus( def ext_cqp_paginate_subcorpus(
cqi_client: CQiClient, cqi_client: CQiClient,
subcorpus: str, subcorpus: str,
context: int = 50, context: int = 50,
@ -218,7 +229,7 @@ def cqp_paginate_subcorpus(
return payload return payload
def cqp_partial_export_subcorpus( def ext_cqp_partial_export_subcorpus(
cqi_client: CQiClient, cqi_client: CQiClient,
subcorpus: str, subcorpus: str,
match_id_list: list, match_id_list: list,
@ -231,7 +242,7 @@ def cqp_partial_export_subcorpus(
return cqi_subcorpus_partial_export return cqi_subcorpus_partial_export
def cqp_export_subcorpus( def ext_cqp_export_subcorpus(
cqi_client: CQiClient, cqi_client: CQiClient,
subcorpus: str, subcorpus: str,
context: int = 50 context: int = 50

View File

@ -1,6 +1,7 @@
from typing import Dict, List
from cqi.models.corpora import Corpus from cqi.models.corpora import Corpus
from cqi.models.subcorpora import Subcorpus 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: def lookups_by_cpos(corpus: Corpus, cpos_list: List[int]) -> Dict:

View File

@ -617,8 +617,8 @@ cqi.api.APIClient = class APIClient {
* @param {string} corpus * @param {string} corpus
* @returns {Promise<cqi.status.StatusOk>} * @returns {Promise<cqi.status.StatusOk>}
*/ */
async corpus_update_db(corpus) { async ext_corpus_update_db(corpus) {
const fn_name = 'nopaque_corpus_update_db'; const fn_name = 'ext_corpus_update_db';
const fn_args = {corpus: corpus}; const fn_args = {corpus: corpus};
let payload = await this.#request(fn_name, fn_args); let payload = await this.#request(fn_name, fn_args);
return new cqi.status.lookup[payload.code](); return new cqi.status.lookup[payload.code]();
@ -628,8 +628,8 @@ cqi.api.APIClient = class APIClient {
* @param {string} corpus * @param {string} corpus
* @returns {Promise<object>} * @returns {Promise<object>}
*/ */
async corpus_static_data(corpus) { async ext_corpus_static_data(corpus) {
const fn_name = 'nopaque_corpus_static_data'; const fn_name = 'ext_corpus_static_data';
const fn_args = {corpus: corpus}; const fn_args = {corpus: corpus};
return await this.#request(fn_name, fn_args); return await this.#request(fn_name, fn_args);
} }
@ -640,8 +640,8 @@ cqi.api.APIClient = class APIClient {
* @param {number=} per_page * @param {number=} per_page
* @returns {Promise<object>} * @returns {Promise<object>}
*/ */
async corpus_paginate_corpus(corpus, page, per_page) { async ext_corpus_paginate_corpus(corpus, page, per_page) {
const fn_name = 'nopaque_corpus_paginate_corpus'; const fn_name = 'ext_corpus_paginate_corpus';
const fn_args = {corpus: corpus} const fn_args = {corpus: corpus}
if (page !== undefined) {fn_args.page = page;} if (page !== undefined) {fn_args.page = page;}
if (per_page !== undefined) {fn_args.per_page = per_page;} if (per_page !== undefined) {fn_args.per_page = per_page;}
@ -655,8 +655,8 @@ cqi.api.APIClient = class APIClient {
* @param {number=} per_page * @param {number=} per_page
* @returns {Promise<object>} * @returns {Promise<object>}
*/ */
async cqp_paginate_subcorpus(subcorpus, context, page, per_page) { async ext_cqp_paginate_subcorpus(subcorpus, context, page, per_page) {
const fn_name = 'nopaque_cqp_paginate_subcorpus'; const fn_name = 'ext_cqp_paginate_subcorpus';
const fn_args = {subcorpus: subcorpus} const fn_args = {subcorpus: subcorpus}
if (context !== undefined) {fn_args.context = context;} if (context !== undefined) {fn_args.context = context;}
if (page !== undefined) {fn_args.page = page;} if (page !== undefined) {fn_args.page = page;}
@ -670,8 +670,8 @@ cqi.api.APIClient = class APIClient {
* @param {number=} context * @param {number=} context
* @returns {Promise<object>} * @returns {Promise<object>}
*/ */
async cqp_partial_export_subcorpus(subcorpus, match_id_list, context) { async ext_cqp_partial_export_subcorpus(subcorpus, match_id_list, context) {
const fn_name = 'nopaque_cqp_partial_export_subcorpus'; const fn_name = 'ext_cqp_partial_export_subcorpus';
const fn_args = {subcorpus: subcorpus, match_id_list: match_id_list}; const fn_args = {subcorpus: subcorpus, match_id_list: match_id_list};
if (context !== undefined) {fn_args.context = context;} if (context !== undefined) {fn_args.context = context;}
return await this.#request(fn_name, fn_args); return await this.#request(fn_name, fn_args);
@ -682,8 +682,8 @@ cqi.api.APIClient = class APIClient {
* @param {number=} context * @param {number=} context
* @returns {Promise<object>} * @returns {Promise<object>}
*/ */
async cqp_export_subcorpus(subcorpus, context) { async ext_cqp_export_subcorpus(subcorpus, context) {
const fn_name = 'nopaque_cqp_export_subcorpus'; const fn_name = 'ext_cqp_export_subcorpus';
const fn_args = {subcorpus: subcorpus}; const fn_args = {subcorpus: subcorpus};
if (context !== undefined) {fn_args.context = context;} if (context !== undefined) {fn_args.context = context;}
return await this.#request(fn_name, fn_args); return await this.#request(fn_name, fn_args);

View File

@ -99,7 +99,7 @@ cqi.models.corpora.Corpus = class Corpus extends cqi.models.resource.Model {
* @returns {cqi.status.StatusOk} * @returns {cqi.status.StatusOk}
*/ */
async updateDb() { 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<object>} * @returns {Promise<object>}
*/ */
async paginate(page, per_page) { 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<object>} * @returns {Promise<object>}
*/ */
async _get(corpusName) { async _get(corpusName) {
return { const returnValue = {
api_name: corpusName, api_name: corpusName,
charset: await this.client.api.corpus_charset(corpusName), charset: await this.client.api.corpus_charset(corpusName),
// full_name: await this.client.api.corpus_full_name(corpusName), // full_name: await this.client.api.corpus_full_name(corpusName),
// info: await this.client.api.corpus_info(corpusName), // info: await this.client.api.corpus_info(corpusName),
name: corpusName, name: corpusName,
properties: await this.client.api.corpus_properties(corpusName), properties: await this.client.api.corpus_properties(corpusName),
size: await this.client.api.cl_attribute_size(`${corpusName}.word`), size: await this.client.api.cl_attribute_size(`${corpusName}.word`)
static_data: await this.client.api.corpus_static_data(corpusName), };
}
/************************************************************************
* 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;
} }
/** /**

View File

@ -100,7 +100,7 @@ cqi.models.subcorpora.Subcorpus = class Subcorpus extends cqi.models.resource.Mo
* @returns {Promise<object>} * @returns {Promise<object>}
*/ */
async paginate(context, page, perPage) { 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<object>} * @returns {Promise<object>}
*/ */
async partialExport(matchIdList, context) { 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<object>} * @returns {Promise<object>}
*/ */
async export(context) { 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);
} }
}; };