Passed
Pull Request — master (#559)
by Konstantin
01:49
created

ocrd.cli.resmgr.resmgr_cli()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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