Commit e5d998de authored by Martin Matějek's avatar Martin Matějek

WIP; call action on message from cli

parent 22c5210d
......@@ -45,6 +45,10 @@ def create_argparser():
parser_get.add_argument("media_type", help="Media type of notification message", nargs="?", default="simple")
parser_get.add_argument("lang", help="Language of notification message", nargs="?", default="en")
parser_call = subparsers.add_parser("call", help="call actions")
parser_call.add_argument("msgid")
parser_call.add_argument("action")
return parser
......@@ -103,8 +107,10 @@ def process_args(parser, args):
media_type = args.media_type
lang = args.lang
ret = api.get_notification(msgid, media_type, lang)
ret = api.get_rendered_notification(msgid, media_type, lang)
print_notification(ret)
elif args.command == 'call':
api.call_action(args.msgid, args.action)
else:
parser.print_usage()
......
......@@ -34,10 +34,15 @@ class Api:
self.delete_invalid_messages()
return self.notifications.get_all()
def get_notification(self, msgid, media_type, lang):
def get_notification(self, msgid):
"""Get single notification instance"""
self.delete_invalid_messages()
return self.notifications.get_notification(msgid)
def get_rendered_notification(self, msgid, media_type, lang):
"""Show notification of one specific by id"""
self.delete_invalid_messages()
return self.notifications.get_notification(msgid, media_type, lang)
return self.notifications.get_rendered_notification(msgid, media_type, lang)
def get_templates(self):
"""Return notification types from plugins"""
......@@ -66,10 +71,13 @@ class Api:
self.notifications.store(notif)
# TODO: rethink/refactor
def call_action(self, mgsid, name, **kwargs):
def call_action(self, msgid, name, **kwargs):
"""Call action on notification"""
pass
# storage.actions[name](**kwargs)
n = self.notifications.get_notification(msgid)
n.call_action(name)
if name == 'dismiss':
self.notifications.delete_message(msgid)
def dismiss(self, msgid):
"""Dismiss specific notification"""
......
......@@ -113,12 +113,12 @@ class Notification:
return json.dumps(json_data, indent=4)
def action(self, name):
def call_action(self, name):
if name == 'dismiss':
# set yourself invalid
pass
self.valid = False
else:
# call action from skeleton
# TODO: action args?
self.skeleton.call_action(name)
@staticmethod
......
import subprocess
import jinja2
......@@ -36,9 +38,16 @@ class NotificationSkeleton:
return defaults
def call_action(self, name):
def call_action(self, name, dry_run=True):
if name in self.actions:
self.actions[name]()
act = self.actions[name]['command']
if dry_run:
print("Dry run: executing command '{}'".format(act))
else:
# TODO: validate command string
cmd = act.split(' ')
subprocess.run(cmd)
def init_jinja_env(self):
"""
......
......@@ -55,7 +55,10 @@ class NotificationStorage:
n = Notification.from_file(filepath)
self.notifications[n.notif_id] = n
def get_notification(self, msgid, media_type, lang):
def get_notification(self, msgid):
return self.notifications[msgid]
def get_rendered_notification(self, msgid, media_type, lang):
"""Return notification either cached or if missing, cache it and return"""
if (msgid, media_type, lang) not in self.rendered:
self.rendered[(msgid, media_type, lang)] = self.notifications[msgid].render(media_type, lang)
......@@ -87,12 +90,17 @@ class NotificationStorage:
self.dismiss(n.notif_id)
logger.debug("Deleting notification '%s' due to timeout", n.notif_id)
def delete_message(self, msgid):
"""Delete single notification"""
self.dismiss(msgid)
def dismiss(self, msgid):
"""Dismiss specific notification"""
"""Dismiss single notification"""
# TODO: do it properly via builtin action
n = self.notifications[msgid]
del self.notifications[msgid]
logger.debug("Dismissing notification '%s'", msgid)
if n.persistent:
storage_dir = self.storage_dirs['persistent']
else:
......
......@@ -11,7 +11,7 @@ class Plugin:
self.notification_types = {}
for a in actions:
self.actions[a.name] = a
self.actions[a['name']] = a
for t in templates:
self.templates[t['type']] = t
......
......@@ -64,7 +64,8 @@ class PluginStorage:
skel_actions = {}
plugin_actions = self.plugins[plugin_name].get_actions()
for action in notification_types[skel_name]['actions']:
skel_actions[action] = plugin_actions[action]
if action in plugin_actions:
skel_actions[action] = plugin_actions[action]
notification_args['actions'] = skel_actions
......
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