1. Luke Plant
  2. lukeplant_python


Luke Plant  committed c167a4f

Reimplemented FileWriter in order to give access to 'make_html_filename' as a public static method

  • Participants
  • Parent commits 7118197
  • Branches default

Comments (0)

Files changed (1)

File bibleverses/writers.py

View file
 import sqlite3
 # Defines factory functions for returning writers that can write
-# data to different backends
+# data to different backends.  The writer objects returned are
+# callables that take the page name and the data and write it out.
 def TracWikiWriter(dbname, author):
     """Returns a writer that can write to the given trac database, and uses
     return write_page
-def FileWriter(dirname):
-    """Returns a writer that writes to files in a directory.
+class FileWriter(object):
+    """A writer that writes to files in a directory.
     Filenames are converted to lowercase, and 'BibleMemorisation'
     is stripped from the front of names."""
-    def write_page(name, data):
-        stem = 'BibleMemorisation'
-        assert name.startswith(stem)
-        name = name[len(stem):].lower() + ".html"
+    # Implemented as a class as 'make_html_filename' is needed
+    # elsewhere
+    def __init__(self, dirname):
+        self.dirname = dirname
+    filenamestem = 'BibleMemorisation'
+    @staticmethod
+    def make_html_filename(pagename):
+        stem = FileWriter.filenamestem
+        assert pagename.startswith(stem)
         # we deliberately leave "/" in, allowing for subdirectories.
         # This will fail if dirs do not exist already
-        with file(os.path.join(dirname, name), "w") as f:
+        return pagename[len(stem):].lower() + ".html"
+    def __call__(self, name, data):
+        filename = self.make_html_filename(name)
+        with file(os.path.join(self.dirname, filename), "w") as f:
-    return write_page