1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
"""
.. module::glue.message
"""
from __future__ import absolute_import, division, print_function
__all__ = ['Message', 'ErrorMessage', 'SubsetMessage', 'SubsetCreateMessage',
'SubsetUpdateMessage', 'SubsetDeleteMessage', 'DataMessage',
'DataAddComponentMessage', 'DataUpdateMessage',
'DataCollectionMessage', 'DataCollectionActiveChange',
'DataCollectionActiveDataChange', 'DataCollectionAddMessage',
'DataCollectionDeleteMessage']
class Message(object):
"""
Base class for messages that the hub handles.
Each message represents a specific kind of event. After clients
register to a hub, the subscribe to specific message classes, and
will only receive those kinds of messages.
The message class family is hierarchical, and a client subscribing
to a message class implicitly subscribes to all of its subclasses.
:attr sender: The object which sent the message
:attr tag: An optional string describing the message
"""
def __init__(self, sender, tag=None):
"""Create a new message
:param sender: The object sending the message
:param tag: An optional string describing the message
"""
self.sender = sender
self.tag = tag
def __str__(self):
return '%s: %s\n\t Sent from: %s' % (type(self).__name__,
self.tag or '',
self.sender)
class ErrorMessage(Message):
""" Used to send general purpose error messages """
pass
class SubsetMessage(Message):
"""
A general message issued by a subset.
"""
def __init__(self, sender, tag=None):
from glue.core.subset import Subset
if (not isinstance(sender, Subset)):
raise TypeError("Sender must be a subset: %s"
% type(sender))
Message.__init__(self, sender, tag=tag)
self.subset = self.sender
class SubsetCreateMessage(SubsetMessage):
"""
A message that a subset issues when its state changes
"""
pass
class SubsetUpdateMessage(SubsetMessage):
"""
A message that a subset issues when its state changes.
"""
def __init__(self, sender, attribute=None, tag=None):
"""
:param attribute: An optional label of what attribute has changed
"""
SubsetMessage.__init__(self, sender, tag=tag)
self.attribute = attribute
def __str__(self):
result = super(SubsetUpdateMessage, self).__str__()
result += "\n\t Updated %s" % self.attribute
return result
class SubsetDeleteMessage(SubsetMessage):
"""
A message that a subset issues when it is deleted
"""
pass
class DataMessage(Message):
"""
The base class for messages that data objects issue
"""
def __init__(self, sender, tag=None):
from glue.core.data import Data
if (not isinstance(sender, Data)):
raise TypeError("Sender must be a data instance: %s"
% type(sender))
Message.__init__(self, sender, tag=tag)
self.data = self.sender
class DataAddComponentMessage(DataMessage):
def __init__(self, sender, component_id, tag=None):
super(DataAddComponentMessage, self).__init__(sender, tag=tag)
self.component_id = component_id
class ComponentsChangedMessage(DataMessage):
pass
class ComponentReplacedMessage(ComponentsChangedMessage):
def __init__(self, sender, old_component, new_component, tag=None):
super(ComponentReplacedMessage, self).__init__(sender, old_component)
self.old = old_component
self.new = new_component
class DataUpdateMessage(DataMessage):
def __init__(self, sender, attribute, tag=None):
super(DataUpdateMessage, self).__init__(sender, tag=tag)
self.attribute = attribute
class NumericalDataChangedMessage(DataMessage):
pass
class DataCollectionMessage(Message):
def __init__(self, sender, tag=None):
from glue.core.data_collection import DataCollection
if (not isinstance(sender, DataCollection)):
raise TypeError("Sender must be a DataCollection instance: %s"
% type(sender))
Message.__init__(self, sender, tag=tag)
class DataCollectionActiveChange(DataCollectionMessage):
pass
class DataCollectionActiveDataChange(DataCollectionMessage):
pass
class DataCollectionAddMessage(DataCollectionMessage):
def __init__(self, sender, data, tag=None):
DataCollectionMessage.__init__(self, sender, tag=tag)
self.data = data
class DataCollectionDeleteMessage(DataCollectionMessage):
def __init__(self, sender, data, tag=None):
DataCollectionMessage.__init__(self, sender, tag=tag)
self.data = data
class SettingsChangeMessage(Message):
"""
Indicates that some of the application-wide settings have changed
Parameters
----------
settings : iterable
An iterable of the settings that have changed.
"""
def __init__(self, sender, settings, tag=None):
super(SettingsChangeMessage, self).__init__(sender=sender, tag=tag)
self.settings = settings
|