The Assimilation Project  based on Assimilation version 1.1.7.1474836767
assimevent.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # vim: smartindent tabstop=4 shiftwidth=4 expandtab number
3 #
4 # This file is part of the Assimilation Project.
5 #
6 # Author: Alan Robertson <alanr@unix.sh>
7 # Copyright (C) 2014 - Assimilation Systems Limited
8 #
9 # Free support is available from the Assimilation Project community - http://assimproj.org
10 # Paid support is available from Assimilation Systems Limited - http://assimilationsystems.com
11 #
12 # The Assimilation software is free software: you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # The Assimilation software is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with the Assimilation Project software. If not, see http://www.gnu.org/licenses/
24 #
25 #
26 '''
27 This module implements classes associated with Events in the Assimilation Project.
28 '''
29 
30 class AssimEvent(object):
31  '''This class is all about highlighting events which others might want to know about.
32  Other objects can register to be notified about the creation of new events.
33  Or at least they will be able to when that code is written ;-).
34  All of this happens without any concern by the event objects themselves.
35  It is handled by static methods and a tiny bit of code in our constructor.
36  '''
37  # Legal event types
38  CREATEOBJ = 0 # Object was newly created
39  OBJUP = 1 # Object status is now up
40  OBJDOWN = 2 # Object status is now down
41  OBJWARN = 3 # Object status is now in a warning state
42  OBJUNWARN = 4 # Object status has exited a warning state
43  OBJUPDATE = 5 # Object was updated
44  OBJDELETE = 6 # Object is about to be deleted
45 
46  eventtypenames = {
47  CREATEOBJ: 'create',
48  OBJUP: 'up',
49  OBJDOWN: 'down',
50  OBJWARN: 'warn',
51  OBJUNWARN: 'unwarn',
52  OBJUPDATE: 'update',
53  OBJDELETE: 'delete'
54  }
55 
56  event_observation_enabled = True
57 
58  observers = []
59 
60  def __init__(self, associatedobject, eventtype, extrainfo=None):
61  '''Initializer for AssimEvent class.
62  We save our parameters then notify our registered observers.
63 
64  Parameters:
65  ----------
66  associatedobject: GraphNode
67  The GraphNode associated with this event
68  eventtype: int
69  one of AssimEvent.CREATEOBJ, AssimEvent.OBJUP, AssimEvent.OBJDOWN,
70  AssimEvent.OBJWARN, AssimEvent.OBJUPDATE or AssimEvent.OBJDELETE
71  '''
72  if eventtype not in AssimEvent.eventtypenames:
73  raise ValueError('Event type [%s] is not a legal event type' % eventtype)
74 
75  self.associatedobject = associatedobject
76  self.eventtype = eventtype
77  self.extrainfo = extrainfo
78  if AssimEvent.event_observation_enabled:
79  self.notifynewevent()
80 
81  @staticmethod
83  "Useful when testing and we don't want to trigger external event observers..."
84  AssimEvent.event_observation_enabled = False
85 
86  @staticmethod
88  "Useful when testing and we want to undo the operation above..."
89  AssimEvent.event_observation_enabled = True
90 
91  @staticmethod
92  def is_registered(observer):
93  '''Return True if the given observer is registered with us.
94  '''
95  return observer in AssimEvent.observers
96 
97  @staticmethod
98  def registerobserver(observer):
99  '''Static method for registering an observer with the AssimEvent class.
100  The given observer object must implement a 'notifynewevent' method
101  -- because we will surely call it :-D.
102  '''
103  if not hasattr(observer, 'notifynewevent'):
104  raise(AttributeError('observer must have a notifynewevent method'))
105  if observer not in AssimEvent.observers:
106  AssimEvent.observers.append(observer)
107 
108  @staticmethod
109  def unregisterobserver(observer):
110  '''Static method for unregistering an observer with the AssimEvent class.
111  We return True if the given observer was registered with us, False otherwise.
112  '''
113  for j in range(0, len(AssimEvent.observers)):
114  if AssimEvent.observers[j] is observer:
115  del AssimEvent.observers[j]
116  return True
117  return False
118 
119  def notifynewevent(self):
120  '''method for notifying all our observers that a new event
121  has been created.
122  We call the 'notifynewevent' method in each registered observer object.
123  '''
124  for observer in AssimEvent.observers:
125  observer.notifynewevent(self)
def registerobserver(observer)
Definition: assimevent.py:98
def is_registered(observer)
Definition: assimevent.py:92
def unregisterobserver(observer)
Definition: assimevent.py:109
def __init__(self, associatedobject, eventtype, extrainfo=None)
Definition: assimevent.py:60