diff --git a/examples/alarm_email.py b/examples/alarm_email.py index bc9ed46..233a4b5 100644 --- a/examples/alarm_email.py +++ b/examples/alarm_email.py @@ -24,18 +24,13 @@ def main(): # Set up an event handler and open the device device.on_alarm += handle_alarm - device.open() - - # Wait for events - while True: - time.sleep(1) + with device.open(): + while True: + time.sleep(1) except Exception, ex: print 'Exception:', ex - finally: - device.close() - def handle_alarm(sender, *args, **kwargs): """ Handles alarm events from the AD2. diff --git a/examples/basics.py b/examples/basics.py index 701765e..37bdfa9 100644 --- a/examples/basics.py +++ b/examples/basics.py @@ -12,18 +12,13 @@ def main(): # Set up an event handler and open the device device.on_message += handle_message - device.open() - - # Wait for events. - while True: - time.sleep(1) + with device.open(): + while True: + time.sleep(1) except Exception, ex: print 'Exception:', ex - finally: - device.close() - def handle_message(sender, *args, **kwargs): """ Handles message events from the AD2. diff --git a/examples/rf_device.py b/examples/rf_device.py index 0d76321..f9679a1 100644 --- a/examples/rf_device.py +++ b/examples/rf_device.py @@ -22,18 +22,13 @@ def main(): # Set up an event handler and open the device device.on_rfx_message += handle_rfx - device.open() - - # Wait for events. - while True: - time.sleep(1) + with device.open(): + while True: + time.sleep(1) except Exception, ex: print 'Exception:', ex - finally: - device.close() - def handle_rfx(sender, *args, **kwargs): """ Handles RF message events from the AD2. diff --git a/examples/serialport.py b/examples/serialport.py index 1f3e8d6..1a6ecc4 100644 --- a/examples/serialport.py +++ b/examples/serialport.py @@ -16,20 +16,16 @@ def main(): # Set up an event handler and open the device device.on_message += handle_message - device.open(baudrate=BAUDRATE) # Override the default SerialDevice - # baudrate since we're using a USB - # device over serial in this example. - # Wait for events. - while True: - time.sleep(1) + # Override the default SerialDevice baudrate since we're using a USB device + # over serial in this example. + with device.open(baudrate=BAUDRATE): + while True: + time.sleep(1) except Exception, ex: print 'Exception:', ex - finally: - device.close() - def handle_message(sender, *args, **kwargs): """ Handles message events from the AD2. diff --git a/examples/socket_example.py b/examples/socket_example.py index fec811f..f588c69 100644 --- a/examples/socket_example.py +++ b/examples/socket_example.py @@ -17,18 +17,13 @@ def main(): # Set up an event handler and open the device device.on_message += handle_message - device.open() - - # Wait for events. - while True: - time.sleep(1) + with device.open(): + while True: + time.sleep(1) except Exception, ex: print 'Exception:', ex - finally: - device.close() - def handle_message(sender, *args, **kwargs): """ Handles message events from the AD2. diff --git a/examples/ssl_socket.py b/examples/ssl_socket.py index e93d345..d0c9c23 100644 --- a/examples/ssl_socket.py +++ b/examples/ssl_socket.py @@ -31,18 +31,13 @@ def main(): # Set up an event handler and open the device device.on_message += handle_message - device.open() - - # Wait for events. - while True: - time.sleep(1) + with device.open(): + while True: + time.sleep(1) except Exception, ex: print 'Exception:', ex - finally: - device.close() - def handle_message(sender, *args, **kwargs): """ Handles message events from the AD2. diff --git a/examples/virtual_zone_expander.py b/examples/virtual_zone_expander.py index 1cdcecb..6a95be9 100644 --- a/examples/virtual_zone_expander.py +++ b/examples/virtual_zone_expander.py @@ -34,24 +34,19 @@ def main(): device.on_zone_fault += handle_zone_fault device.on_zone_restore += handle_zone_restore - device.open() + with device.open(): + last_update = time.time() + while True: + if time.time() - last_update > WAIT_TIME: + last_update = time.time() - # Wait for events. - last_update = time.time() - while True: - if time.time() - last_update > WAIT_TIME: - last_update = time.time() + device.fault_zone(TARGET_ZONE) - device.fault_zone(TARGET_ZONE) - - time.sleep(1) + time.sleep(1) except Exception, ex: print 'Exception:', ex - finally: - device.close() - def handle_zone_fault(sender, *args, **kwargs): """ Handles zone fault messages. diff --git a/pyad2/ad2.py b/pyad2/ad2.py index f937e09..e65772e 100644 --- a/pyad2/ad2.py +++ b/pyad2/ad2.py @@ -85,6 +85,20 @@ class AD2(object): self.emulate_lrr = False self.deduplicate = False + def __enter__(self): + """ + Support for context manager __enter__. + """ + return self + + def __exit__(self, type, value, traceback): + """ + Support for context manager __exit__. + """ + self.close() + + return False + @property def id(self): """ @@ -107,6 +121,8 @@ class AD2(object): self._device.open(baudrate=baudrate, no_reader_thread=no_reader_thread) self.get_config() + return self + def close(self): """ Closes the device. diff --git a/pyad2/devices.py b/pyad2/devices.py index 4938428..0de3b72 100644 --- a/pyad2/devices.py +++ b/pyad2/devices.py @@ -37,6 +37,20 @@ class Device(object): self._running = False self._read_thread = Device.ReadThread(self) + def __enter__(self): + """ + Support for context manager __enter__. + """ + return self + + def __exit__(self, type, value, traceback): + """ + Support for context manager __exit__. + """ + self.close() + + return False + @property def id(self): """ @@ -338,10 +352,12 @@ class USBDevice(Device): else: self._running = True + self.on_open() + if not no_reader_thread: self._read_thread.start() - self.on_open() + return self def close(self): """ @@ -630,6 +646,8 @@ class SerialDevice(Device): if not no_reader_thread: self._read_thread.start() + return self + def close(self): """ Closes the device. @@ -890,12 +908,13 @@ class SocketDevice(Device): else: self._running = True - self.on_open() if not no_reader_thread: self._read_thread.start() + return self + def close(self): """ Closes the device.