Browse Source

Updated event module to support kwargs.

pyserial_fix
Scott Petersen 11 years ago
parent
commit
2f9d95e0ea
6 changed files with 67 additions and 66 deletions
  1. +26
    -26
      pyad2/ad2.py
  2. +6
    -6
      pyad2/devices.py
  3. +8
    -7
      pyad2/event/event.py
  4. +23
    -23
      pyad2/tests/test_ad2.py
  5. +2
    -2
      pyad2/tests/test_zonetracking.py
  6. +2
    -2
      pyad2/zonetracking.py

+ 26
- 26
pyad2/ad2.py View File

@@ -158,10 +158,10 @@ class AD2Factory(object):
last_devices = current_devices last_devices = current_devices


for d in new_devices: for d in new_devices:
self._factory.on_attached(d)
self._factory.on_attached(device=d)


for d in removed_devices: for d in removed_devices:
self._factory.on_detached(d)
self._factory.on_detached(device=d)


except CommError, err: except CommError, err:
pass pass
@@ -416,7 +416,7 @@ class AD2(object):
""" """
msg = RFMessage(data) msg = RFMessage(data)


self.on_rfx_message(msg)
self.on_rfx_message(message=msg)


return msg return msg


@@ -433,14 +433,14 @@ class AD2(object):


if msg.event_type == 'ALARM_PANIC': if msg.event_type == 'ALARM_PANIC':
self._panic_status = True self._panic_status = True
self.on_panic(True)
self.on_panic(status=True)


elif msg.event_type == 'CANCEL': elif msg.event_type == 'CANCEL':
if self._panic_status == True: if self._panic_status == True:
self._panic_status = False self._panic_status = False
self.on_panic(False)
self.on_panic(status=False)


self.on_lrr_message(msg)
self.on_lrr_message(message=msg)


return msg return msg


@@ -486,19 +486,19 @@ class AD2(object):
self._power_status, old_status = message.ac_power, self._power_status self._power_status, old_status = message.ac_power, self._power_status


if old_status is not None: if old_status is not None:
self.on_power_changed(self._power_status)
self.on_power_changed(status=self._power_status)


if message.alarm_sounding != self._alarm_status: if message.alarm_sounding != self._alarm_status:
self._alarm_status, old_status = message.alarm_sounding, self._alarm_status self._alarm_status, old_status = message.alarm_sounding, self._alarm_status


if old_status is not None: if old_status is not None:
self.on_alarm(self._alarm_status)
self.on_alarm(status=self._alarm_status)


if message.zone_bypassed != self._bypass_status: if message.zone_bypassed != self._bypass_status:
self._bypass_status, old_status = message.zone_bypassed, self._bypass_status self._bypass_status, old_status = message.zone_bypassed, self._bypass_status


if old_status is not None: if old_status is not None:
self.on_bypass(self._bypass_status)
self.on_bypass(status=self._bypass_status)


if (message.armed_away | message.armed_home) != self._armed_status: if (message.armed_away | message.armed_home) != self._armed_status:
self._armed_status, old_status = message.armed_away | message.armed_home, self._armed_status self._armed_status, old_status = message.armed_away | message.armed_home, self._armed_status
@@ -514,20 +514,20 @@ class AD2(object):
else: else:
if message.battery_low == True or time.time() > self._battery_status[1] + AD2.BATTERY_TIMEOUT: if message.battery_low == True or time.time() > self._battery_status[1] + AD2.BATTERY_TIMEOUT:
self._battery_status = (message.battery_low, time.time()) self._battery_status = (message.battery_low, time.time())
self.on_low_battery(self._battery_status)
self.on_low_battery(status=self._battery_status)


if message.fire_alarm == self._fire_status[0]: if message.fire_alarm == self._fire_status[0]:
self._fire_status = (self._fire_status[0], time.time()) self._fire_status = (self._fire_status[0], time.time())
else: else:
if message.fire_alarm == True or time.time() > self._fire_status[1] + AD2.FIRE_TIMEOUT: if message.fire_alarm == True or time.time() > self._fire_status[1] + AD2.FIRE_TIMEOUT:
self._fire_status = (message.fire_alarm, time.time()) self._fire_status = (message.fire_alarm, time.time())
self.on_fire(self._fire_status)
self.on_fire(status=self._fire_status)


elif isinstance(message, ExpanderMessage): elif isinstance(message, ExpanderMessage):
if message.type == ExpanderMessage.RELAY: if message.type == ExpanderMessage.RELAY:
self._relay_status[(message.address, message.channel)] = message.value self._relay_status[(message.address, message.channel)] = message.value


self.on_relay_changed(message)
self.on_relay_changed(message=message)


self._update_zone_tracker(message) self._update_zone_tracker(message)


@@ -548,42 +548,42 @@ class AD2(object):


self._zonetracker.update(message) self._zonetracker.update(message)


def _on_open(self, sender, args):
def _on_open(self, sender, *args, **kwargs):
""" """
Internal handler for opening the device. Internal handler for opening the device.
""" """
self.on_open(args)
self.on_open(args, kwargs)


