X-Git-Url: https://git.kaliko.me/?p=python-musicpd.git;a=blobdiff_plain;f=doc%2Fsource%2Fexamples%2Fexceptions.py;fp=doc%2Fsource%2Fexamples%2Fexceptions.py;h=1d10c325a69dd3a0fab3e3fab1e2f07a3a58bfd4;hp=0000000000000000000000000000000000000000;hb=9aa136ff3dc89f1f9a396f8404eb4c8064fa891c;hpb=2399137b973746fdc4286856e1d78c22c7fcaff1 diff --git a/doc/source/examples/exceptions.py b/doc/source/examples/exceptions.py new file mode 100644 index 0000000..1d10c32 --- /dev/null +++ b/doc/source/examples/exceptions.py @@ -0,0 +1,59 @@ +"""client class dealing with all Exceptions +""" +import logging + +import musicpd + + +# Wrap Exception decorator +def wrapext(func): + """Decorator to wrap errors in musicpd.MPDError""" + errors=(OSError, TimeoutError) + into = musicpd.MPDError + def w_func(*args, **kwargs): + try: + return func(*args, **kwargs) + except errors as err: + strerr = str(err) + if hasattr(err, 'strerror'): + if err.strerror: + strerr = err.strerror + raise into(strerr) from err + return w_func + + +class MyClient(musicpd.MPDClient): + """Plain client inheriting from MPDClient""" + + def __init__(self): + # Set logging to debug level + logging.basicConfig(level=logging.DEBUG, + format='%(levelname)-8s %(module)-10s %(message)s') + self.log = logging.getLogger(__name__) + super().__init__() + + @wrapext + def __getattr__(self, cmd): + """Wrapper around MPDClient calls for abstract overriding""" + self.log.debug('cmd: %s', cmd) + return super().__getattr__(cmd) + + +if __name__ == '__main__': + cli = MyClient() + # You can overrides host here or in init + #cli.host = 'example.org' + # Connect MPD server + try: + cli.connect() + cli.currentsong() + cli.stats() + except musicpd.MPDError as error: + cli.log.fatal(error) + finally: + cli.log.info('Disconnecting') + try: + # Tries to close the socket anyway + cli.disconnect() + except OSError: + pass