Coverage for pygeoapi/process/hello_world.py: 60%
21 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# Francesco Martinelli <francesco.martinelli@ingv.it>
5#
6# Copyright (c) 2022 Tom Kralidis
7# Copyright (c) 2024 Francesco Martinelli
8#
9# Permission is hereby granted, free of charge, to any person
10# obtaining a copy of this software and associated documentation
11# files (the "Software"), to deal in the Software without
12# restriction, including without limitation the rights to use,
13# copy, modify, merge, publish, distribute, sublicense, and/or sell
14# copies of the Software, and to permit persons to whom the
15# Software is furnished to do so, subject to the following
16# conditions:
17#
18# The above copyright notice and this permission notice shall be
19# included in all copies or substantial portions of the Software.
20#
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28# OTHER DEALINGS IN THE SOFTWARE.
29#
30# =================================================================
32import logging
34from pygeoapi.process.base import BaseProcessor, ProcessorExecuteError
37LOGGER = logging.getLogger(__name__)
39#: Process metadata and description
40PROCESS_METADATA = {
41 'version': '0.2.0',
42 'id': 'hello-world',
43 'title': {
44 'en': 'Hello World',
45 'fr': 'Bonjour le Monde'
46 },
47 'description': {
48 'en': 'An example process that takes a name as input, and echoes '
49 'it back as output. Intended to demonstrate a simple '
50 'process with a single literal input.',
51 'fr': 'Un exemple de processus qui prend un nom en entrée et le '
52 'renvoie en sortie. Destiné à démontrer un processus '
53 'simple avec une seule entrée littérale.',
54 },
55 'jobControlOptions': ['sync-execute', 'async-execute'],
56 'keywords': ['hello world', 'example', 'echo'],
57 'links': [{
58 'type': 'text/html',
59 'rel': 'about',
60 'title': 'information',
61 'href': 'https://example.org/process',
62 'hreflang': 'en-US'
63 }],
64 'inputs': {
65 'name': {
66 'title': 'Name',
67 'description': 'The name of the person or entity that you wish to'
68 'be echoed back as an output',
69 'schema': {
70 'type': 'string'
71 },
72 'minOccurs': 1,
73 'maxOccurs': 1,
74 'keywords': ['full name', 'personal']
75 },
76 'message': {
77 'title': 'Message',
78 'description': 'An optional message to echo as well',
79 'schema': {
80 'type': 'string'
81 },
82 'minOccurs': 0,
83 'maxOccurs': 1,
84 'keywords': ['message']
85 }
86 },
87 'outputs': {
88 'echo': {
89 'title': 'Hello, world',
90 'description': 'A "hello world" echo with the name and (optional)'
91 ' message submitted for processing',
92 'schema': {
93 'type': 'object',
94 'contentMediaType': 'application/json'
95 }
96 }
97 },
98 'example': {
99 'inputs': {
100 'name': 'World',
101 'message': 'An optional message.',
102 }
103 }
104}
107class HelloWorldProcessor(BaseProcessor):
108 """Hello World Processor example"""
110 def __init__(self, processor_def):
111 """
112 Initialize object
114 :param processor_def: provider definition
116 :returns: pygeoapi.process.hello_world.HelloWorldProcessor
117 """
119 super().__init__(processor_def, PROCESS_METADATA)
120 self.supports_outputs = True
122 def execute(self, data, outputs=None):
123 mimetype = 'application/json'
124 name = data.get('name')
126 if name is None: 126 ↛ 129line 126 didn't jump to line 129 because the condition on line 126 was always true
127 raise ProcessorExecuteError('Cannot process without a name')
129 message = data.get('message', '')
130 value = f'Hello {name}! {message}'.strip()
132 produced_outputs = {}
133 if not bool(outputs) or 'echo' in outputs:
134 produced_outputs = {
135 'id': 'echo',
136 'value': value
137 }
139 return mimetype, produced_outputs
141 def __repr__(self):
142 return f'<HelloWorldProcessor> {self.name}'