Context Manager for capture keyboard interrupt Args: callback: function Callback function when KeyboardInterrupt occurs. Examples: >>> with CaptureKeyboardInterrupt(callback): >>> do_somethings()
| 295 | |
| 296 | |
| 297 | class CaptureKeyboardInterrupt(object): |
| 298 | """Context Manager for capture keyboard interrupt |
| 299 | |
| 300 | Args: |
| 301 | callback: function |
| 302 | Callback function when KeyboardInterrupt occurs. |
| 303 | |
| 304 | Examples: |
| 305 | >>> with CaptureKeyboardInterrupt(callback): |
| 306 | >>> do_somethings() |
| 307 | """ |
| 308 | |
| 309 | def __init__(self, callback=None): |
| 310 | self._callback = callback |
| 311 | |
| 312 | def __enter__(self): |
| 313 | return self |
| 314 | |
| 315 | def __exit__(self, exc_type, exc_value, exc_tb): |
| 316 | if exc_type is not None: |
| 317 | if self._callback: |
| 318 | try: |
| 319 | self._callback() |
| 320 | except: # noqa: E722 |
| 321 | pass |
| 322 | return False |
| 323 | |
| 324 | |
| 325 | class SignalIgnore(object): |