Commit ba04172d by Chris Jerdonek

Added module pystache.common with a read(path) function.

parent 75030e7d
# coding: utf-8
"""
Exposes common functions.
"""
# This function was designed to be portable across Python versions -- both
# with older versions and with Python 3 after applying 2to3.
def read(path):
"""
Return the contents of a text file as a byte string.
"""
# Opening in binary mode is necessary for compatibility across Python
# 2 and 3. In both Python 2 and 3, open() defaults to opening files in
# text mode. However, in Python 2, open() returns file objects whose
# read() method returns byte strings (strings of type `str` in Python 2),
# whereas in Python 3, the file object returns unicode strings (strings
# of type `str` in Python 3).
f = open(path, 'rb')
# We avoid use of the with keyword for Python 2.4 support.
try:
return f.read()
finally:
f.close()
......@@ -8,6 +8,7 @@ This module provides a Loader class for locating and reading templates.
import os
import sys
from pystache import common
from pystache import defaults
from pystache.locator import Locator
......@@ -106,17 +107,12 @@ class Loader(object):
Read the template at the given path, and return it as a unicode string.
"""
# We avoid use of the with keyword for Python 2.4 support.
f = open(path, 'r')
try:
text = f.read()
finally:
f.close()
b = common.read(path)
if encoding is None:
encoding = self.file_encoding
return self.unicode(text, encoding)
return self.unicode(b, encoding)
# TODO: unit-test this method.
def load_name(self, name):
......
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