From 826a1083083b02b46a56cfd8647be182ae472f75 Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 7 Dec 2021 03:42:04 +0100 Subject: [PATCH] Update examples to v0.4 --- examples/config.json | 13 ------------- examples/config.yaml | 16 ++++++++++++++++ examples/plugins/example.py | 36 ----------------------------------- examples/plugins/filestate.py | 23 ++++++++++++++++++++++ examples/template.json | 20 ------------------- examples/template.yaml | 12 ++++++++++++ 6 files changed, 51 insertions(+), 69 deletions(-) delete mode 100644 examples/config.json create mode 100644 examples/config.yaml delete mode 100644 examples/plugins/example.py create mode 100644 examples/plugins/filestate.py delete mode 100644 examples/template.json create mode 100644 examples/template.yaml diff --git a/examples/config.json b/examples/config.json deleted file mode 100644 index e891d7d..0000000 --- a/examples/config.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "address": "::1", - "port": 8000, - "template": "examples/template.json", - "plugins_dir": "examples/plugins", - - "plugins": { - "example": { - "test_value": "the Spanish Inquisition" - } - } -} - diff --git a/examples/config.yaml b/examples/config.yaml new file mode 100644 index 0000000..2bdce0c --- /dev/null +++ b/examples/config.yaml @@ -0,0 +1,16 @@ +--- +# The address to listen on. +address: "::1" +# The TCP port to listen on. +port: 8000 +# The Bottle backend server to use. +server: wsgiref +# Path to the SpaceAPI response template file. +template: template.yaml +# Path to the directory containing your plugins. +plugins_dir: plugins +plugins: + # Config for the "filestate" plugin + filestate: + # Use this statefile instead of the default + filename: /var/www/html/space_state diff --git a/examples/plugins/example.py b/examples/plugins/example.py deleted file mode 100644 index 40994d1..0000000 --- a/examples/plugins/example.py +++ /dev/null @@ -1,36 +0,0 @@ - -from spaceapi_server import plugins - - -@plugins.template_function -def example_function(name: str): - """ - This function is registered as a Jinja2 function. It can be used like this: - {{ example_function('the Spanish Inquisition') }} - """ - return f'Nobody expects {name}' - - -@plugins.template_filter -def example_filter(name: str): - """ - This function is registered as a Jinja2 filter. It can be used like this: - {{ 'the Spanish Inquisition' | example_filter }} - """ - return f'Nobody expects {name}' - - -@plugins.template_function -def example_config_function(): - """ - 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 = plugins.get_plugin_config('example') - # Get the .test_value property from the plugin config, falling - # back to a default value - return conf.get('test_value', 'the Spanish Inquisition') diff --git a/examples/plugins/filestate.py b/examples/plugins/filestate.py new file mode 100644 index 0000000..7191a70 --- /dev/null +++ b/examples/plugins/filestate.py @@ -0,0 +1,23 @@ +import os +from spaceapi_server import config, plugins + +@plugins.template_function +# The plugin can be invoked by using the !space_state YAML tag +def space_state(): + # Get the plugin config dict + conf = config.get_plugin_config('filestate') + # Get the filename, default to /var/space_state + filename = conf.get('filename', '/var/space_state') + try: + # Get the file's properties + stat = os.stat(filename) + except FileNotFoundError: + # File doesn't exist, aka. space is closed + return { + 'open': False + } + # File exists, aka. space is open. Also report the mtime as "last changed" timestamp + return { + 'open': True, + 'lastchange': int(stat.st_mtime) + } diff --git a/examples/template.json b/examples/template.json deleted file mode 100644 index 7c701f9..0000000 --- a/examples/template.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "api": "0.13 {#- Go look at https://spaceapi.io/docs -#}", - "space": "Our New Hackerspace", - "logo": "https://example.org/logo.png", - "url": "https://example.org/", - "location": { - "lat": 0.0, - "lon": 0.0 - }, - "contact": { - "email": "example@example.org" - }, - "issue_report_channels": [ - "email" - ], - "state": "{#- You can write your own plugins for retrieving dynamic information -#} {{ our_space_state() }}", - "sensors": { - "people_now_present": "{{ our_sensor_backend('people_count') }}" - } -} diff --git a/examples/template.yaml b/examples/template.yaml new file mode 100644 index 0000000..27e8d24 --- /dev/null +++ b/examples/template.yaml @@ -0,0 +1,12 @@ +--- +api: "0.13" +space: My Hackerspace +# This is a plugin invocation +# with no arguments +state: !space_state {} +sensors: + # This is a plugin invocation with + # arguments. They are passed to the + # plugin function as kwargs. + network_connections: !network_connections + networks: [ "2.4 GHz", "5 GHz" ]