changeset 7:e3d52edde00b

Make renderers write to a provided output stream.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 04 Jan 2017 01:21:55 -0800
parents 486c2349598e
children 02d2e4d8b0c1
files fontaine/console.py fontaine/renderer.py
diffstat 2 files changed, 60 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/fontaine/console.py	Tue Jan 03 16:41:02 2017 -0800
+++ b/fontaine/console.py	Wed Jan 04 01:21:55 2017 -0800
@@ -1,11 +1,10 @@
 import os
-import sys
 import colorama
 from .renderer import BaseDocumentRenderer, BaseTextRenderer
 
 
-def _w(style, text, reset_all=False):
-    f = sys.stdout.write
+def _w(out, style, text, reset_all=False):
+    f = out.write
     f(style)
     f(text)
     if not reset_all:
@@ -21,53 +20,53 @@
         self.width = width
         colorama.init()
 
-    def write_title_heading(self, text):
-        print("")
+    def write_title_heading(self, text, out):
+        print("", file=out)
         for line in text.split('\n'):
-            print(line.center(self.width))
-        print("")
-        print("")
+            print(line.center(self.width), file=out)
+        print("", file=out)
+        print("", file=out)
 
-    def write_title_footer(self, text):
-        _w(colorama.Style.DIM, text)
-        print("")
-        _w(colorama.Style.DIM, 80 * '=')
+    def write_title_footer(self, text, out):
+        _w(out, colorama.Style.DIM, text)
+        print("", file=out)
+        _w(out, colorama.Style.DIM, 80 * '=')
 
-    def write_scene_heading(self, text):
-        print("")
-        _w(colorama.Fore.WHITE + colorama.Style.BRIGHT, text, True)
+    def write_scene_heading(self, text, out):
+        print("", file=out)
+        _w(out, colorama.Fore.WHITE + colorama.Style.BRIGHT, text, True)
 
-    def write_action(self, text):
-        print(text)
+    def write_action(self, text, out):
+        print(text, file=out)
 
-    def write_centeredaction(self, text):
-        print("")
+    def write_centeredaction(self, text, out):
+        print("", file=out)
         for line in text.split('\n'):
-            print(line.center(self.width))
+            print(line.center(self.width), file=out)
 
-    def write_character(self, text):
-        print("")
-        _w(colorama.Fore.WHITE, "\t\t\t" + text, True)
+    def write_character(self, text, out):
+        print("", file=out)
+        _w(out, colorama.Fore.WHITE, "\t\t\t" + text, True)
 
-    def write_dialog(self, text):
+    def write_dialog(self, text, out):
         for line in text.split('\n'):
-            print("\t" + line)
+            print("\t" + line, file=out)
 
-    def write_parenthetical(self, text):
+    def write_parenthetical(self, text, out):
         for line in text.split('\n'):
-            print("\t\t" + line)
+            print("\t\t" + line, file=out)
 
-    def write_transition(self, text):
-        print("")
-        print("\t\t\t\t" + text)
+    def write_transition(self, text, out):
+        print("", file=out)
+        print("\t\t\t\t" + text, file=out)
 
-    def write_lyrics(self, text):
-        print("")
-        _w(colorama.Fore.MAGENTA, text, True)
+    def write_lyrics(self, text, out):
+        print("", file=out)
+        _w(out, colorama.Fore.MAGENTA, text, True)
 
-    def write_pagebreak(self):
-        print("")
-        _w(colorama.Style.DIM, 80 * '=')
+    def write_pagebreak(self, out):
+        print("", file=out)
+        _w(out, colorama.Style.DIM, 80 * '=')
 
 
 class ConsoleTextRenderer(BaseTextRenderer):
--- a/fontaine/renderer.py	Tue Jan 03 16:41:02 2017 -0800
+++ b/fontaine/renderer.py	Wed Jan 04 01:21:55 2017 -0800
@@ -24,14 +24,14 @@
     def _tr(self, text):
         return self.text_renderer.render_text(text)
 
-    def render_doc(self, doc):
-        self.write_header(doc)
-        self.render_title_page(doc.title_values)
+    def render_doc(self, doc, out):
+        self.write_header(doc, out)
+        self.render_title_page(doc.title_values, out)
         for s in doc.scenes:
-            self.render_scene(s)
-        self.write_footer(doc)
+            self.render_scene(s, out)
+        self.write_footer(doc, out)
 
-    def render_title_page(self, values):
+    def render_title_page(self, values, out):
         # Render known metadata.
         title = values.get('title')
         credit = values.get('credit')
@@ -40,62 +40,62 @@
         center_text = '\n\n'.join([
             i for i in [title, credit, author, source]
             if i is not None])
-        self.write_title_heading(self._tr(center_text))
+        self.write_title_heading(self._tr(center_text), out)
 
         ddate = values.get('date') or values.get('draft date')
         contact = values.get('contact')
         bottom_text = '\n\n'.join([
             i for i in [ddate, contact]
             if i is not None])
-        self.write_title_footer(self._tr(bottom_text))
+        self.write_title_footer(self._tr(bottom_text), out)
 
-    def render_scene(self, scene):
+    def render_scene(self, scene, out):
         if scene.header is not None:
-            self.write_scene_heading(scene.header)
+            self.write_scene_heading(scene.header, out)
         for p in scene.paragraphs:
             rdr_func = self._para_rdrs[p.type]
             if p.type != TYPE_PAGEBREAK:
-                rdr_func(self._tr(p.text))
+                rdr_func(self._tr(p.text), out)
             else:
-                rdr_func()
+                rdr_func(out)
 
-    def write_header(self, doc):
+    def write_header(self, doc, out):
         pass
 
-    def write_footer(self, doc):
+    def write_footer(self, doc, out):
         pass
 
-    def write_title_heading(self, text):
+    def write_title_heading(self, text, out):
         raise NotImplementedError()
 
-    def write_title_footer(self, text):
+    def write_title_footer(self, text, out):
         raise NotImplementedError()
 
-    def write_scene_heading(self, text):
+    def write_scene_heading(self, text, out):
         raise NotImplementedError()
 
-    def write_action(self, text):
+    def write_action(self, text, out):
         raise NotImplementedError()
 
-    def write_centeredaction(self, text):
+    def write_centeredaction(self, text, out):
         raise NotImplementedError()
 
-    def write_character(self, text):
+    def write_character(self, text, out):
         raise NotImplementedError()
 
-    def write_dialog(self, text):
+    def write_dialog(self, text, out):
         raise NotImplementedError()
 
-    def write_parenthetical(self, text):
+    def write_parenthetical(self, text, out):
         raise NotImplementedError()
 
-    def write_transition(self, text):
+    def write_transition(self, text, out):
         raise NotImplementedError()
 
-    def write_lyrics(self, text):
+    def write_lyrics(self, text, out):
         raise NotImplementedError()
 
-    def write_pagebreak(self):
+    def write_pagebreak(self, out):
         raise NotImplementedError()