Passed
Pull Request — master (#559)
by Konstantin
02:18
created

ocrd.cli.resmgr.download()   C

Complexity

Conditions 9

Size

Total Lines 46
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 46
rs 6.6666
c 0
b 0
f 0
cc 9
nop 5
1
import sys
2
from os import getcwd
3
from pathlib import Path
4
5
import click
6
7
from ocrd_utils import (
8
    initLogging,
9
    getLogger,
10
    XDG_CACHE_HOME,
11
    XDG_CONFIG_HOME,
12
    XDG_DATA_HOME
13
)
14
from ocrd_validators import OcrdZipValidator
15
16
from ..resource_manager import OcrdResourceManager
17
18
def print_resources(executable, reslist):
19
    print('%s' % executable)
20
    for resdict in reslist:
21
        print('- %s (%s)\n  %s' % (resdict['name'], resdict['url'], resdict['description']))
22
    print()
23
24
@click.group("resmgr")
25
def resmgr_cli():
26
    """
27
    Managing processor resources
28
    """
29
    initLogging()
30
31
@resmgr_cli.command('list-available')
32
@click.option('-e', '--executable', help='Show only resources for executable EXEC', metavar='EXEC')
33
def list_available(executable=None):
34
    """
35
    List available resources
36
    """
37
    resmgr = OcrdResourceManager()
38
    for executable, reslist in resmgr.list_available(executable):
39
        print_resources(executable, reslist)
40
41
@resmgr_cli.command('list-installed')
42
@click.option('-e', '--executable', help='Show only resources for executable EXEC', metavar='EXEC')
43
def list_installed(executable=None):
44
    """
45
    List installed resources
46
    """
47
    resmgr = OcrdResourceManager()
48
    ret = []
49
    for executable, reslist in resmgr.list_installed(executable):
50
        print_resources(executable, reslist)
51
52
@resmgr_cli.command('download')
53
@click.option('-n', '--any-url', help='Allow downloading unregistered resources', is_flag=True)
54
@click.option('-o', '--overwrite', help='Overwrite existing resources', is_flag=True)
55
@click.option('-l', '--location', help='Where to store resources', type=click.Choice(['cache', 'config', 'data', 'cwd']), default='cache', show_default=True)
56
@click.argument('executable', required=True)
57
@click.argument('url_or_name', required=True)
58
def download(any_url, overwrite, location, executable, url_or_name):
59
    """
60
    Download resource URL_OR_NAME for processor EXECUTABLE.
61
62
    URL_OR_NAME can either be the ``name`` or ``url`` of a registered resource.
63
64
    If ``--any-url`` is given, also accepts URL of non-registered resources for ``URL_OR_NAME``.
65
    """
66
    log = getLogger('ocrd.cli.resmgr')
67
    resmgr = OcrdResourceManager()
68
    basedir = XDG_CACHE_HOME if location == 'cache' else \
69
            XDG_DATA_HOME if location == 'data' else \
70
            XDG_CONFIG_HOME if location == 'config' else \
71
            getcwd()
72
    is_url = url_or_name.startswith('https://') or url_or_name.startswith('http://')
73
    find_kwargs = {'executable': executable}
74
    find_kwargs['url' if is_url else 'name'] = url_or_name
75
    reslist = resmgr.find_resources(**find_kwargs)
76
    if not reslist:
77
        log.info("No resources found in registry")
78
        if is_url and any_url:
79
            log.info("Downloading unregistered resource %s" % url_or_name)
80
            fpath = resmgr.download(executable, url_or_name, overwrite=overwrite, basedir=basedir)
81
            log.info("Downloaded %s to %s" % (url_or_name, fpath))
82
            log.info("Use in parameters as '%s'" % fpath.name)
83
        else:
84
            sys.exit(1)
85
    else:
86
        for _, resdict in reslist:
87
            fpath = resmgr.download(
88
                executable,
89
                resdict['url'],
90
                name=resdict['name'],
91
                resource_type=resdict['type'],
92
                path_in_archive=resdict.get('path_in_archive', '.'),
93
                overwrite=overwrite,
94
                basedir=basedir
95
            )
96
            log.info("Downloaded %s to %s" % (resdict['url'], fpath))
97
            log.info("Use in parameters as '%s'" % resmgr.parameter_usage(resdict['name'], usage=resdict['parameter_usage']))
98
99