Remove template_filter decorator, as there really isn't any use case for it.

This commit is contained in:
s3lph 2019-11-30 04:49:01 +01:00
parent f6ad53555c
commit 138234c4a3
6 changed files with 23 additions and 76 deletions

View file

@ -126,9 +126,8 @@ the space as open depending on its existence.
The `@template_function` decorator registers the function as a
callable in Jinja's globals. There's also `@template_filter`,
which registers a Jinja2 filter, and `@template_test`, which
registers a test. For more information on the Jinja2 templating
engine, see [Jinja2][jinja].
which registers a Jinja2 filter. For more information on the
Jinja2 templating engine, see [Jinja2][jinja].
2. Call the template function in your template:
@ -205,9 +204,9 @@ of the global configuration file, or an empty object if absent.
Usage:
```python
from spaceapi_server import config
from spaceapi_server import plugins
print(config.get_plugin_config('my_plugin'))
print(plugins.get_plugin_config('my_plugin'))
```
### Templating
@ -286,33 +285,6 @@ def lookup_sensor(query, default=None):
}
```
#### `spaceapi_server.plugins.template_test`
This decorator registers a function as a **test** in the Jinja2
environment. The decorated function must take exactly one argument.
The decorated function may take any argument that can be represented
in a Jinja2 template.
The decorated function must return either `True` or `False`.
Usage:
```python
from spaceapi_server import plugins
@plugins.template_test
def easteregg(expr):
return expr == 23
```
```json
{
# ...
message": "{% if people_present() is easteregg -%} Nobody expects the Spanish Iniqusition! {%- endif %}"
# ...
}
```
[master]: https://gitlab.com/s3lph/spaceapi-server/commits/master
[releases]: https://gitlab.com/s3lph/spaceapi-server/-/releases

View file

@ -1,5 +1,5 @@
from spaceapi_server import config, plugins
from spaceapi_server import plugins
@plugins.template_function
@ -20,21 +20,17 @@ def example_filter(name: str):
return f'Nobody expects {name}'
@plugins.template_test
def example_test(name: str):
@plugins.template_function
def example_config_function():
"""
This function is registered as a Jinja2 test. It can be used like this:
{% if 'the Spanish Inquisition' is example_test %}
This function demonstrates the use of configuration.
{( example_config_function() }}
"""
# Config lookup example. A plugin's config should be below
# `.plugins[plugin_name]` (JSONPath)
# Get the .plugins.example dict
conf = config.get_plugin_config('example')
conf = plugins.get_plugin_config('example')
# 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
return conf.get('test_value', 'the Spanish Inquisition')

View file

@ -23,12 +23,3 @@ def get() -> dict:
"""
global __CONFIG
return __CONFIG
def get_plugin_config(name: str):
"""
Return a plugin's configuration under .plugins[name]
:param name: The plugin name.
"""
global __CONFIG
return __CONFIG.get('plugins', {}).get(name, {})

View file

@ -2,7 +2,8 @@
import unittest
import tempfile
from spaceapi_server.config import load, get, get_plugin_config
from spaceapi_server.config import load, get
from spaceapi_server.plugins import get_plugin_config
class ConfigTest(unittest.TestCase):

View file

@ -1,5 +1,5 @@
from spaceapi_server.template import _env_init
from spaceapi_server import config, template
def template_function(fn):
@ -8,7 +8,7 @@ def template_function(fn):
:param fn: The function to register.
"""
# Make sure the Jinja2 environment is initialized
env = _env_init()
env = template._env_init()
# Add the function to the environment's globals
env.globals[fn.__name__] = fn
return fn
@ -20,19 +20,15 @@ def template_filter(fn):
:param fn: The function to register.
"""
# Make sure the Jinja2 environment is initialized
env = _env_init()
env = template._env_init()
# Add the function to the environment's filters
env.filters[fn.__name__] = fn
return fn
def template_test(fn):
def get_plugin_config(name: str):
"""
Register the decorated function as a template test.
:param fn: The function to register.
Return a plugin's configuration under .plugins[name]
:param name: The plugin name.
"""
# Make sure the Jinja2 environment is initialized
env = _env_init()
# Add the function to the environment's tests
env.tests[fn.__name__] = fn
return fn
return config.get().get('plugins', {}).get(name, {})

View file

@ -2,7 +2,7 @@
import unittest
from spaceapi_server.template import _env_init, render, render_traverse
from spaceapi_server.plugins import template_function, template_filter, template_test
from spaceapi_server.plugins import template_function, template_filter
class TemplateTest(unittest.TestCase):
@ -17,11 +17,6 @@ class TemplateTest(unittest.TestCase):
def template_test_filter(value, other):
return f'test_{other}_{value}'
@staticmethod
@template_test
def template_test_test(value):
return value == 'baz'
@staticmethod
@template_function
def template_test_function_nocache():
@ -55,9 +50,7 @@ class TemplateTest(unittest.TestCase):
'builtin': '{{ [ 1337, 42 ] | first }}',
'test_functions': {
'test_function': '{{ template_test_function("foo") }}',
'test_filter': '{{ "bar" | template_test_filter("other") }}',
'test_test_true': '{{ "baz" is template_test_test }}',
'test_test_false': '{{ "foo" is template_test_test }}'
'test_filter': '{{ "bar" | template_test_filter("other") }}'
}
}
rendered = render_traverse(template)
@ -67,8 +60,6 @@ class TemplateTest(unittest.TestCase):
self.assertEqual(1337, rendered['builtin'])
self.assertEqual('test_foo', rendered['test_functions']['test_function'])
self.assertEqual('test_other_bar', rendered['test_functions']['test_filter'])
self.assertTrue(rendered['test_functions']['test_test_true'])
self.assertFalse(rendered['test_functions']['test_test_false'])
def test_no_cache(self):
template = ['{{ template_test_function_nocache() }}']