def _on_close(self, sender, args):
def _on_close(self, sender, *args, **kwargs):
""" """
Internal handler for closing the device. Internal handler for closing the device.
""" """
self.on_close(args)
self.on_close(args, kwargs)


def _on_read(self, sender, args):
def _on_read(self, sender, *args, **kwargs):
""" """
Internal handler for reading from the device. Internal handler for reading from the device.
""" """
self.on_read(args)
self.on_read(args, kwargs)


msg = self._handle_message(args)
msg = self._handle_message(kwargs['data'])
if msg: if msg:
self.on_message(msg)
self.on_message(message=msg)


def _on_write(self, sender, args):
def _on_write(self, sender, *args, **kwargs):
""" """
Internal handler for writing to the device. Internal handler for writing to the device.
""" """
self.on_write(args)
self.on_write(args, kwargs)


def _on_zone_fault(self, sender, args):
def _on_zone_fault(self, sender, *args, **kwargs):
""" """
Internal handler for zone faults. Internal handler for zone faults.
""" """
self.on_zone_fault(args)
self.on_zone_fault(args, kwargs)


def _on_zone_restore(self, sender, args):
def _on_zone_restore(self, sender, *args, **kwargs):
""" """
Internal handler for zone restoration. Internal handler for zone restoration.
""" """
self.on_zone_restore(args)
self.on_zone_restore(args, kwargs)

+ 6
- 6
pyad2/devices.py View File

@@ -304,7 +304,7 @@ class USBDevice(Device):
try: try:
self._device.write_data(data) self._device.write_data(data)


self.on_write(data)
self.on_write(data=data)


except FtdiError, err: except FtdiError, err:
raise CommError('Error writing to device: {0}'.format(str(err)), err) raise CommError('Error writing to device: {0}'.format(str(err)), err)
@@ -385,7 +385,7 @@ class USBDevice(Device):
ret = self._buffer ret = self._buffer
self._buffer = '' self._buffer = ''


self.on_read(ret)
self.on_read(data=ret)


if timer: if timer:
if timer.is_alive(): if timer.is_alive():
@@ -530,7 +530,7 @@ class SerialDevice(Device):
raise CommError('Error writing to device.', err) raise CommError('Error writing to device.', err)


else: else:
self.on_write(data)
self.on_write(data=data)


def read(self): def read(self):
""" """
@@ -604,7 +604,7 @@ class SerialDevice(Device):
ret = self._buffer ret = self._buffer
self._buffer = '' self._buffer = ''


self.on_read(ret)
self.on_read(data=ret)


if timer: if timer:
if timer.is_alive(): if timer.is_alive():
@@ -797,7 +797,7 @@ class SocketDevice(Device):
if data_sent == 0: if data_sent == 0:
raise CommError('Error writing to device.') raise CommError('Error writing to device.')


self.on_write(data)
self.on_write(data=data)


except (SSL.Error, socket.error), err: except (SSL.Error, socket.error), err:
raise CommError('Error writing to device.', err) raise CommError('Error writing to device.', err)
@@ -880,7 +880,7 @@ class SocketDevice(Device):
ret = self._buffer ret = self._buffer
self._buffer = '' self._buffer = ''


self.on_read(ret)
self.on_read(data=ret)


if timer: if timer:
if timer.is_alive(): if timer.is_alive():


+ 8
- 7
pyad2/event/event.py View File

@@ -2,11 +2,12 @@
# #


# Based on pyevent originally found at http://www.emptypage.jp/notes/pyevent.en.html # Based on pyevent originally found at http://www.emptypage.jp/notes/pyevent.en.html
#
#
# License: https://creativecommons.org/licenses/by/2.1/jp/deed.en # License: https://creativecommons.org/licenses/by/2.1/jp/deed.en
# #
# Changes: # Changes:
# Added type check in fire()
# * Added type check in fire()
# * Removed earg from fire() and added support for args/kwargs.


class Event(object): class Event(object):


@@ -60,19 +61,19 @@ class EventHandler(object):
self._getfunctionlist().remove(func) self._getfunctionlist().remove(func)
return self return self


def fire(self, earg=None):
def fire(self, *args, **kwargs):


"""Fire event and call all handler functions """Fire event and call all handler functions


You can call EventHandler object itself like e(earg) instead of
e.fire(earg).
You can call EventHandler object itself like e(*args, **kwargs) instead of
e.fire(*args, **kwargs).
""" """


for func in self._getfunctionlist(): for func in self._getfunctionlist():
if type(func) == EventHandler: if type(func) == EventHandler:
func.fire(earg)
func.fire(*args, **kwargs)
else: else:
func(self.obj, earg)
func(self.obj, *args, **kwargs)


__iadd__ = add __iadd__ = add
__isub__ = remove __isub__ = remove


+ 23
- 23
pyad2/tests/test_ad2.py View File

@@ -20,10 +20,10 @@ class TestAD2Factory(TestCase):
def tearDown(self): def tearDown(self):
self._factory.stop() self._factory.stop()


