Skip to content

Commit

Permalink
Initialize projects matching pattern given in env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
juhoinkinen committed Feb 29, 2024
1 parent eba2b0c commit 54ee767
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
5 changes: 4 additions & 1 deletion annif/registry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Registry that keeps track of Annif projects"""
from __future__ import annotations

import os
import re

from flask import Flask, current_app
Expand Down Expand Up @@ -35,9 +36,11 @@ def __init__(
self._projects_config_path = projects_config_path
self._datadir = datadir
self._init_vars()
projects_pattern = os.getenv("ANNIF_PROJECTS_INIT", ".*")
if init_projects:
for project in self._projects[self._rid].values():
project.initialize()
if re.search(projects_pattern, project.project_id) is not None:
project.initialize()

def _init_vars(self) -> None:
# initialize the static variables, if necessary
Expand Down
9 changes: 8 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""common fixtures for use by all test classes"""

import os.path
import os
import shutil
import unittest.mock

Expand Down Expand Up @@ -33,6 +33,13 @@ def app_with_initialize():
return app


@pytest.fixture(scope="module")
@unittest.mock.patch.dict(os.environ, {"ANNIF_PROJECTS_INIT": ".*-fi"})
def app_with_initialize_fi_projects():
app = annif.create_app(config_name="annif.default_config.TestingInitializeConfig")
return app


@pytest.fixture
def app_client(app):
with app.test_client() as app_client:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,20 @@ def test_project_initialized(app_with_initialize):
assert project.backend.initialized


def test_project_not_initialized_with_selection(app_with_initialize_fi_projects):
with app_with_initialize_fi_projects.app_context():
project = annif.registry.get_project("dummy-en")
assert not project.initialized
assert not project.backend.initialized


def test_project_initialized_with_selection(app_with_initialize_fi_projects):
with app_with_initialize_fi_projects.app_context():
project = annif.registry.get_project("dummy-fi")
assert project.initialized
assert project.backend.initialized


def test_project_file_not_found():
app = annif.create_app(config_name="annif.default_config.TestingNoProjectsConfig")
with app.app_context():
Expand Down

0 comments on commit 54ee767

Please sign in to comment.