diff --git a/traktor_nowplaying/core.py b/traktor_nowplaying/core.py index 3c37ca8..0ef26f1 100644 --- a/traktor_nowplaying/core.py +++ b/traktor_nowplaying/core.py @@ -3,6 +3,7 @@ import functools import http.server import socketserver +import pathlib import types import io import os @@ -67,6 +68,17 @@ def __init__(self, port=PORT, quiet=QUIET, outfile=None, custom_callback=None): self.outfile = outfile self.custom_callback = custom_callback + def _create_outfile(self): + if not self.outfile: + return + outpath = pathlib.Path(self.outfile) + outpath.parent.mkdir(parents=True, exist_ok=True) + outpath.touch(exist_ok=True) + + if not outpath.is_file(): + print(f'{self.outfile} is a directory!') + raise IsADirectoryError + def start(self): """Start listening to Traktor broadcast.""" @@ -79,7 +91,12 @@ def start(self): callbacks.append(_output_to_console) if self.outfile: - callbacks.append(functools.partial(_output_to_file, outfile=self.outfile)) + try: + self._create_outfile() + callbacks.append(functools.partial(_output_to_file, outfile=self.outfile)) + except: + print(f'Error encountered while trying to write to {self.outfile}.') + return if self.custom_callback: callbacks.append(self.custom_callback) diff --git a/traktor_nowplaying/version.py b/traktor_nowplaying/version.py index b794fd4..df9144c 100644 --- a/traktor_nowplaying/version.py +++ b/traktor_nowplaying/version.py @@ -1 +1 @@ -__version__ = '0.1.0' +__version__ = '0.1.1'