Package Geoclue :: Module Signal'

[hide private]
[frames] | no frames]

# Source Code for Module Geoclue.Signal'

     1  # -*- coding: utf-8 -*-
     2  # Copyright (c) 2009 - Paulo Cabido <paulo.cabido@gmail.com>
     3  #
     4  # This program is free software: you can redistribute it and/or modify it under
     5  # the terms of the GNU General Public License as published by the Free Software
     6  # Foundation, either version 3 of the License, or (at your option) any later
     7  # version.
     8  #
     9  # This program is distributed in the hope that it will be useful, but WITHOUT
    10  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    11  # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    12  # details.
    13  #
    14  # You should have received a copy of the GNU General Public License along with
    15  # this program.  If not, see <http://www.gnu.org/licenses/>.
    16
    17  import random
    18
    19 -class Signal:
    20 -    class Slot:
    21 -        def __init__(self, func):
    22              self.__func = func
    23
    24 -        def __call__(self, accum, *args, **kwargs):
    25              result = self.__func(*args, **kwargs)
    26              return accum(result)
    27
    28 -    class Accumulator:
    29 -        def __call__(self, *args, **kwargs):
    30              return True
    31
    32 -        def finalize(self):
    33              return None
    34
    35 -    def __init__(self):
    36          self.__slots = []
    37
    38 -    def create_accumulator(self):
    39          return self.Accumulator()
    40
    41      # execute the slots
    42 -    def __call__(self, *args, **kwargs):
    43          accum = self.create_accumulator()
    44          for conn in xrange(len(self.__slots)):
    45              if not self.__slots[conn][1](accum, *args, **kwargs):
    46                  break
    47          return accum.finalize()
    48
    49 -    def find(self, conn):
    50          for i in xrange(len(self.__slots)):
    51              if self.__slots[i][0] == conn:
    52                  return i
    53
    54          return -1
    55
    56      # create the connection name
    57 -    def new_connection(self):
    58          value = 0
    59          while self.find(value) >= 0:
    60              value = random.randint(1, 100000000)
    61          return value
    62
    63 -    def connect(self, func):
    64          conn = self.new_connection()
    65          self.__slots.append([conn, Signal.Slot(func)])
    66          return conn
    67
    68      # disconnect a slot
    69 -    def disconnect(self, conn):
    70          result = self.Find(conn)
    71          if result >= 0:
    72              del self.__slots[result]
    73
    74      # disconnect all slots
    75 -    def disconnect_all(self):
    76          self.__slots = []
    77