Source code for vdat.libvdat.callback
"""Enqueue commands coming from users pushing buttons on the gui"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from logging import getLogger
from PyQt4.QtCore import QObject
import vdat.libvdat
# TODO: move it to vdat/gui
[docs]class Worker(QObject):
"""Generic worker that runs the given function"""
def __init__(self, parent=None):
super(Worker, self).__init__(parent)
[docs] def run(func):
"""Run the given function: it doesn't have any argument"""
func()
[docs]class Callback(object):
"""Class to deal with the user pushing buttons in the GUI. Causes the
buttons to add commands to a queue, specified by the set_queue command.
Attributes
----------
queue : :class:`vdat.gui.queue.Queue` object
the queue the callback interacts with
"""
queue = None
@classmethod
[docs] def set_queue(cls, queue):
"""Set the queue that the callbacks post jobs too.
Parameters
----------
queue : vdat.gui.queue Queue object
the queue the callback interacts with
"""
cls.queue = queue
@staticmethod
[docs] def make_callback_function(name, alias):
"""Factory function, used to generate functions
to attach to objects.
Parameters
----------
name : String
name of function to run on button click. Has to be in libvdat.
Syntax is::
package_name:function_name
alias : String
a label for the job on the queue GUI.
immediate : bool (optional)
bypass the usual queue and run immediately
"""
log = getLogger('logger')
def _notImplemented():
"""Deal with a button click from the GUI
"""
log.error("<b> %s </b> not yet implemented!", name)
try:
split_name = name.split(':')
log.debug(split_name)
package = getattr(vdat.libvdat, split_name[0])
cmd = getattr(package, split_name[1])
def func():
Callback.queue.queue_command(cmd, alias)
except AttributeError:
func = _notImplemented
return func