Skip to content

Commit

Permalink
[#56] Use profiles from config in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed May 22, 2024
1 parent 48b5e61 commit c27a456
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions ckanext/dcat/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
import ckan.plugins.toolkit as tk

import ckanext.dcat.utils as utils
from ckanext.dcat.processors import RDFParser, RDFSerializer, DEFAULT_RDF_PROFILES
from ckanext.dcat.processors import (
RDFParser,
RDFSerializer,
DEFAULT_RDF_PROFILES,
RDF_PROFILES_CONFIG_OPTION,
)


@click.group()
Expand All @@ -24,6 +29,17 @@ def generate_static(output):
utils.generate_static_json(output)


def _get_profiles(profiles):
if profiles:
profiles = profiles.split()
elif tk.config.get(RDF_PROFILES_CONFIG_OPTION):
profiles = tk.aslist(tk.config[RDF_PROFILES_CONFIG_OPTION])
else:
profiles = None

return profiles


@dcat.command(context_settings={"show_default": True})
@click.argument("input", type=click.File(mode="r"))
@click.option(
Expand All @@ -40,8 +56,8 @@ def generate_static(output):
@click.option(
"-p",
"--profiles",
default=" ".join(DEFAULT_RDF_PROFILES),
help="RDF profiles to use",
help=f"RDF profiles to use. If not provided will be read from config, "
"if not present there, the default will be used: {DEFAULT_RDF_PROFILES}",
)
@click.option(
"-P", "--pretty", is_flag=True, help="Make the output more human readable"
Expand All @@ -63,8 +79,8 @@ def consume(input, output, format, profiles, pretty, compat_mode):
"""
contents = input.read()

if profiles:
profiles = profiles.split()
profiles = _get_profiles(profiles)

parser = RDFParser(profiles=profiles, compatibility_mode=compat_mode)
parser.parse(contents, _format=format)

Expand Down Expand Up @@ -92,8 +108,8 @@ def consume(input, output, format, profiles, pretty, compat_mode):
@click.option(
"-p",
"--profiles",
default=" ".join(DEFAULT_RDF_PROFILES),
help="RDF profiles to use",
help=f"RDF profiles to use. If not provided will be read from config, "
"if not present there, the default will be used: {DEFAULT_RDF_PROFILES}",
)
@click.option(
"-m", "--compat_mode", is_flag=True, help="Compatibility mode (deprecated)"
Expand All @@ -112,12 +128,9 @@ def produce(input, output, format, profiles, compat_mode):
"""
contents = input.read()

if profiles:
profiles = profiles.split()
serializer = RDFSerializer(
profiles=profiles,
compatibility_mode=compat_mode
)
profiles = _get_profiles(profiles)

serializer = RDFSerializer(profiles=profiles, compatibility_mode=compat_mode)

dataset = json.loads(contents)
if isinstance(dataset, list):
Expand Down

0 comments on commit c27a456

Please sign in to comment.