Commit 0e0481d5 authored by Ad Schellevis's avatar Ad Schellevis

(configd) add configd method to list registered actions

add a description tag to the actions_[service].conf sections to describe actions for the user interface.
parent 435492ad
[actions]
command:configd.actions
parameters:%s
type:inline
message:get registered configd actions
...@@ -31,3 +31,13 @@ ...@@ -31,3 +31,13 @@
""" """
def singleton(cls, *args, **kwargs):
""" singleton pattern, use ad decorator
"""
instances = {}
def getinstance(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return getinstance
...@@ -60,5 +60,21 @@ def execute(action, parameters): ...@@ -60,5 +60,21 @@ def execute(action, parameters):
del tmpl del tmpl
return 'OK' return 'OK'
elif action.command == 'configd.actions':
# list all available configd actions
from processhandler import ActionHandler
actHandler = ActionHandler()
actions = actHandler.listActions(['message', 'description'])
if unicode(parameters).lower() == 'json':
import json
return json.dumps(actions)
else:
result = []
for action in actions:
result.append('%s [ %s ]'%(action,actions[action]['description']))
return '\n'.join(result)
return 'ERR' return 'ERR'
...@@ -46,6 +46,7 @@ import time ...@@ -46,6 +46,7 @@ import time
import uuid import uuid
import shlex import shlex
import ph_inline_actions import ph_inline_actions
from modules import singleton
class Handler(object): class Handler(object):
...@@ -216,21 +217,25 @@ class HandlerClient(threading.Thread): ...@@ -216,21 +217,25 @@ class HandlerClient(threading.Thread):
if not exec_in_background: if not exec_in_background:
self.connection.close() self.connection.close()
@singleton
class ActionHandler(object): class ActionHandler(object):
""" Start/stop services and functions using configuration data defined in conf/actions_<topic>.conf """ Start/stop services and functions using configuration data defined in conf/actions_<topic>.conf
""" """
def __init__(self, config_path, config_environment): def __init__(self, config_path=None, config_environment=None):
""" Initialize action handler to start system functions """ Initialize action handler to start system functions
:param config_path: full path of configuration data :param config_path: full path of configuration data
:param config_environment: environment to use (if possible) :param config_environment: environment to use (if possible)
:return: :return:
""" """
self.config_path = config_path if config_path is not None:
self.config_environment = config_environment self.config_path = config_path
self.action_map = {} if config_environment is not None:
self.load_config() self.config_environment = config_environment
# try to load data on initial start
if not hasattr(self, 'action_map'):
self.load_config()
def load_config(self): def load_config(self):
""" load action configuration from config files into local dictionary """ load action configuration from config files into local dictionary
...@@ -266,6 +271,23 @@ class ActionHandler(object): ...@@ -266,6 +271,23 @@ class ActionHandler(object):
for alias in section.split('|'): for alias in section.split('|'):
self.action_map[topic_name][alias] = action_obj self.action_map[topic_name][alias] = action_obj
def listActions(self, attributes=[]):
""" list all available actions
:return: dict
"""
result = {}
for command in self.action_map:
for action in self.action_map[command]:
cmd='%s %s'%(command,action)
result[cmd] = {}
for actAttr in attributes:
if hasattr(self.action_map[command][action], actAttr):
result[cmd][actAttr] = getattr(self.action_map[command][action], actAttr)
else:
result[cmd][actAttr] = ''
return result
def findAction(self, command, action, parameters): def findAction(self, command, action, parameters):
""" find action object """ find action object
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment