Skip to content

Commit

Permalink
Printer: Allow closing
Browse files Browse the repository at this point in the history
Now every printer can be closed, only ClosablePrinter's *need* to be
closed.
  • Loading branch information
sils committed Apr 15, 2015
1 parent eadb369 commit 49b8c23
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
13 changes: 0 additions & 13 deletions coalib/output/printers/ClosablePrinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,6 @@ class ClosablePrinter(Printer):
programmer will be warned, hopefully the user will never see any
functionality coming from this printer.
"""
def __init__(self):
Printer.__init__(self)

self._closed = False

def _close(self):
pass

def close(self):
if not self._closed:
self._close()
self._closed = True

def __del__(self): # pragma: no cover
"""
May warn if not closed properly - programmer error!
Expand Down
11 changes: 11 additions & 0 deletions coalib/output/printers/Printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ class Printer:
- EspeakPrinter for Voice output
- NullPrinter for no output
"""
def __init__(self):
self._closed = False

def _close(self):
pass

def close(self):
if not self._closed:
self._close()
self._closed = True

def _print(self, output, **kwargs):
"""
Expand All @@ -37,6 +47,7 @@ def print(self, *args, delimiter=' ', end='\n', **kwargs):
:param kwargs: Will be passed through to the Printer derivative
handling the actual printing
"""
assert not self._closed, "Cannot print after closing."
output = str(delimiter).join(str(arg) for arg in args) + str(end)

return self._print(output, **kwargs)
5 changes: 5 additions & 0 deletions coalib/tests/output/printers/PrinterTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def test_printer_interface(self):
self.uut = Printer()
self.assertRaises(NotImplementedError, self.uut.print, "test")

def test_closed_printing(self):
self.uut = TestPrinter()
self.uut.close()
self.assertRaises(AssertionError, self.uut.print)

def test_printer_concatenation(self):
self.uut = TestPrinter()
self.assertEqual(self.uut.print("hello",
Expand Down

0 comments on commit 49b8c23

Please sign in to comment.