Commit 7f76b6cc authored by Ryan Herbert's avatar Ryan Herbert Committed by Mikaël Salson

vidjilparser.py make file output optional

since we may not want to export to a file, leaving the filepath variable
at None will disable the file writing capabilities.
parent 01db44c6
......@@ -4,28 +4,31 @@ from six import string_types
class VidjilWriter(object):
def __init__(self, filepath, pretty=False):
def __init__(self, filepath=None, pretty=False):
self._filepath = filepath
self.pretty = pretty
self.buffer = ""
self.buffering = False
self.conserveBuffer = False
self.file = None
def __enter__(self):
self.file = open(self._filepath, 'w')
if self._filepath:
self.file = open(self._filepath, 'w')
return self
def __exit__(self, exc_type, exc_value, traceback):
self.file.close()
if self.file:
self.file.close()
def write(self, prefix, event, value, previous):
res = self._write(prefix, event, value, previous)
if self.buffering:
self.buffer += res
return ""
else:
elif self.file:
self.file.write(res)
return res
return res
def _write(self, prefix, event, value, previous):
if self.pretty:
......@@ -67,7 +70,8 @@ class VidjilWriter(object):
def writeBuffer(self):
try:
self.file.write(self.buffer)
if self.file:
self.file.write(self.buffer)
return self.buffer
finally:
self.purgeBuffer()
......@@ -106,7 +110,7 @@ class VidjilParser(object):
if writer is not None:
self._writer = writer
else:
self._writer = VidjilWriter('out.json')
self._writer = VidjilWriter()
self._model_prefixes = []
self.prefixes = []
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment