Permalink
Browse files

Improved SIGINT handler. ref #46

  • Loading branch information...
1 parent b9dc2fb commit 2310534492f7d8573502ae0900a39f81455aaeeb @SeyZ committed Aug 12, 2012
Showing with 22 additions and 14 deletions.
  1. +16 −8 baboon/main.py
  2. +6 −6 bin/baboon
View
@@ -14,7 +14,9 @@
class Main(object):
def __init__(self):
- self.monitor = None
+
+ # Exit baboon when receiving a sigint signal.
+ signal.signal(signal.SIGINT, self.sigint_handler)
self.which = config['parser']['which']
@@ -28,6 +30,9 @@ def __init__(self):
if hasattr(commands, self.which):
getattr(commands, self.which)()
+ # TODO this won't work on windows...
+ signal.pause()
+
def check_config(self):
"""Some sections and options of the config file are mandatory. Let's be
sure they are at least filled. We leave the "filled correctly" to the
@@ -83,19 +88,16 @@ def check_config(self):
return success
def start(self):
+ self.monitor = None
+
try:
- # exists baboon when receiving a sigint signal
- signal.signal(signal.SIGINT, self.sigint_handler)
self.transport = WatchTransport()
self.transport.open()
self.monitor = Monitor(self.transport)
self.monitor.watch()
- # TODO this won't work on windows...
- signal.pause()
-
except BaboonException, err:
sys.stderr.write("%s\n" % err)
# Try to close the transport properly. If the transport is
@@ -115,8 +117,14 @@ def sigint_handler(self, signal, frame):
Closing baboon in a clean way.
"""
self.logger.debug("Received SIGINT signal")
- self.transport.close()
- self.monitor.close()
+
+ try:
+ self.transport.close()
+ self.monitor.close()
+ except AttributeError:
+ # If AttributeError is raise, transport or monitor does not exist.
+ # It's not a problem.
+ pass
self.logger.info("Bye !")
sys.exit(0)
View
@@ -1,9 +1,9 @@
#!/usr/bin/env python
-import os
-import sys
+try:
+ from baboon.main import Main
-from baboon.main import Main
-
-if __name__ == "__main__":
- Main()
+ if __name__ == "__main__":
+ Main()
+except KeyboardInterrupt:
+ pass

0 comments on commit 2310534

Please sign in to comment.