From 3d99b8770cb051b5a208b11dced0c3ed311b6425 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Wed, 8 Dec 2021 14:45:05 +0100 Subject: [PATCH] Codestyle enhancements --- app/auth/routes.py | 3 +-- app/corpora/cqi_over_socketio/__init__.py | 2 +- app/corpora/query_results_routes.py | 21 +++++++++++---------- app/corpora/routes.py | 21 +++++++++++---------- app/corpora/tasks.py | 8 ++++---- app/jobs/routes.py | 4 ++-- app/jobs/tasks.py | 4 ++-- app/services/routes.py | 18 ++++++++++-------- app/settings/tasks.py | 2 +- app/templates/corpora/corpus.html.j2 | 2 +- 10 files changed, 44 insertions(+), 41 deletions(-) diff --git a/app/auth/routes.py b/app/auth/routes.py index 5cce4f00..2cda4bc2 100644 --- a/app/auth/routes.py +++ b/app/auth/routes.py @@ -69,8 +69,7 @@ def register(): os.makedirs(user.path) except OSError: current_app.logger.error( - 'Make dir {} led to an OSError!'.format(user.path) - ) + f'Make dir {user.path} led to an OSError!') db.session.delete(user) db.session.commit() abort(500) diff --git a/app/corpora/cqi_over_socketio/__init__.py b/app/corpora/cqi_over_socketio/__init__.py index ff698400..14031c4a 100644 --- a/app/corpora/cqi_over_socketio/__init__.py +++ b/app/corpora/cqi_over_socketio/__init__.py @@ -82,7 +82,7 @@ def connect(auth): socketio.sleep(3) retry_counter -= 1 db.session.refresh(corpus) - cqi_client = cqi.CQiClient('cqpserver_{}'.format(corpus_id)) + cqi_client = cqi.CQiClient(f'cqpserver_{corpus_id}') session['d'] = { 'corpus_id': corpus_id, 'cqi_client': cqi_client, diff --git a/app/corpora/query_results_routes.py b/app/corpora/query_results_routes.py index 478b6fe1..27402dc9 100644 --- a/app/corpora/query_results_routes.py +++ b/app/corpora/query_results_routes.py @@ -33,8 +33,7 @@ def add_query_result(): os.makedirs(os.path.dirname(query_result.path)) except OSError: current_app.logger.error( - 'Make dir {} led to an OSError!'.format(query_result.path) - ) + f'Make dir {query_result.path} led to an OSError!') db.session.rollback() flash('Internal Server Error', 'error') return make_response( @@ -99,13 +98,15 @@ def inspect_query_result(query_result_id): ) with open(query_result.path, 'r') as query_result_file: query_result_file_content = json.load(query_result_file) - return render_template('corpora/query_results/inspect.html.j2', - query_result=query_result, - display_options_form=display_options_form, - inspect_display_options_form=inspect_display_options_form, # noqa - query_result_file_content=query_result_file_content, - query_metadata=query_metadata, - title='Inspect query result') + return render_template( + 'corpora/query_results/inspect.html.j2', + query_result=query_result, + display_options_form=display_options_form, + inspect_display_options_form=inspect_display_options_form, # noqa + query_result_file_content=query_result_file_content, + query_metadata=query_metadata, + title='Inspect query result' + ) @bp.route('/result//delete') @@ -116,7 +117,7 @@ def delete_query_result(query_result_id): if not (query_result.user == current_user or current_user.is_administrator()): abort(403) - flash('Query result "{}" has been marked for deletion!'.format(query_result), 'result') # noqa + flash(f'Query result "{query_result}" marked for deletion!', 'result') tasks.delete_query_result(query_result_id) return redirect(url_for('services.service', service="corpus_analysis")) diff --git a/app/corpora/routes.py b/app/corpora/routes.py index 5af9ea92..1086c298 100644 --- a/app/corpora/routes.py +++ b/app/corpora/routes.py @@ -37,7 +37,7 @@ def add_corpus(): abort(500) else: db.session.commit() - flash('Corpus "{}" added!'.format(corpus.title), 'corpus') + flash(f'Corpus "{corpus.title}" added!', 'corpus') return redirect(url_for('.corpus', corpus_id=corpus.id)) return render_template('corpora/add_corpus.html.j2', form=form, title='Add corpus') @@ -102,7 +102,7 @@ def import_corpus(): corpus.status = 'prepared' db.session.commit() os.remove(archive_file) - flash('Corpus "{}" imported!'.format(corpus.title), 'corpus') + flash(f'Corpus "{corpus.title}" imported!', 'corpus') return make_response( {'redirect_url': url_for('.corpus', corpus_id=corpus.id)}, 201) else: @@ -158,7 +158,7 @@ def delete_corpus(corpus_id): corpus = Corpus.query.get_or_404(corpus_id) if not (corpus.user == current_user or current_user.is_administrator()): abort(403) - flash('Corpus "{}" marked for deletion!'.format(corpus.title), 'corpus') + flash(f'Corpus "{corpus.title}" marked for deletion!', 'corpus') tasks.delete_corpus(corpus_id) return redirect(url_for('main.dashboard')) @@ -194,7 +194,7 @@ def add_corpus_file(corpus_id): db.session.add(corpus_file) corpus.status = 'unprepared' db.session.commit() - flash('Corpus file "{}" added!'.format(corpus_file.filename), 'corpus') + flash(f'Corpus file "{corpus_file.filename}" added!', 'corpus') return make_response({'redirect_url': url_for('.corpus', corpus_id=corpus.id)}, 201) # noqa return render_template('corpora/add_corpus_file.html.j2', corpus=corpus, form=form, title='Add corpus file') @@ -209,7 +209,8 @@ def delete_corpus_file(corpus_id, corpus_file_id): if not (corpus_file.corpus.user == current_user or current_user.is_administrator()): abort(403) - flash('Corpus file "{}" marked for deletion!'.format(corpus_file.filename), 'corpus') # noqa + flash( + f'Corpus file "{corpus_file.filename}" marked for deletion!', 'corpus') tasks.delete_corpus_file(corpus_file_id) return redirect(url_for('.corpus', corpus_id=corpus_id)) @@ -253,7 +254,7 @@ def corpus_file(corpus_id, corpus_file_id): corpus_file.title = form.title.data corpus.status = 'unprepared' db.session.commit() - flash('Corpus file "{}" edited!'.format(corpus_file.filename), 'corpus') # noqa + flash(f'Corpus file "{corpus_file.filename}" edited!', 'corpus') return redirect(url_for('.corpus', corpus_id=corpus_id)) # If no form is submitted or valid, fill out fields with current values form.address.data = corpus_file.address @@ -273,15 +274,15 @@ def corpus_file(corpus_id, corpus_file_id): title='Edit corpus file') -@bp.route('//prepare') +@bp.route('//build') @login_required -def prepare_corpus(corpus_id): +def build_corpus(corpus_id): corpus = Corpus.query.get_or_404(corpus_id) if not (corpus.user == current_user or current_user.is_administrator()): abort(403) if corpus.files.all(): tasks.build_corpus(corpus_id) - flash('Corpus "{}" has been marked to get build!'.format(corpus.title), 'corpus') # noqa + flash(f'Corpus "{corpus.title}" marked for building!', 'corpus') else: - flash('Can not build corpus "{}": No corpus file(s)!'.format(corpus.title), 'error') # noqa + flash(f'Can\'t build corpus "{corpus.title}": No corpus file(s)!', 'error') # noqa return redirect(url_for('.corpus', corpus_id=corpus_id)) diff --git a/app/corpora/tasks.py b/app/corpora/tasks.py index f65a00de..c37ebd99 100644 --- a/app/corpora/tasks.py +++ b/app/corpora/tasks.py @@ -9,7 +9,7 @@ def build_corpus(corpus_id, *args, **kwargs): with app.app_context(): corpus = Corpus.query.get(corpus_id) if corpus is None: - raise Exception('Corpus {} not found'.format(corpus_id)) + raise Exception(f'Corpus {corpus_id} not found') corpus.build() db.session.commit() @@ -19,7 +19,7 @@ def delete_corpus(corpus_id, *args, **kwargs): with kwargs['app'].app_context(): corpus = Corpus.query.get(corpus_id) if corpus is None: - raise Exception('Corpus {} not found'.format(corpus_id)) + raise Exception(f'Corpus {corpus_id} not found') corpus.delete() db.session.commit() @@ -29,7 +29,7 @@ def delete_corpus_file(corpus_file_id, *args, **kwargs): with kwargs['app'].app_context(): corpus_file = CorpusFile.query.get(corpus_file_id) if corpus_file is None: - raise Exception('Corpus file {} not found'.format(corpus_file_id)) + raise Exception(f'Corpus file {corpus_file_id} not found') corpus_file.delete() db.session.commit() @@ -39,6 +39,6 @@ def delete_query_result(query_result_id, *args, **kwargs): with kwargs['app'].app_context(): query_result = QueryResult.query.get(query_result_id) if query_result is None: - raise Exception('QueryResult {} not found'.format(query_result_id)) + raise Exception(f'QueryResult {query_result_id} not found') query_result.delete() db.session.commit() diff --git a/app/jobs/routes.py b/app/jobs/routes.py index 5138a28f..db8c686c 100644 --- a/app/jobs/routes.py +++ b/app/jobs/routes.py @@ -48,10 +48,10 @@ def download_job_input(job_id, job_input_id): def restart(job_id): job = Job.query.get_or_404(job_id) if job.status not in ['complete', 'failed']: - flash('Can not restart job "{}": Status is not "complete/failed"'.format(job.title), 'error') # noqa + flash(f'Can not restart job "{job.title}": Status is not "complete/failed"', 'error') # noqa else: tasks.restart_job(job_id) - flash('Job "{}" has been marked to get restarted!'.format(job.title), 'job') # noqa + flash(f'Job "{job.title}" marked to get restarted!', 'job') return redirect(url_for('.job', job_id=job_id)) diff --git a/app/jobs/tasks.py b/app/jobs/tasks.py index 08c2aa22..9ba974e0 100644 --- a/app/jobs/tasks.py +++ b/app/jobs/tasks.py @@ -8,7 +8,7 @@ def delete_job(job_id, *args, **kwargs): with kwargs['app'].app_context(): job = Job.query.get(job_id) if job is None: - raise Exception('Job {} not found'.format(job_id)) + raise Exception(f'Job {job_id} not found') job.delete() db.session.commit() @@ -18,7 +18,7 @@ def restart_job(job_id, *args, **kwargs): with kwargs['app'].app_context(): job = Job.query.get(job_id) if job is None: - raise Exception('Job {} not found'.format(job_id)) + raise Exception(f'Job {job_id} not found') try: job.restart() except Exception: diff --git a/app/services/routes.py b/app/services/routes.py index 26218226..805ab692 100644 --- a/app/services/routes.py +++ b/app/services/routes.py @@ -37,11 +37,11 @@ def service(service): return make_response(form.errors, 400) service_args = [] if service == 'nlp': - service_args.append('-l {}'.format(form.language.data)) + service_args.append(f'-l {form.language.data}') if form.check_encoding.data: service_args.append('--check-encoding') if service == 'ocr': - service_args.append('-l {}'.format(form.language.data)) + service_args.append(f'-l {form.language.data}') if form.binarization.data: service_args.append('--binarize') job = Job(user=current_user, @@ -55,9 +55,7 @@ def service(service): try: os.makedirs(job.path) except OSError: - current_app.logger.error( - 'Make dir {} led to an OSError!'.format(job.path) - ) + current_app.logger.error(f'Make dir {job.path} led to an OSError!') db.session.rollback() flash('Internal Server Error', 'error') return make_response( @@ -71,8 +69,12 @@ def service(service): db.session.add(job_input) job.status = 'submitted' db.session.commit() - flash('Job "{}" added'.format(job.title), 'job') + flash(f'Job "{job.title}" added', 'job') return make_response( {'redirect_url': url_for('jobs.job', job_id=job.id)}, 201) - return render_template('services/{}.html.j2'.format(service.replace('-', '_')), - form=form, title=title, versions=versions) + return render_template( + f'services/{service.replace("-", "_")}.html.j2', + form=form, + title=title, + versions=versions + ) diff --git a/app/settings/tasks.py b/app/settings/tasks.py index 61f737c5..cc5d9e36 100644 --- a/app/settings/tasks.py +++ b/app/settings/tasks.py @@ -8,6 +8,6 @@ def delete_user(user_id, *args, **kwargs): with kwargs['app'].app_context(): user = User.query.get(user_id) if user is None: - raise Exception('User {} not found'.format(user_id)) + raise Exception(f'User {user_id} not found') user.delete() db.session.commit() diff --git a/app/templates/corpora/corpus.html.j2 b/app/templates/corpora/corpus.html.j2 index bae6f8d5..7ca7b413 100644 --- a/app/templates/corpora/corpus.html.j2 +++ b/app/templates/corpora/corpus.html.j2 @@ -65,7 +65,7 @@