-
-
Notifications
You must be signed in to change notification settings - Fork 306
fix support for custom formatters (#1997) #2144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tomkralidis
wants to merge
2
commits into
master
Choose a base branch
from
formatters
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−45
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |
| # Colin Blackburn <[email protected]> | ||
| # Ricardo Garcia Silva <[email protected]> | ||
| # | ||
| # Copyright (c) 2025 Tom Kralidis | ||
| # Copyright (c) 2026 Tom Kralidis | ||
| # Copyright (c) 2025 Francesco Bartoli | ||
| # Copyright (c) 2022 John A Stevenson and Colin Blackburn | ||
| # Copyright (c) 2023 Ricardo Garcia Silva | ||
|
|
@@ -55,13 +55,15 @@ | |
| set_content_crs_header) | ||
| from pygeoapi.formatter.base import FormatterSerializationError | ||
| from pygeoapi.linked_data import geojson2jsonld | ||
| from pygeoapi.openapi import get_oas_30_parameters | ||
| from pygeoapi.plugin import load_plugin, PLUGINS | ||
| from pygeoapi.provider.base import ( | ||
| ProviderGenericError, ProviderTypeError, SchemaType) | ||
|
|
||
| from pygeoapi.util import (filter_providers_by_type, to_json, | ||
| filter_dict_by_key_value, str2bool, | ||
| get_provider_by_type, render_j2_template) | ||
| get_provider_by_type, render_j2_template, | ||
| get_dataset_formatters) | ||
|
|
||
| from . import ( | ||
| APIRequest, API, SYSTEM_LOCALE, F_JSON, FORMAT_TYPES, F_HTML, F_JSONLD, | ||
|
|
@@ -241,9 +243,6 @@ def get_collection_items( | |
| :returns: tuple of headers, status code, content | ||
| """ | ||
|
|
||
| if not request.is_valid(PLUGINS['formatter'].keys()): | ||
| return api.get_format_exception(request) | ||
|
|
||
| # Set Content-Language to system locale until provider locale | ||
| # has been determined | ||
| headers = request.get_response_headers(SYSTEM_LOCALE, | ||
|
|
@@ -352,6 +351,12 @@ def get_collection_items( | |
| err.http_status_code, headers, request.format, | ||
| err.ogc_exception_code, err.message) | ||
|
|
||
| LOGGER.debug('Validating requested format') | ||
| dataset_formatters = get_dataset_formatters(collections[dataset]) | ||
|
|
||
| if not request.is_valid(dataset_formatters.keys()): | ||
| return api.get_format_exception(request) | ||
|
|
||
| crs_transform_spec = None | ||
| if provider_type == 'feature': | ||
| # crs query parameter is only available for OGC API - Features | ||
|
|
@@ -581,6 +586,14 @@ def get_collection_items( | |
| 'href': f'{uri}?f={F_HTML}{serialized_query_params}' | ||
| }]) | ||
|
|
||
| for key, value in dataset_formatters.items(): | ||
| content['links'].append({ | ||
| 'type': value.mimetype, | ||
| 'rel': 'alternate', | ||
| 'title': f'This document as {key}', | ||
| 'href': f'{uri}?f={value.name}{serialized_query_params}' | ||
| }) | ||
|
|
||
| next_link = False | ||
| prev_link = False | ||
|
|
||
|
|
@@ -656,9 +669,9 @@ def get_collection_items( | |
| 'collections/items/index.html', | ||
| content, request.locale) | ||
| return headers, HTTPStatus.OK, content | ||
| elif request.format == 'csv': # render | ||
| formatter = load_plugin('formatter', | ||
| {'name': 'CSV', 'geom': True}) | ||
| elif request.format in [df.f for df in dataset_formatters.values()]: | ||
| formatter = [v for v in dataset_formatters.values() if | ||
| v.f == request.format][0] | ||
|
|
||
| try: | ||
| content = formatter.write( | ||
|
|
@@ -677,13 +690,14 @@ def get_collection_items( | |
|
|
||
| headers['Content-Type'] = formatter.mimetype | ||
|
|
||
| if p.filename is None: | ||
| filename = f'{dataset}.csv' | ||
| else: | ||
| filename = f'{p.filename}' | ||
| if formatter.attachment: | ||
| if p.filename is None: | ||
| filename = f'{dataset}.{formatter.extension}' | ||
| else: | ||
| filename = f'{p.filename}' | ||
|
|
||
| cd = f'attachment; filename="{filename}"' | ||
| headers['Content-Disposition'] = cd | ||
| cd = f'attachment; filename="{filename}"' | ||
| headers['Content-Disposition'] = cd | ||
|
|
||
| return headers, HTTPStatus.OK, content | ||
|
|
||
|
|
@@ -1073,14 +1087,19 @@ def get_oas_30(cfg: dict, locale: str) -> tuple[list[dict[str, str]], dict[str, | |
| v.get('limits', {}) | ||
| ) | ||
|
|
||
| dataset_formatters = get_dataset_formatters(v) | ||
| coll_f_parameter = deepcopy(get_oas_30_parameters(cfg, locale))['f'] # noqa | ||
| for key, value in dataset_formatters.items(): | ||
| coll_f_parameter['schema']['enum'].append(value.f) | ||
|
|
||
| paths[items_path] = { | ||
| 'get': { | ||
| 'summary': f'Get {title} items', | ||
| 'description': description, | ||
| 'tags': [k], | ||
| 'operationId': f'get{k.capitalize()}Features', | ||
| 'parameters': [ | ||
| {'$ref': '#/components/parameters/f'}, | ||
| coll_f_parameter, | ||
| {'$ref': '#/components/parameters/lang'}, | ||
| {'$ref': '#/components/parameters/bbox'}, | ||
| coll_limit, | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| # | ||
| # Authors: Tom Kralidis <[email protected]> | ||
| # | ||
| # Copyright (c) 2022 Tom Kralidis | ||
| # Copyright (c) 2026 Tom Kralidis | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person | ||
| # obtaining a copy of this software and associated documentation | ||
|
|
@@ -39,23 +39,27 @@ def __init__(self, formatter_def: dict): | |
| """ | ||
| Initialize object | ||
|
|
||
| :param formatter_def: formatter definition | ||
| param formatter_def: formatter definition | ||
|
|
||
| :returns: pygeoapi.formatter.base.BaseFormatter | ||
| """ | ||
|
|
||
| self.extension = None | ||
| self.mimetype = None | ||
| self.geom = False | ||
|
|
||
| self.name = formatter_def['name'] | ||
| if 'geom' in formatter_def: | ||
| self.geom = formatter_def['geom'] | ||
| try: | ||
| self.name = formatter_def['name'] | ||
| except KeyError: | ||
| raise RuntimeError('name is required') | ||
|
|
||
| self.geom = formatter_def.get('geom', False) | ||
| self.attachment = formatter_def.get('attachment', False) | ||
|
|
||
| def write(self, options: dict = {}, data: dict | None = None) -> str: | ||
| """ | ||
| Generate data in specified format | ||
|
|
||
| :param options: CSV formatting options | ||
| :param options: formatting options | ||
| :param data: dict representation of GeoJSON object | ||
|
|
||
| :returns: string representation of format | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the formatter need to response both
f=and theAcceptHeader?