-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #279 from ckan/new-cli
New `ckan dcat consume` and `ckan dcat produce` commands
- Loading branch information
Showing
4 changed files
with
186 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,132 @@ | ||
# -*- coding: utf-8 -*- | ||
import json | ||
|
||
import click | ||
|
||
import ckan.plugins.toolkit as tk | ||
|
||
import ckanext.dcat.utils as utils | ||
from ckanext.dcat.processors import RDFParser, RDFSerializer, DEFAULT_RDF_PROFILES | ||
|
||
@click.group() | ||
def generate_static(): | ||
"""Generates static files containing all datasets. | ||
|
||
""" | ||
@click.group() | ||
def dcat(): | ||
"""DCAT utilities for CKAN""" | ||
pass | ||
|
||
@generate_static.command() | ||
@click.argument('output', type=click.File(mode="w")) | ||
def json(output): | ||
"""The generate command will generate a static file containing all of | ||
the datasets in the catalog in JSON format. | ||
|
||
@dcat.command() | ||
@click.argument("output", type=click.File(mode="w")) | ||
def generate_static(output): | ||
"""[Deprecated] Generate a static datasets file in JSON format | ||
(requires the dcat_json_interface plugin) . | ||
""" | ||
utils.generate_static_json(output) | ||
|
||
|
||
@dcat.command(context_settings={"show_default": True}) | ||
@click.argument("input", type=click.File(mode="r")) | ||
@click.option( | ||
"-o", | ||
"--output", | ||
type=click.File(mode="w"), | ||
default="-", | ||
help="By default the command will output the result to stdin, " | ||
"alternatively you can provide a file path with this option", | ||
) | ||
@click.option( | ||
"-f", "--format", default="xml", help="Serialization format (eg ttl, jsonld)" | ||
) | ||
@click.option( | ||
"-p", | ||
"--profiles", | ||
default=" ".join(DEFAULT_RDF_PROFILES), | ||
help="RDF profiles to use", | ||
) | ||
@click.option( | ||
"-P", "--pretty", is_flag=True, help="Make the output more human readable" | ||
) | ||
@click.option( | ||
"-m", "--compat_mode", is_flag=True, help="Compatibility mode (deprecated)" | ||
) | ||
def consume(input, output, format, profiles, pretty, compat_mode): | ||
""" | ||
Parses DCAT RDF graphs into CKAN dataset JSON objects. | ||
The input serializations can be provided as a path to a file, e.g.: | ||
ckan dcat consume examples/dataset.ttl | ||
Or be read from stdin: | ||
ckan dcat consume - | ||
""" | ||
contents = input.read() | ||
|
||
if profiles: | ||
profiles = profiles.split() | ||
parser = RDFParser(profiles=profiles, compatibility_mode=compat_mode) | ||
parser.parse(contents, _format=format) | ||
|
||
ckan_datasets = [d for d in parser.datasets()] | ||
|
||
indent = 4 if pretty else None | ||
out = json.dumps(ckan_datasets, indent=indent) | ||
|
||
output.write(out) | ||
|
||
|
||
@dcat.command(context_settings={"show_default": True}) | ||
@click.argument("input", type=click.File(mode="r")) | ||
@click.option( | ||
"-o", | ||
"--output", | ||
type=click.File(mode="w"), | ||
default="-", | ||
help="By default the command will output the result to stdin, " | ||
"alternatively you can provide a file path with this option", | ||
) | ||
@click.option( | ||
"-f", "--format", default="xml", help="Serialization format (eg ttl, jsonld)" | ||
) | ||
@click.option( | ||
"-p", | ||
"--profiles", | ||
default=" ".join(DEFAULT_RDF_PROFILES), | ||
help="RDF profiles to use", | ||
) | ||
@click.option( | ||
"-m", "--compat_mode", is_flag=True, help="Compatibility mode (deprecated)" | ||
) | ||
def produce(input, output, format, profiles, compat_mode): | ||
""" | ||
Transforms CKAN dataset JSON objects into DCAT RDF serializations. | ||
The input datasets can be provided as a path to a file, e.g.: | ||
ckan dcat consume examples/ckan_dataset.json | ||
Or be read from stdin: | ||
ckan dcat produce - | ||
""" | ||
contents = input.read() | ||
|
||
if profiles: | ||
profiles = profiles.split() | ||
serializer = RDFSerializer( | ||
profiles=profiles, | ||
compatibility_mode=compat_mode | ||
) | ||
|
||
dataset = json.loads(contents) | ||
if isinstance(dataset, list): | ||
out = serializer.serialize_datasets(dataset, _format=format) | ||
else: | ||
out = serializer.serialize_dataset(dataset, _format=format) | ||
|
||
output.write(out) | ||
|
||
|
||
def get_commands(): | ||
return [generate_static] | ||
return [dcat] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import json | ||
import os | ||
|
||
from ckanext.dcat.cli import dcat as dcat_cli | ||
|
||
|
||
def test_consume(cli): | ||
|
||
path = os.path.join( | ||
os.path.dirname(__file__), "..", "..", "..", "examples", "dataset_afs.ttl" | ||
) | ||
|
||
result = cli.invoke(dcat_cli, ["consume", "-f", "ttl", path]) | ||
assert result.exit_code == 0 | ||
|
||
assert json.loads(result.stdout)[0]["title"] == "A test dataset on your catalogue" | ||
|
||
|
||
def test_produce(cli): | ||
|
||
path = os.path.join( | ||
os.path.dirname(__file__), | ||
"..", | ||
"..", | ||
"..", | ||
"examples", | ||
"full_ckan_dataset.json", | ||
) | ||
|
||
result = cli.invoke(dcat_cli, ["produce", "-f", "jsonld", path]) | ||
assert result.exit_code == 0 | ||
|
||
assert json.loads(result.stdout)["@context"]["dcat"] == "http://www.w3.org/ns/dcat#" |