|
| 1 | +#! /usr/bin/env python |
| 2 | +# |
| 3 | +#Introduction |
| 4 | +# |
| 5 | +#A python program that continuously polls for song info. Demonstrates how and where to handle errors |
| 6 | +#Details |
| 7 | +# |
| 8 | + |
| 9 | +from mpd import MPDClient, MPDError, CommandError |
| 10 | +import sys |
| 11 | + |
| 12 | + |
| 13 | +class PollerError(Exception): |
| 14 | + """Fatal error in poller.""" |
| 15 | + |
| 16 | + |
| 17 | +class MPDPoller(object): |
| 18 | + def __init__(self, host="localhost", port="6600", password=None): |
| 19 | + self._host = host |
| 20 | + self._port = port |
| 21 | + self._password = password |
| 22 | + self._client = MPDClient() |
| 23 | + |
| 24 | + def connect(self): |
| 25 | + try: |
| 26 | + self._client.connect(self._host, self._port) |
| 27 | + # Catch socket errors |
| 28 | + except IOError as err: |
| 29 | + errno, strerror = err |
| 30 | + raise PollerError("Could not connect to '%s': %s" % |
| 31 | + (self._host, strerror)) |
| 32 | + |
| 33 | + # Catch all other possible errors |
| 34 | + # ConnectionError and ProtocolError are always fatal. Others may not |
| 35 | + # be, but we don't know how to handle them here, so treat them as if |
| 36 | + # they are instead of ignoring them. |
| 37 | + except MPDError as e: |
| 38 | + raise PollerError("Could not connect to '%s': %s" % |
| 39 | + (self._host, e)) |
| 40 | + |
| 41 | + if self._password: |
| 42 | + try: |
| 43 | + self._client.password(self._password) |
| 44 | + |
| 45 | + # Catch errors with the password command (e.g., wrong password) |
| 46 | + except CommandError as e: |
| 47 | + raise PollerError("Could not connect to '%s': " |
| 48 | + "password commmand failed: %s" % |
| 49 | + (self._host, e)) |
| 50 | + |
| 51 | + # Catch all other possible errors |
| 52 | + except (MPDError, IOError) as e: |
| 53 | + raise PollerError("Could not connect to '%s': " |
| 54 | + "error with password command: %s" % |
| 55 | + (self._host, e)) |
| 56 | + |
| 57 | + def disconnect(self): |
| 58 | + # Try to tell MPD we're closing the connection first |
| 59 | + try: |
| 60 | + self._client.close() |
| 61 | + |
| 62 | + # If that fails, don't worry, just ignore it and disconnect |
| 63 | + except (MPDError, IOError): |
| 64 | + pass |
| 65 | + |
| 66 | + try: |
| 67 | + self._client.disconnect() |
| 68 | + |
| 69 | + # Disconnecting failed, so use a new client object instead |
| 70 | + # This should never happen. If it does, something is seriously broken, |
| 71 | + # and the client object shouldn't be trusted to be re-used. |
| 72 | + except (MPDError, IOError): |
| 73 | + self._client = MPDClient() |
| 74 | + |
| 75 | + def poll(self): |
| 76 | + try: |
| 77 | + song = self._client.currentsong() |
| 78 | + |
| 79 | + # Couldn't get the current song, so try reconnecting and retrying |
| 80 | + except (MPDError, IOError): |
| 81 | + # No error handling required here |
| 82 | + # Our disconnect function catches all exceptions, and therefore |
| 83 | + # should never raise any. |
| 84 | + self.disconnect() |
| 85 | + |
| 86 | + try: |
| 87 | + self.connect() |
| 88 | + |
| 89 | + # Reconnecting failed |
| 90 | + except PollerError as e: |
| 91 | + raise PollerError("Reconnecting failed: %s" % e) |
| 92 | + |
| 93 | + try: |
| 94 | + song = self._client.currentsong() |
| 95 | + |
| 96 | + # Failed again, just give up |
| 97 | + except (MPDError, IOError) as e: |
| 98 | + raise PollerError("Couldn't retrieve current song: %s" % e) |
| 99 | + |
| 100 | + # Hurray! We got the current song without any errors! |
| 101 | + print(song) |
| 102 | + |
| 103 | + |
| 104 | +def main(): |
| 105 | + from time import sleep |
| 106 | + |
| 107 | + poller = MPDPoller() |
| 108 | + poller.connect() |
| 109 | + |
| 110 | + while True: |
| 111 | + poller.poll() |
| 112 | + sleep(3) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + import sys |
| 117 | + |
| 118 | + try: |
| 119 | + main() |
| 120 | + |
| 121 | + # Catch fatal poller errors |
| 122 | + except PollerError as e: |
| 123 | + print("Fatal poller error: %s" % e, file=sys.stderr) |
| 124 | + sys.exit(1) |
| 125 | + |
| 126 | + # Catch all other non-exit errors |
| 127 | + except Exception as e: |
| 128 | + print("Unexpected exception: %s" % e, file=sys.stderr) |
| 129 | + sys.exit(1) |
| 130 | + |
| 131 | + # Catch the remaining exit errors |
| 132 | + except: |
| 133 | + sys.exit(0) |
| 134 | + |
| 135 | + |
| 136 | +# vim: set expandtab shiftwidth=4 softtabstop=4 textwidth=79: |
0 commit comments