2022-02-03 11:39:16 +00:00
|
|
|
from flask import current_app
|
2021-09-15 10:31:53 +00:00
|
|
|
from flask_migrate import upgrade
|
2022-02-03 11:39:16 +00:00
|
|
|
from . import db
|
|
|
|
from .models import Corpus, Job, Role, User, TesseractOCRModel
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
def _make_default_dirs():
|
|
|
|
base_dir = current_app.config['NOPAQUE_DATA_DIR']
|
|
|
|
|
|
|
|
default_directories = [
|
|
|
|
os.path.join(base_dir, 'tmp'),
|
|
|
|
os.path.join(base_dir, 'users')
|
|
|
|
]
|
|
|
|
for directory in default_directories:
|
|
|
|
if os.path.exists(directory):
|
|
|
|
if not os.path.isdir(directory):
|
|
|
|
raise NotADirectoryError(f'{directory} is not a directory')
|
|
|
|
else:
|
|
|
|
os.mkdir(directory)
|
2021-09-15 10:31:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def register(app):
|
|
|
|
@app.cli.command()
|
|
|
|
def deploy():
|
2021-12-07 13:18:05 +00:00
|
|
|
''' Run deployment tasks. '''
|
2022-02-03 11:39:16 +00:00
|
|
|
# Make default directories
|
|
|
|
_make_default_dirs()
|
|
|
|
|
2021-09-15 10:31:53 +00:00
|
|
|
# migrate database to latest revision
|
|
|
|
upgrade()
|
2022-02-03 11:39:16 +00:00
|
|
|
|
|
|
|
# Insert/Update default database values
|
|
|
|
current_app.logger.info('Insert/Update default roles')
|
|
|
|
Role.insert_defaults()
|
|
|
|
current_app.logger.info('Insert/Update default users')
|
|
|
|
User.insert_defaults()
|
|
|
|
current_app.logger.info('Insert/Update default tesseract ocr models')
|
|
|
|
TesseractOCRModel.insert_defaults()
|
2021-09-15 10:31:53 +00:00
|
|
|
|
2021-09-22 11:53:39 +00:00
|
|
|
@app.cli.group()
|
|
|
|
def daemon():
|
2021-12-07 13:18:05 +00:00
|
|
|
''' Daemon commands. '''
|
2021-09-22 11:53:39 +00:00
|
|
|
pass
|
2021-09-15 10:31:53 +00:00
|
|
|
|
2021-09-22 12:13:59 +00:00
|
|
|
@daemon.command('run')
|
2021-09-22 11:58:46 +00:00
|
|
|
def run_daemon():
|
2021-12-07 13:18:05 +00:00
|
|
|
''' Run daemon '''
|
|
|
|
corpus: Corpus
|
2021-11-16 14:23:57 +00:00
|
|
|
for corpus in Corpus.query.filter(Corpus.num_analysis_sessions > 0):
|
|
|
|
corpus.num_analysis_sessions = 0
|
|
|
|
db.session.commit()
|
2021-09-22 11:53:39 +00:00
|
|
|
from app.daemon import Daemon
|
2021-12-07 13:18:05 +00:00
|
|
|
daemon: Daemon = Daemon()
|
2021-09-22 11:53:39 +00:00
|
|
|
daemon.run()
|
|
|
|
|
|
|
|
@app.cli.group()
|
2021-09-15 10:31:53 +00:00
|
|
|
def test():
|
2021-12-07 13:18:05 +00:00
|
|
|
''' Test commands. '''
|
2021-09-22 11:53:39 +00:00
|
|
|
pass
|
|
|
|
|
2021-09-22 12:13:59 +00:00
|
|
|
@test.command('run')
|
2021-09-22 11:58:46 +00:00
|
|
|
def run_test():
|
2021-12-07 13:18:05 +00:00
|
|
|
''' Run unit tests. '''
|
|
|
|
from unittest import TestLoader, TextTestRunner
|
|
|
|
from unittest.suite import TestSuite
|
|
|
|
tests: TestSuite = TestLoader().discover('tests')
|
|
|
|
TextTestRunner(verbosity=2).run(tests)
|
2022-02-03 11:39:16 +00:00
|
|
|
|
|
|
|
@app.cli.group()
|
|
|
|
def convert():
|
|
|
|
''' Datebase convert commands. '''
|
|
|
|
|
|
|
|
@convert.command()
|
|
|
|
def nlp_jobs():
|
|
|
|
for job in Job.query.filter_by(service='nlp').all():
|
|
|
|
job.service = 'spacy-nlp'
|
|
|
|
service_args = json.loads(job.service_args)
|
|
|
|
new_service_args = {}
|
|
|
|
for service_arg in service_args:
|
|
|
|
if service_arg == '--check-encoding':
|
|
|
|
new_service_args['encoding_detection'] = True
|
|
|
|
elif re.match(r'-l ([a-z]{2})', service_arg):
|
|
|
|
language_code = re.search(r'-l ([a-z]{2})', service_arg).group(1) # noqa
|
|
|
|
new_service_args['language'] = language_code
|
|
|
|
job.service_args = json.dumps(new_service_args)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
@convert.command()
|
|
|
|
def ocr_jobs():
|
|
|
|
# Language code to TesseractOCRModel.title lookup
|
|
|
|
language_code_lookup = {
|
|
|
|
'ara': 'Arabic',
|
|
|
|
'chi_tra': 'Chinese - Traditional',
|
|
|
|
'dan': 'Danish',
|
|
|
|
'eng': 'English',
|
|
|
|
'enm': 'English, Middle (1100-1500)',
|
|
|
|
'fra': 'French',
|
|
|
|
'frm': 'French, Middle (ca. 1400-1600)',
|
|
|
|
'deu': 'German',
|
|
|
|
'frk': 'German Fraktur',
|
|
|
|
'ell': 'Greek, Modern (1453-)',
|
|
|
|
'ita': 'Italian',
|
|
|
|
'por': 'Portuguese',
|
|
|
|
'rus': 'Russian',
|
|
|
|
'spa': 'Spanish; Castilian'
|
|
|
|
}
|
|
|
|
for job in Job.query.filter_by(service='ocr').all():
|
|
|
|
job.service = 'tesseract-ocr'
|
|
|
|
service_args = json.loads(job.service_args)
|
|
|
|
new_service_args = {}
|
|
|
|
for service_arg in service_args:
|
|
|
|
if service_arg == '--binarize':
|
|
|
|
new_service_args['binarization'] = True
|
|
|
|
elif re.match(r'-l ([a-z]{3})', service_arg):
|
|
|
|
language_code = re.search(r'-l ([a-z]{3})', service_arg).group(1) # noqa
|
|
|
|
tesseract_ocr_model = TesseractOCRModel.query.filter_by(title=language_code_lookup[language_code]).first() # noqa
|
|
|
|
new_service_args['model'] = tesseract_ocr_model.id
|
|
|
|
job.service_args = json.dumps(new_service_args)
|
|
|
|
db.session.commit()
|