def attached_event(self, sender, args):
def attached_event(self, sender, *args, **kwargs):
self._attached = True self._attached = True


def detached_event(self, sender, args):
def detached_event(self, sender, *args, **kwargs):
self._detached = True self._detached = True


def test_find_all(self): def test_find_all(self):
@@ -114,43 +114,43 @@ class TestAD2(TestCase):
def tearDown(self): def tearDown(self):
pass pass


def on_panic(self, sender, args):
self._panicked = args
def on_panic(self, sender, *args, **kwargs):
self._panicked = kwargs['status']


def on_relay_changed(self, sender, args):
def on_relay_changed(self, sender, *args, **kwargs):
self._relay_changed = True self._relay_changed = True


def on_power_changed(self, sender, args):
self._power_changed = args
def on_power_changed(self, sender, *args, **kwargs):
self._power_changed = kwargs['status']


def on_alarm(self, sender, args):
self._alarmed = args
def on_alarm(self, sender, *args, **kwargs):
self._alarmed = kwargs['status']


def on_bypass(self, sender, args):
self._bypassed = args
def on_bypass(self, sender, *args, **kwargs):
self._bypassed = kwargs['status']


def on_battery(self, sender, args):
self._battery = args
def on_battery(self, sender, *args, **kwargs):
self._battery = kwargs['status']


def on_fire(self, sender, args):
self._fire = args
def on_fire(self, sender, *args, **kwargs):
self._fire = kwargs['status']


def on_arm(self, sender, args):
def on_arm(self, sender, *args, **kwargs):
self._armed = True self._armed = True


def on_disarm(self, sender, args):
def on_disarm(self, sender, *args, **kwargs):
self._armed = False self._armed = False


def on_config(self, sender, args):
def on_config(self, sender, *args, **kwargs):
self._got_config = True self._got_config = True


def on_message(self, sender, args):
def on_message(self, sender, *args, **kwargs):
self._message_received = True self._message_received = True


def on_rfx_message(self, sender, args):
def on_rfx_message(self, sender, *args, **kwargs):
self._rfx_message_received = True self._rfx_message_received = True


def on_lrr_message(self, sender, args):
def on_lrr_message(self, sender, *args, **kwargs):
self._lrr_message_received = True self._lrr_message_received = True


def test_open(self): def test_open(self):
@@ -195,14 +195,14 @@ class TestAD2(TestCase):
msg = self._ad2._handle_message('[0000000000000000----],000,[f707000600e5800c0c020000]," "') msg = self._ad2._handle_message('[0000000000000000----],000,[f707000600e5800c0c020000]," "')
self.assertIsInstance(msg, Message) self.assertIsInstance(msg, Message)


self._ad2._on_read(self, '[0000000000000000----],000,[f707000600e5800c0c020000]," "')
self._ad2._on_read(self, data='[0000000000000000----],000,[f707000600e5800c0c020000]," "')
self.assertTrue(self._message_received) self.assertTrue(self._message_received)


def test_message_kpe(self): def test_message_kpe(self):
msg = self._ad2._handle_message('!KPE:[0000000000000000----],000,[f707000600e5800c0c020000]," "') msg = self._ad2._handle_message('!KPE:[0000000000000000----],000,[f707000600e5800c0c020000]," "')
self.assertIsInstance(msg, Message) self.assertIsInstance(msg, Message)


self._ad2._on_read(self, '[0000000000000000----],000,[f707000600e5800c0c020000]," "')
self._ad2._on_read(self, data='[0000000000000000----],000,[f707000600e5800c0c020000]," "')
self.assertTrue(self._message_received) self.assertTrue(self._message_received)


def test_expander_message(self): def test_expander_message(self):


+ 2
- 2
pyad2/tests/test_zonetracking.py View File

@@ -17,10 +17,10 @@ class TestZonetracking(TestCase):
def tearDown(self): def tearDown(self):
pass pass


def fault_event(self, sender, args):
def fault_event(self, sender, *args, **kwargs):
self._faulted = True self._faulted = True


def restore_event(self, sender, args):
def restore_event(self, sender, *args, **kwargs):
self._restored = True self._restored = True


def _build_expander_message(self, msg): def _build_expander_message(self, msg):


+ 2
- 2
pyad2/zonetracking.py View File

@@ -238,7 +238,7 @@ class Zonetracker(object):
self._zones[zone] = Zone(zone=zone, name=name, status=status) self._zones[zone] = Zone(zone=zone, name=name, status=status)


if status != Zone.CLEAR: if status != Zone.CLEAR:
self.on_fault(zone)
self.on_fault(zone=zone)


def _update_zone(self, zone, status=None): def _update_zone(self, zone, status=None):
""" """
@@ -263,7 +263,7 @@ class Zonetracker(object):
if zone in self._zones_faulted: if zone in self._zones_faulted:
self._zones_faulted.remove(zone) self._zones_faulted.remove(zone)


self.on_restore(zone)
self.on_restore(zone=zone)


def _zone_expired(self, zone): def _zone_expired(self, zone):
""" """


Loading…
Cancel
Save