Passed
Push — issue703-python-3.11-support ( f59527...05d52a )
by Juho
04:06 queued 14s
created

annif._set_tensorflow_loglevel()   A

Complexity

Conditions 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nop 0
dl 0
loc 15
rs 9.85
c 0
b 0
f 0
1
#!/usr/bin/env python3
2
3
from __future__ import annotations
4
5
import logging
6
import os
7
import os.path
8
from typing import TYPE_CHECKING
9
10
logging.basicConfig()
11
logger = logging.getLogger("annif")
12
logger.setLevel(level=logging.INFO)
13
14
import annif.backend  # noqa
15
16
if TYPE_CHECKING:
17
    from flask.app import Flask
18
19
20
def create_flask_app(config_name: str | None = None) -> Flask:
21
    """Create a Flask app to be used by the CLI."""
22
    from flask import Flask
23
24
    _set_tensorflow_loglevel()
25
26
    app = Flask(__name__)
27
    config_name = _get_config_name(config_name)
28
    logger.debug(f"creating flask app with configuration {config_name}")
29
    app.config.from_object(config_name)
30
    app.config.from_envvar("ANNIF_SETTINGS", silent=True)
31
    return app
32
33
34
def create_app(config_name: str | None = None) -> Flask:
35
    """Create a Connexion app to be used for the API."""
36
    # 'cxapp' here is the Connexion application that has a normal Flask app
37
    # as a property (cxapp.app)
38
    import connexion
39
    from flask_cors import CORS
40
41
    from annif.openapi.validation import CustomRequestBodyValidator
42
43
    specdir = os.path.join(os.path.dirname(__file__), "openapi")
44
    cxapp = connexion.App(__name__, specification_dir=specdir)
45
    config_name = _get_config_name(config_name)
46
    logger.debug(f"creating connexion app with configuration {config_name}")
47
    cxapp.app.config.from_object(config_name)
48
    cxapp.app.config.from_envvar("ANNIF_SETTINGS", silent=True)
49
50
    validator_map = {
51
        "body": CustomRequestBodyValidator,
52
    }
53
    cxapp.add_api("annif.yaml", validator_map=validator_map)
54
55
    # add CORS support
56
    CORS(cxapp.app)
57
58
    if cxapp.app.config["INITIALIZE_PROJECTS"]:
59
        annif.registry.initialize_projects(cxapp.app)
60
        logger.info("finished initializing projects")
61
62
    # register the views via blueprints
63
    from annif.views import bp
64
65
    cxapp.app.register_blueprint(bp)
66
67
    # return the Flask app
68
    return cxapp.app
69
70
71
def _get_config_name(config_name: str | None) -> str:
72
    if config_name is None:
73
        config_name = os.environ.get("ANNIF_CONFIG")
74
    if config_name is None:
75
        if os.environ.get("FLASK_RUN_FROM_CLI") == "true":  # pragma: no cover
76
            config_name = "annif.default_config.Config"
77
        else:
78
            config_name = "annif.default_config.ProductionConfig"  # pragma: no cover
79
    return config_name
80
81
82
def _set_tensorflow_loglevel():
83
    """Set TensorFlow log level based on Annif log level (--verbosity/-v
84
    option) using an environment variable. INFO messages by TF are shown only on
85
    DEBUG (or NOTSET) level of Annif."""
86
    annif_loglevel = logger.getEffectiveLevel()
87
    tf_loglevel_mapping = {
88
        0: "0",  # NOTSET
89
        10: "0",  # DEBUG
90
        20: "1",  # INFO
91
        30: "1",  # WARNING
92
        40: "2",  # ERROR
93
        50: "3",  # CRITICAL
94
    }
95
    tf_loglevel = tf_loglevel_mapping[annif_loglevel]
96
    os.environ.setdefault("TF_CPP_MIN_LOG_LEVEL", tf_loglevel)
97