spaceapi-server/examples/plugins/example.py

41 lines
1.2 KiB
Python
Raw Normal View History

2019-11-25 02:48:12 +01:00
from spaceapi_server import config, plugins
@plugins.template_function
def example_function(name: str):
2019-11-25 04:10:20 +01:00
"""
2019-11-25 02:48:12 +01:00
This function is registered as a Jinja2 function. It can be used like this:
{{ example_function('the Spanish Inquisition') }}
2019-11-25 04:10:20 +01:00
"""
2019-11-25 02:48:12 +01:00
return f'Nobody expects {name}'
@plugins.template_filter
def example_filter(name: str):
2019-11-25 04:10:20 +01:00
"""
2019-11-25 02:48:12 +01:00
This function is registered as a Jinja2 filter. It can be used like this:
{{ 'the Spanish Inquisition' | example_filter }}
2019-11-25 04:10:20 +01:00
"""
2019-11-25 02:48:12 +01:00
return f'Nobody expects {name}'
@plugins.template_test
def example_test(name: str):
2019-11-25 04:10:20 +01:00
"""
2019-11-25 02:48:12 +01:00
This function is registered as a Jinja2 test. It can be used like this:
{% if 'the Spanish Inquisition' is example_test %}
2019-11-25 04:10:20 +01:00
"""
2019-11-25 02:48:12 +01:00
# Config lookup example. A plugin's config should be below
# `.plugins[plugin_name]` (JSONPath)
# Get the .plugins.example dict
2019-11-25 04:10:20 +01:00
conf = config.get_plugin_config('example')
2019-11-25 02:48:12 +01:00
# Get the .test_value property from the plugin config, falling
# back to a default value
test_value = conf.get('test_value', 'the Spanish Inquisition')
# Tests must always return a boolean or boolean-castable
# expression
return name == test_value