Coverage for pygeoapi/__init__.py: 48%
53 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 08:15 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 08:15 +0000
1# =================================================================
2#
3# Authors: Tom Kralidis <tomkralidis@gmail.com>
4# Ricardo Garcia Silva <ricardo.garcia.silva@geobeyond.it>
5#
6# Copyright (c) 2026 Tom Kralidis
7# Copyright (c) 2023 Ricardo Garcia Silva
8# Copyright (c) 2025 Angelos Tzotsos
9#
10# Permission is hereby granted, free of charge, to any person
11# obtaining a copy of this software and associated documentation
12# files (the "Software"), to deal in the Software without
13# restriction, including without limitation the rights to use,
14# copy, modify, merge, publish, distribute, sublicense, and/or sell
15# copies of the Software, and to permit persons to whom the
16# Software is furnished to do so, subject to the following
17# conditions:
18#
19# The above copyright notice and this permission notice shall be
20# included in all copies or substantial portions of the Software.
21#
22# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
24# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29# OTHER DEALINGS IN THE SOFTWARE.
30#
31# =================================================================
33__version__ = '0.23.5'
35import click
36try:
37 # importlib.metadata is part of Python's standard library from 3.8
38 from importlib.metadata import entry_points
39except ImportError:
40 from importlib_metadata import entry_points
42from pygeoapi.asyncapi import asyncapi
43from pygeoapi.config import config
44from pygeoapi.openapi import openapi
47def _find_plugins():
48 """
49 A decorator to find pygeoapi CLI plugins provided by third-party packages.
51 pygeoapi plugins can hook into the pygeoapi CLI by providing their CLI
52 functions and then using an entry_point named 'pygeoapi'.
53 """
55 def decorator(click_group):
56 try:
57 found_entrypoints = entry_points(group="pygeoapi")
58 except TypeError:
59 # earlier versions of importlib_metadata did not have the
60 # `group` kwarg. More detail:
61 #
62 # https://github.com/geopython/pygeoapi/issues/1241#issuecomment-1536128897 # noqa: E501
63 for group, entries in entry_points().items():
64 if group == "pygeoapi":
65 found_entrypoints = entries
66 break
67 else:
68 found_entrypoints = []
69 for entry_point in found_entrypoints: 69 ↛ 70line 69 didn't jump to line 70 because the loop on line 69 never started
70 try:
71 click_group.add_command(entry_point.load())
72 except Exception as err:
73 click.echo(err)
74 return click_group
76 return decorator
79@click.group()
80@click.version_option(version=__version__)
81def cli():
82 pass
85@_find_plugins()
86@cli.group()
87def plugins():
88 """Additional commands provided by third-party pygeoapi plugins"""
89 pass
92@cli.command()
93@click.option('--flask', 'server', flag_value="flask", default=True)
94@click.option('--starlette', 'server', flag_value="starlette")
95@click.option('--django', 'server', flag_value="django")
96@click.pass_context
97def serve(ctx, server):
98 """Run the server with different daemon type (--flask is the default)"""
100 if server == "flask":
101 from pygeoapi.flask_app import serve as serve_flask
102 ctx.invoke(serve_flask)
103 elif server == "starlette":
104 from pygeoapi.starlette_app import serve as serve_starlette
105 ctx.invoke(serve_starlette)
106 elif server == "django":
107 from pygeoapi.django_app import main as serve_django
108 ctx.invoke(serve_django)
109 else:
110 raise click.ClickException('--flask/--starlette/--django is required')
113cli.add_command(config)
114cli.add_command(openapi)
115cli.add_command(asyncapi)