diff --git a/README.md b/README.md index 14593f4..fa47ae4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/plugins/example.py b/examples/plugins/example.py index a0de9cf..40994d1 100644 --- a/examples/plugins/example.py +++ b/examples/plugins/example.py @@ -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') diff --git a/spaceapi_server/config/__init__.py b/spaceapi_server/config/__init__.py index f9e9156..ed499a4 100644 --- a/spaceapi_server/config/__init__.py +++ b/spaceapi_server/config/__init__.py @@ -22,13 +22,4 @@ def get() -> dict: Return the current configuration. """ 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, {}) + return __CONFIG \ No newline at end of file diff --git a/spaceapi_server/config/test/test_config.py b/spaceapi_server/config/test/test_config.py index fc1685a..ec0df6b 100644 --- a/spaceapi_server/config/test/test_config.py +++ b/spaceapi_server/config/test/test_config.py @@ -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): diff --git a/spaceapi_server/plugins/__init__.py b/spaceapi_server/plugins/__init__.py index b1a4e5a..d6b52fc 100644 --- a/spaceapi_server/plugins/__init__.py +++ b/spaceapi_server/plugins/__init__.py @@ -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, {}) diff --git a/spaceapi_server/template/test/test_template.py b/spaceapi_server/template/test/test_template.py index a2e9153..3c51076 100644 --- a/spaceapi_server/template/test/test_template.py +++ b/spaceapi_server/template/test/test_template.py @@ -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() }}']