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 @@
"""
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):
del tmpl
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'
......@@ -46,6 +46,7 @@ import time
import uuid
import shlex
import ph_inline_actions
from modules import singleton
class Handler(object):
......@@ -216,20 +217,24 @@ class HandlerClient(threading.Thread):
if not exec_in_background:
self.connection.close()
@singleton
class ActionHandler(object):
""" 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
:param config_path: full path of configuration data
:param config_environment: environment to use (if possible)
:return:
"""
if config_path is not None:
self.config_path = config_path
if config_environment is not None:
self.config_environment = config_environment
self.action_map = {}
# try to load data on initial start
if not hasattr(self, 'action_map'):
self.load_config()
def load_config(self):
......@@ -266,6 +271,23 @@ class ActionHandler(object):
for alias in section.split('|'):
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):
""" 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