Passed
Pull Request — main (#762)
by Juho
05:52 queued 02:58
created

annif.cli_util   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 156
dl 0
loc 250
rs 9.44
c 0
b 0
f 0
wmc 37
1
"""Utility functions for Annif CLI commands"""
2
3
from __future__ import annotations
4
5
import binascii
6
import collections
7
import configparser
8
import importlib
9
import io
10
import itertools
11
import os
12
import pathlib
13
import shutil
14
import sys
15
import tempfile
16
import time
17
import zipfile
18
from fnmatch import fnmatch
19
from typing import TYPE_CHECKING
20
21
import click
22
import click_log
23
from flask import current_app
24
25
import annif
26
from annif.exception import ConfigurationException, OperationFailedException
27
from annif.project import Access
28
29
if TYPE_CHECKING:
30
    from datetime import datetime
31
32
    from click.core import Argument, Context, Option
33
34
    from annif.corpus.document import DocumentCorpus, DocumentList
35
    from annif.corpus.subject import SubjectIndex
36
    from annif.project import AnnifProject
37
    from annif.suggestion import SuggestionResult
38
    from annif.vocab import AnnifVocabulary
39
40
logger = annif.logger
41
42
43
def _set_project_config_file_path(
44
    ctx: Context, param: Option, value: str | None
45
) -> None:
46
    """Override the default path or the path given in env by CLI option"""
47
    with ctx.obj.load_app().app_context():
48
        if value:
49
            current_app.config["PROJECTS_CONFIG_PATH"] = value
50
51
52
def common_options(f):
53
    """Decorator to add common options for all CLI commands"""
54
    f = click.option(
55
        "-p",
56
        "--projects",
57
        help="Set path to project configuration file or directory",
58
        type=click.Path(dir_okay=True, exists=True),
59
        callback=_set_project_config_file_path,
60
        expose_value=False,
61
        is_eager=True,
62
    )(f)
63
    return click_log.simple_verbosity_option(logger)(f)
64
65
66
def project_id(f):
67
    """Decorator to add a project ID parameter to a CLI command"""
68
    return click.argument("project_id", shell_complete=complete_param)(f)
69
70
71
def backend_param_option(f):
72
    """Decorator to add an option for CLI commands to override BE parameters"""
73
    return click.option(
74
        "--backend-param",
75
        "-b",
76
        multiple=True,
77
        help="Override backend parameter of the config file. "
78
        + "Syntax: `-b <backend>.<parameter>=<value>`.",
79
    )(f)
80
81
82
def docs_limit_option(f):
83
    """Decorator to add an option for CLI commands to limit the number of documents to
84
    use"""
85
    return click.option(
86
        "--docs-limit",
87
        "-d",
88
        default=None,
89
        type=click.IntRange(0, None),
90
        help="Maximum number of documents to use",
91
    )(f)
92
93
94
def get_project(project_id: str) -> AnnifProject:
95
    """
96
    Helper function to get a project by ID and bail out if it doesn't exist"""
97
    try:
98
        return annif.registry.get_project(project_id, min_access=Access.private)
99
    except ValueError:
100
        click.echo("No projects found with id '{0}'.".format(project_id), err=True)
101
        sys.exit(1)
102
103
104
def get_vocab(vocab_id: str) -> AnnifVocabulary:
105
    """
106
    Helper function to get a vocabulary by ID and bail out if it doesn't
107
    exist"""
108
    try:
109
        return annif.registry.get_vocab(vocab_id, min_access=Access.private)
110
    except ValueError:
111
        click.echo(f"No vocabularies found with the id '{vocab_id}'.", err=True)
112
        sys.exit(1)
113
114
115
def make_list_template(*rows) -> str:
116
    """Helper function to create a template for a list of entries with fields of
117
    variable width. The width of each field is determined by the longest item in the
118
    field in the given rows."""
119
120
    max_field_widths = collections.defaultdict(int)
121
    for row in rows:
122
        for field_ind, item in enumerate(row):
123
            max_field_widths[field_ind] = max(max_field_widths[field_ind], len(item))
124
125
    return "  ".join(
126
        [
127
            f"{{{field_ind}: <{field_width}}}"
128
            for field_ind, field_width in max_field_widths.items()
129
        ]
130
    )
131
132
133
def format_datetime(dt: datetime | None) -> str:
134
    """Helper function to format a datetime object as a string in the local time."""
135
    if dt is None:
136
        return "-"
137
    return dt.astimezone().strftime("%Y-%m-%d %H:%M:%S")
138
139
140
def open_documents(
141
    paths: tuple[str, ...],
142
    subject_index: SubjectIndex,
143
    vocab_lang: str,
144
    docs_limit: int | None,
145
) -> DocumentCorpus:
146
    """Helper function to open a document corpus from a list of pathnames,
147
    each of which is either a TSV file or a directory of TXT files. For
148
    directories with subjects in TSV files, the given vocabulary language
149
    will be used to convert subject labels into URIs. The corpus will be
150
    returned as an instance of DocumentCorpus or LimitingDocumentCorpus."""
151
152
    def open_doc_path(path, subject_index):
153
        """open a single path and return it as a DocumentCorpus"""
154
        if os.path.isdir(path):
155
            return annif.corpus.DocumentDirectory(
156
                path, subject_index, vocab_lang, require_subjects=True
157
            )
158
        return annif.corpus.DocumentFile(path, subject_index)
159
160
    if len(paths) == 0:
161
        logger.warning("Reading empty file")
162
        docs = open_doc_path(os.path.devnull, subject_index)
163
    elif len(paths) == 1:
164
        docs = open_doc_path(paths[0], subject_index)
165
    else:
166
        corpora = [open_doc_path(path, subject_index) for path in paths]
167
        docs = annif.corpus.CombinedCorpus(corpora)
168
    if docs_limit is not None:
169
        docs = annif.corpus.LimitingDocumentCorpus(docs, docs_limit)
170
    return docs
171
172
173
def open_text_documents(paths: tuple[str, ...], docs_limit: int | None) -> DocumentList:
174
    """
175
    Helper function to read text documents from the given file paths. Returns a
176
    DocumentList object with Documents having no subjects. If a path is "-", the
177
    document text is read from standard input. The maximum number of documents to read
178
    is set by docs_limit parameter.
179
    """
180
181
    def _docs(paths):
182
        for path in paths:
183
            if path == "-":
184
                doc = annif.corpus.Document(text=sys.stdin.read(), subject_set=None)
185
            else:
186
                with open(path, errors="replace", encoding="utf-8-sig") as docfile:
187
                    doc = annif.corpus.Document(text=docfile.read(), subject_set=None)
188
            yield doc
189
190
    return annif.corpus.DocumentList(_docs(paths[:docs_limit]))
191
192
193
def show_hits(
194
    hits: SuggestionResult,
195
    project: AnnifProject,
196
    lang: str,
197
    file: io.TextIOWrapper | None = None,
198
) -> None:
199
    """
200
    Print subject suggestions to the console or a file. The suggestions are displayed as
201
    a table, with one row per hit. Each row contains the URI, label, possible notation,
202
    and score of the suggestion. The label is given in the specified language.
203
    """
204
    template = "<{}>\t{}\t{:.04f}"
205
    for hit in hits:
206
        subj = project.subjects[hit.subject_id]
207
        line = template.format(
208
            subj.uri,
209
            "\t".join(filter(None, (subj.labels[lang], subj.notation))),
210
            hit.score,
211
        )
212
        click.echo(line, file=file)
213
214
215
def parse_backend_params(
216
    backend_param: tuple[str, ...] | tuple[()], project: AnnifProject
217
) -> collections.defaultdict[str, dict[str, str]]:
218
    """Parse a list of backend parameters given with the --backend-param
219
    option into a nested dict structure"""
220
    backend_params = collections.defaultdict(dict)
221
    for beparam in backend_param:
222
        backend, param = beparam.split(".", 1)
223
        key, val = param.split("=", 1)
224
        _validate_backend_params(backend, beparam, project)
225
        backend_params[backend][key] = val
226
    return backend_params
227
228
229
def _validate_backend_params(backend: str, beparam: str, project: AnnifProject) -> None:
230
    if backend != project.config["backend"]:
231
        raise ConfigurationException(
232
            'The backend {} in CLI option "-b {}" not matching the project'
233
            " backend {}.".format(backend, beparam, project.config["backend"])
234
        )
235
236
237
def generate_filter_params(filter_batch_max_limit: int) -> list[tuple[int, float]]:
238
    limits = range(1, filter_batch_max_limit + 1)
239
    thresholds = [i * 0.05 for i in range(20)]
240
    return list(itertools.product(limits, thresholds))
241
242
243
def get_matching_projects(pattern):
244
    """
245
    Get projects that match the given pattern.
246
    """
247
    return [
248
        proj
249
        for proj in annif.registry.get_projects(min_access=Access.private).values()
250
        if fnmatch(proj.project_id, pattern)
251
    ]
252
253
254
def upload_datadir(data_dir, repo_id, token, commit_message):
255
    """
256
    Upload a data directory to HuggingFace Hub.
257
    """
258
    zip_path = data_dir.split(os.path.sep, 1)[1] + ".zip"
259
    with _archive_dir(data_dir) as fobj:
260
        _upload_to_hf_hub(fobj, zip_path, repo_id, token, commit_message)
261
262
263
def upload_config(project, repo_id, token, commit_message):
264
    """
265
    Upload a project configuration to HuggingFace Hub.
266
    """
267
    config_repo_path = project.project_id + ".cfg"
268
    with _get_project_config(project) as fobj:
269
        _upload_to_hf_hub(fobj, config_repo_path, repo_id, token, commit_message)
270
271
272
def _is_train_file(fname):
273
    train_file_patterns = ("-train", "tmp-")
274
    for pat in train_file_patterns:
275
        if pat in fname:
276
            return True
277
    return False
278
279
280
def _archive_dir(data_dir):
281
    fp = tempfile.TemporaryFile()
282
    path = pathlib.Path(data_dir)
283
    fpaths = [fpath for fpath in path.glob("**/*") if not _is_train_file(fpath.name)]
284
    with zipfile.ZipFile(fp, mode="w") as zfile:
285
        zfile.comment = bytes(
286
            f"Archived by Annif {importlib.metadata.version('annif')}",
287
            encoding="utf-8",
288
        )
289
        for fpath in fpaths:
290
            logger.debug(f"Adding {fpath}")
291
            arcname = os.path.join(*fpath.parts[1:])
292
            zfile.write(fpath, arcname=arcname)
293
    fp.seek(0)
294
    return fp
295
296
297
def _get_project_config(project):
298
    fp = tempfile.TemporaryFile(mode="w+t")
299
    config = configparser.ConfigParser()
300
    config[project.project_id] = project.config
301
    config.write(fp)  # This needs tempfile in text mode
302
    fp.seek(0)
303
    # But for upload fobj needs to be in binary mode
304
    return io.BytesIO(fp.read().encode("utf8"))
305
306
307
def _upload_to_hf_hub(fileobj, filename, repo_id, token, commit_message):
308
    from huggingface_hub import HfApi
309
    from huggingface_hub.utils import HfHubHTTPError, HFValidationError
310
311
    api = HfApi()
312
    try:
313
        api.upload_file(
314
            path_or_fileobj=fileobj,
315
            path_in_repo=filename,
316
            repo_id=repo_id,
317
            token=token,
318
            commit_message=commit_message,
319
        )
320
    except (HfHubHTTPError, HFValidationError) as err:
321
        raise OperationFailedException(str(err))
322
323
324
def get_matching_project_ids_from_hf_hub(project_ids_pattern, repo_id, token, revision):
325
    all_repo_file_paths = _list_files_in_hf_hub(repo_id, token, revision)
326
    return [
327
        path.rsplit(".zip")[0].split("projects/")[1]
328
        for path in all_repo_file_paths
329
        if fnmatch(path, f"projects/{project_ids_pattern}.zip")
330
    ]
331
332
333
def _list_files_in_hf_hub(repo_id, token, revision):
334
    from huggingface_hub import list_repo_files
335
    from huggingface_hub.utils import HfHubHTTPError, HFValidationError
336
337
    try:
338
        return [
339
            repofile
340
            for repofile in list_repo_files(
341
                repo_id=repo_id, token=token, revision=revision
342
            )
343
        ]
344
    except (HfHubHTTPError, HFValidationError) as err:
345
        raise OperationFailedException(str(err))
346
347
348
def download_from_hf_hub(filename, repo_id, token, revision):
349
    from huggingface_hub import hf_hub_download
350
    from huggingface_hub.utils import HfHubHTTPError, HFValidationError
351
352
    try:
353
        return hf_hub_download(
354
            repo_id=repo_id,
355
            filename=filename,
356
            token=token,
357
            revision=revision,
358
        )
359
    except (HfHubHTTPError, HFValidationError) as err:
360
        raise OperationFailedException(str(err))
361
362
363
def unzip_archive(src_path, force):
364
    datadir = current_app.config["DATADIR"]
365
    with zipfile.ZipFile(src_path, "r") as zfile:
366
        archive_comment = str(zfile.comment, encoding="utf-8")
367
        logger.debug(
368
            f'Extracting archive {src_path}; archive comment: "{archive_comment}"'
369
        )
370
        for member in zfile.infolist():
371
            _unzip_member(zfile, member, datadir, force)
372
373
374
def _unzip_member(zfile, member, datadir, force):
375
    dest_path = os.path.join(datadir, member.filename)
376
    if os.path.exists(dest_path) and not force:
377
        if _are_identical(member, dest_path):
378
            logger.debug(f"Skipping unzip to {dest_path}; already in place")
379
        else:
380
            click.echo(f"Not overwriting {dest_path} (use --force to override)")
381
    else:
382
        logger.debug(f"Unzipping to {dest_path}")
383
        zfile.extract(member, path=datadir)
384
        date_time = time.mktime(member.date_time + (0, 0, -1))
385
        os.utime(dest_path, (date_time, date_time))
386
387
388
def copy_project_config(src_path, force):
389
    project_configs_dest_dir = "projects.d"
390
    if not os.path.isdir(project_configs_dest_dir):
391
        os.mkdir(project_configs_dest_dir)
392
393
    dest_path = os.path.join(project_configs_dest_dir, os.path.basename(src_path))
394
    if os.path.exists(dest_path) and not force:
395
        src_crc32 = _compute_crc32(src_path)
396
        dest_crc32 = _compute_crc32(dest_path)
397
        if src_crc32 == dest_crc32:
398
            logger.debug(f"Skipping copy to {dest_path}; already in place")
399
        else:
400
            click.echo(f"Not overwriting {dest_path} (use --force to override)")
401
    else:
402
        logger.debug(f"Copying to {dest_path}")
403
        shutil.copy(src_path, dest_path)
404
405
406
def _are_identical(member, dest_path):
407
    path_crc = _compute_crc32(dest_path)
408
    return path_crc == member.CRC
409
410
411
def _compute_crc32(path):
412
    if os.path.isdir(path):
413
        return 0
414
415
    size = 1024 * 1024 * 10  # 10 MiB chunks
416
    with open(path, "rb") as fp:
417
        crcval = 0
418
        while chunk := fp.read(size):
419
            crcval = binascii.crc32(chunk, crcval)
420
    return crcval
421
422
423
def get_vocab_id(config_path):
424
    config = configparser.ConfigParser()
425
    config.read(config_path)
426
    section = config.sections()[0]
427
    return config[section]["vocab"]
428
429
430
def _get_completion_choices(
431
    param: Argument,
432
) -> dict[str, AnnifVocabulary] | dict[str, AnnifProject] | list:
433
    if param.name == "project_id":
434
        return annif.registry.get_projects()
435
    elif param.name == "vocab_id":
436
        return annif.registry.get_vocabs()
437
    else:
438
        return []
439
440
441
def complete_param(ctx: Context, param: Argument, incomplete: str) -> list[str]:
442
    with ctx.obj.load_app().app_context():
443
        return [
444
            choice
445
            for choice in _get_completion_choices(param)
446
            if choice.startswith(incomplete)
447
        ]
448