Passed
Pull Request — master (#547)
by Konstantin
01:39
created

ocrd.cli.bashlib.bashlib_constants()   B

Complexity

Conditions 7

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 22
rs 8
c 0
b 0
f 0
cc 7
nop 1
1
from __future__ import print_function
2
import sys
3
import click
4
5
from ocrd.constants import BASHLIB_FILENAME
6
import ocrd.constants
7
import ocrd_utils.constants
8
import ocrd_models.constants
9
import ocrd_validators.constants
10
11
# ----------------------------------------------------------------------
12
# ocrd bashlib
13
# ----------------------------------------------------------------------
14
15
@click.group('bashlib')
16
def bashlib_cli():
17
    """
18
    Work with bash library
19
    """
20
21
# ----------------------------------------------------------------------
22
# ocrd bashlib filename
23
# ----------------------------------------------------------------------
24
25
@bashlib_cli.command('filename')
26
def bashlib_filename():
27
    """
28
    Dump the bash library filename for sourcing by shell scripts
29
    """
30
    print(BASHLIB_FILENAME)
31
32
@bashlib_cli.command('constants')
33
@click.argument('name')
34
def bashlib_constants(name):
35
    """
36
    Query constants from ocrd_utils and ocrd_models
37
    """
38
    all_constants = {}
39
    for src in [ocrd.constants, ocrd_utils.constants, ocrd_models.constants, ocrd_validators.constants]:
40
        for k in src.__all__:
41
            all_constants[k] = src.__dict__[k]
42
    if name in ['*', 'KEYS', '__all__']:
43
        print(sorted(all_constants.keys()))
44
    if name not in all_constants:
45
        print("ERROR: name '%s' is not a known constant" % name, file=sys.stderr)
46
        sys.exit(1)
47
    val = all_constants[name]
48
    if isinstance(val, dict):
49
        # make this bash-friendly (show initialization for associative array)
50
        for key in val:
51
            print("[%s]=%s" % (key, val[key]), end=' ')
52
    else:
53
        print(val)
54