Luke Plant avatar Luke Plant committed c167a4f

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

Comments (0)

Files changed (1)

bibleverses/writers.py

 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
         c.close()
     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:
             f.write(data.encode("UTF-8"))
-    return write_page
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.