File size: 3,369 Bytes
64940d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""DC Tabular Application Profiles (DCTAP) to ShEx

Modeled on https://github.com/dcmi/dctap-python/blob/main/dctap/cli.py
"""

import sys
import json
import click
from pathlib import Path
from tapshex.defaults import DEFAULT_CONFIGFILE
from tapshex.config import tapshex_config
from tapshex.csvreader import dctap_csvreader
from tapshex.shexify import tapdict_to_shexc
from tapshex.template import SHEX_JINJA
from dctap.config import get_config, write_configfile
from dctap.csvreader import csvreader
from dctap.inspect import pprint_tapshapes, print_warnings
from dctap.loggers import stderr_logger
from dctap.utils import expand_uri_prefixes

# pylint: disable=unused-argument,no-value-for-parameter
# => unused-argument: Allows placeholders for now.
# => no-value-for-parameter: Okay in cli.py


@click.group()
@click.version_option("0.2.3", help="Show version and exit")
@click.help_option(help="Show help and exit")
@click.pass_context
def cli(context):
    """Generate ShEx schema from a Tabular Application Profile (TAP).

    Examples (see https://tapshex.rtfd.io):

    \b
    Write starter config file:
    $ tapshex init                     # write dctap.yaml
    \b
    Show normalized view of TAP:
    $ tapshex parse x.csv              # output plain text
    $ tapshex parse --tapjson x.csv       # output TAP/JSON
    $ tapshex parse --shexc x.csv      # output TAP/ShExC
    $ tapshex parse --warnings x.csv   # also show warnings
    """


@cli.command()
@click.help_option(help="Show help and exit")
@click.pass_context
def init(context):
    """Write config file [default: tapshex.yaml]."""
    configfile = DEFAULT_CONFIGFILE
    write_configfile(configfile)


@cli.command()
@click.argument("csvfile", type=click.Path(exists=True))
@click.option("--config", type=click.Path(exists=True), help="Nondefault config file")
@click.option("--uris", is_flag=True, help="Expand compact URIs")
@click.option("--warnings", is_flag=True, help="Also show warnings")
@click.option("--tapjson", is_flag=True, help="View as TAP/JSON")
@click.option("--shexc", is_flag=True, help="View as TAP/ShExC")
@click.help_option(help="Show help and exit")
@click.pass_context
def parse(context, csvfile, config, uris, warnings, tapjson, shexc):
    """View TAP/TXT (default), TAP/JSON, or ShExC, optionally with warnings."""
    # pylint: disable=too-many-locals,too-many-arguments

    csvfile_str = Path(csvfile).read_text()
    if config:
        config_dict = tapshex_config(configfile_name=config)
    else:
        config_dict = tapshex_config()

    tapshapes_dict = dctap_csvreader(
        csvfile_str=csvfile_str,
        config_dict=config_dict
    )
    if uris:
        tapshapes_dict = expand_uri_prefixes(tapshapes_dict, config_dict)

    if tapjson:
        if not warnings:
            del tapshapes_dict["warnings"]
        json_output = json.dumps(tapshapes_dict, indent=2)
        print(json_output)
    elif shexc:
        shexc_output = tapdict_to_shexc(
            dctap_as_dict=tapshapes_dict, 
            shex_template=SHEX_JINJA,
        )
        for line in shexc_output.splitlines():
            print(line)
    else:
        pprint_output = pprint_tapshapes(tapshapes_dict, config_dict)
        for line in pprint_output:
            print(line, file=sys.stdout)
        if warnings:
            print_warnings(tapshapes_dict["warnings"])