Skip to content

Commit

Permalink
Create directories as needed and check that outfile can be written to
Browse files Browse the repository at this point in the history
  • Loading branch information
radusuciu committed May 8, 2020
1 parent 99bf323 commit 7ebd21d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 18 additions & 1 deletion traktor_nowplaying/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import functools
import http.server
import socketserver
import pathlib
import types
import io
import os
Expand Down Expand Up @@ -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."""

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion traktor_nowplaying/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.0'
__version__ = '0.1.1'

0 comments on commit 7ebd21d

Please sign in to comment.