Spaces:
Build error
Build error
Download tapshex/cli.py from dcmi/dctap2shex: direct link, hf CLI and curl.
- Browser
- Download file 3.37 kB
-
https://huggingface.co/spaces/dcmi/dctap2shex/resolve/main/tapshex/cli.py
- Command line
-
hf download hf://spaces/dcmi/dctap2shex/tapshex/cli.py
-
curl -L -o cli.py https://huggingface.co/spaces/dcmi/dctap2shex/resolve/main/tapshex/cli.py
3.37 kB
| """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 | |
| 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 | |
| """ | |
| def init(context): | |
| """Write config file [default: tapshex.yaml].""" | |
| configfile = DEFAULT_CONFIGFILE | |
| write_configfile(configfile) | |
| 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"]) | |