Remove template_filter decorator, as there really isn't any use case for it.
This commit is contained in:
parent
f6ad53555c
commit
138234c4a3
6 changed files with 23 additions and 76 deletions
36
README.md
36
README.md
|
@ -126,9 +126,8 @@ the space as open depending on its existence.
|
||||||
|
|
||||||
The `@template_function` decorator registers the function as a
|
The `@template_function` decorator registers the function as a
|
||||||
callable in Jinja's globals. There's also `@template_filter`,
|
callable in Jinja's globals. There's also `@template_filter`,
|
||||||
which registers a Jinja2 filter, and `@template_test`, which
|
which registers a Jinja2 filter. For more information on the
|
||||||
registers a test. For more information on the Jinja2 templating
|
Jinja2 templating engine, see [Jinja2][jinja].
|
||||||
engine, see [Jinja2][jinja].
|
|
||||||
|
|
||||||
2. Call the template function in your template:
|
2. Call the template function in your template:
|
||||||
|
|
||||||
|
@ -205,9 +204,9 @@ of the global configuration file, or an empty object if absent.
|
||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
```python
|
```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
|
### 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
|
[master]: https://gitlab.com/s3lph/spaceapi-server/commits/master
|
||||||
[releases]: https://gitlab.com/s3lph/spaceapi-server/-/releases
|
[releases]: https://gitlab.com/s3lph/spaceapi-server/-/releases
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
from spaceapi_server import config, plugins
|
from spaceapi_server import plugins
|
||||||
|
|
||||||
|
|
||||||
@plugins.template_function
|
@plugins.template_function
|
||||||
|
@ -20,21 +20,17 @@ def example_filter(name: str):
|
||||||
return f'Nobody expects {name}'
|
return f'Nobody expects {name}'
|
||||||
|
|
||||||
|
|
||||||
@plugins.template_test
|
@plugins.template_function
|
||||||
def example_test(name: str):
|
def example_config_function():
|
||||||
"""
|
"""
|
||||||
This function is registered as a Jinja2 test. It can be used like this:
|
This function demonstrates the use of configuration.
|
||||||
{% if 'the Spanish Inquisition' is example_test %}
|
{( example_config_function() }}
|
||||||
"""
|
"""
|
||||||
# Config lookup example. A plugin's config should be below
|
# Config lookup example. A plugin's config should be below
|
||||||
# `.plugins[plugin_name]` (JSONPath)
|
# `.plugins[plugin_name]` (JSONPath)
|
||||||
|
|
||||||
# Get the .plugins.example dict
|
# 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
|
# Get the .test_value property from the plugin config, falling
|
||||||
# back to a default value
|
# back to a default value
|
||||||
test_value = conf.get('test_value', 'the Spanish Inquisition')
|
return conf.get('test_value', 'the Spanish Inquisition')
|
||||||
|
|
||||||
# Tests must always return a boolean or boolean-castable
|
|
||||||
# expression
|
|
||||||
return name == test_value
|
|
||||||
|
|
|
@ -23,12 +23,3 @@ def get() -> dict:
|
||||||
"""
|
"""
|
||||||
global __CONFIG
|
global __CONFIG
|
||||||
return __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, {})
|
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
import unittest
|
import unittest
|
||||||
import tempfile
|
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):
|
class ConfigTest(unittest.TestCase):
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
from spaceapi_server.template import _env_init
|
from spaceapi_server import config, template
|
||||||
|
|
||||||
|
|
||||||
def template_function(fn):
|
def template_function(fn):
|
||||||
|
@ -8,7 +8,7 @@ def template_function(fn):
|
||||||
:param fn: The function to register.
|
:param fn: The function to register.
|
||||||
"""
|
"""
|
||||||
# Make sure the Jinja2 environment is initialized
|
# Make sure the Jinja2 environment is initialized
|
||||||
env = _env_init()
|
env = template._env_init()
|
||||||
# Add the function to the environment's globals
|
# Add the function to the environment's globals
|
||||||
env.globals[fn.__name__] = fn
|
env.globals[fn.__name__] = fn
|
||||||
return fn
|
return fn
|
||||||
|
@ -20,19 +20,15 @@ def template_filter(fn):
|
||||||
:param fn: The function to register.
|
:param fn: The function to register.
|
||||||
"""
|
"""
|
||||||
# Make sure the Jinja2 environment is initialized
|
# Make sure the Jinja2 environment is initialized
|
||||||
env = _env_init()
|
env = template._env_init()
|
||||||
# Add the function to the environment's filters
|
# Add the function to the environment's filters
|
||||||
env.filters[fn.__name__] = fn
|
env.filters[fn.__name__] = fn
|
||||||
return fn
|
return fn
|
||||||
|
|
||||||
|
|
||||||
def template_test(fn):
|
def get_plugin_config(name: str):
|
||||||
"""
|
"""
|
||||||
Register the decorated function as a template test.
|
Return a plugin's configuration under .plugins[name]
|
||||||
:param fn: The function to register.
|
:param name: The plugin name.
|
||||||
"""
|
"""
|
||||||
# Make sure the Jinja2 environment is initialized
|
return config.get().get('plugins', {}).get(name, {})
|
||||||
env = _env_init()
|
|
||||||
# Add the function to the environment's tests
|
|
||||||
env.tests[fn.__name__] = fn
|
|
||||||
return fn
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from spaceapi_server.template import _env_init, render, render_traverse
|
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):
|
class TemplateTest(unittest.TestCase):
|
||||||
|
@ -17,11 +17,6 @@ class TemplateTest(unittest.TestCase):
|
||||||
def template_test_filter(value, other):
|
def template_test_filter(value, other):
|
||||||
return f'test_{other}_{value}'
|
return f'test_{other}_{value}'
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@template_test
|
|
||||||
def template_test_test(value):
|
|
||||||
return value == 'baz'
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@template_function
|
@template_function
|
||||||
def template_test_function_nocache():
|
def template_test_function_nocache():
|
||||||
|
@ -55,9 +50,7 @@ class TemplateTest(unittest.TestCase):
|
||||||
'builtin': '{{ [ 1337, 42 ] | first }}',
|
'builtin': '{{ [ 1337, 42 ] | first }}',
|
||||||
'test_functions': {
|
'test_functions': {
|
||||||
'test_function': '{{ template_test_function("foo") }}',
|
'test_function': '{{ template_test_function("foo") }}',
|
||||||
'test_filter': '{{ "bar" | template_test_filter("other") }}',
|
'test_filter': '{{ "bar" | template_test_filter("other") }}'
|
||||||
'test_test_true': '{{ "baz" is template_test_test }}',
|
|
||||||
'test_test_false': '{{ "foo" is template_test_test }}'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rendered = render_traverse(template)
|
rendered = render_traverse(template)
|
||||||
|
@ -67,8 +60,6 @@ class TemplateTest(unittest.TestCase):
|
||||||
self.assertEqual(1337, rendered['builtin'])
|
self.assertEqual(1337, rendered['builtin'])
|
||||||
self.assertEqual('test_foo', rendered['test_functions']['test_function'])
|
self.assertEqual('test_foo', rendered['test_functions']['test_function'])
|
||||||
self.assertEqual('test_other_bar', rendered['test_functions']['test_filter'])
|
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):
|
def test_no_cache(self):
|
||||||
template = ['{{ template_test_function_nocache() }}']
|
template = ['{{ template_test_function_nocache() }}']
|
||||||
|
|
Loading…
Reference in a new issue