Update examples to v0.4

This commit is contained in:
s3lph 2021-12-07 03:42:04 +01:00
parent 7557183a3a
commit 826a108308
6 changed files with 51 additions and 69 deletions

View file

@ -1,13 +0,0 @@
{
"address": "::1",
"port": 8000,
"template": "examples/template.json",
"plugins_dir": "examples/plugins",
"plugins": {
"example": {
"test_value": "the Spanish Inquisition"
}
}
}

16
examples/config.yaml Normal file
View file

@ -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

View file

@ -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')

View file

@ -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)
}

View file

@ -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') }}"
}
}

12
examples/template.yaml Normal file
View file

@ -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" ]