Package Gnumed :: Package timelinelib :: Package general :: Module observer |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.general.observer

     1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg
     2  #
     3  # This file is part of Timeline.
     4  #
     5  # Timeline is free software: you can redistribute it and/or modify
     6  # it under the terms of the GNU General Public License as published by
     7  # the Free Software Foundation, either version 3 of the License, or
     8  # (at your option) any later version.
     9  #
    10  # Timeline is distributed in the hope that it will be useful,
    11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  # GNU General Public License for more details.
    14  #
    15  # You should have received a copy of the GNU General Public License
    16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>.
    17
    18
    19 -class Observable(object):
    20
    21 -    def __init__(self):
    22          self._observers = []
    23          self._listeners = []
    24
    25 -    def listen_for(self, event, function):
    26          self._listeners.append((True, event, function))
    27
    28 -    def listen_for_any(self, function):
    29          self._listeners.append((False, None, function))
    30
    31 -    def unlisten(self, function):
    32          self._listeners = [x for x in self._listeners if x[2] != function]
    33
    34 -    def register(self, fn):
    35          self._observers.append(fn)
    36
    37 -    def unregister(self, fn):
    38          if fn in self._observers:
    39              self._observers.remove(fn)
    40
    41 -    def _notify(self, state_change=None):
    42          for (listen_for_specific, event, function) in self._listeners:
    43              if listen_for_specific:
    44                  if state_change == event:
    45                      function()
    46              else:
    47                  function()
    48          for fn in self._observers:
    49              fn(state_change)
    50
    51
    52 -class Listener(object):
    53
    54 -    def __init__(self, callback):
    55          self._observable = None
    56          self._callback = callback
    57
    58 -    def set_observable(self, observable):
    59          self._unlisten()
    60          self._observable = observable
    61          self._listen()
    62
    63 -    def _unlisten(self):
    64          if self._observable is not None:
    65              self._observable.unlisten(self._listener)
    66
    67 -    def _listen(self):
    68          if self._observable is not None:
    69              self._observable.listen_for_any(self._listener)
    70              self._listener()
    71
    72 -    def _listener(self):
    73          self._callback(self._observable)
    74