summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvan Klitzke <evan@yelp.com>2010-11-10 16:39:48 -0800
committerR. Tyler Croy <tyler@monkeypox.org>2010-12-12 20:00:47 -0800
commit1f3e4ed66a343f21f6171e42b7222b5189a1fdce (patch)
tree5b8b328f3fb9a08b20537a6dc8868a4593a32327
parente8352935d01dbf1f20a8bc6aecc3c40592508435 (diff)
downloadpython-cheetah-1f3e4ed66a343f21f6171e42b7222b5189a1fdce.tar.gz
make it possible to force file encoding via the command line
-rw-r--r--cheetah/CheetahWrapper.py1
-rw-r--r--cheetah/Compiler.py10
2 files changed, 10 insertions, 1 deletions
diff --git a/cheetah/CheetahWrapper.py b/cheetah/CheetahWrapper.py
index 77a5696..a8a1b12 100644
--- a/cheetah/CheetahWrapper.py
+++ b/cheetah/CheetahWrapper.py
@@ -173,6 +173,7 @@ class CheetahWrapper(object):
pao("--templateAPIClass", action="store", dest="templateClassName", default=None, help='Name of a subclass of Cheetah.Template.Template to use for compilation, e.g. MyTemplateClass')
pao("--parallel", action="store", type="int", dest="parallel", default=1, help='Compile/fill templates in parallel, e.g. --parallel=4')
pao('--shbang', dest='shbang', default='#!/usr/bin/env python', help='Specify the shbang to place at the top of compiled templates, e.g. --shbang="#!/usr/bin/python2.6"')
+ pao('--encoding', dest='encoding', default=None, help='Specify the encoding of source files (e.g. \'utf-8\' to force input files to be interpreted as UTF-8)')
opts, files = self.parser.parse_args(args)
self.opts = opts
diff --git a/cheetah/Compiler.py b/cheetah/Compiler.py
index ee55868..3f23e4d 100644
--- a/cheetah/Compiler.py
+++ b/cheetah/Compiler.py
@@ -18,6 +18,7 @@ import time
import random
import warnings
import copy
+import codecs
from Cheetah.Version import Version, VersionTuple
from Cheetah.SettingsManager import SettingsManager
@@ -99,6 +100,7 @@ _DEFAULT_COMPILER_SETTINGS = [
('allowEmptySingleLineMethods', False, ''),
('allowNestedDefScopes', True, ''),
('allowPlaceholderFilterArgs', True, ''),
+ ('encoding', None, 'The encoding to read input files as (or None for ASCII)'),
]
DEFAULT_COMPILER_SETTINGS = dict([(v[0], v[1]) for v in _DEFAULT_COMPILER_SETTINGS])
@@ -1530,7 +1532,13 @@ class ModuleCompiler(SettingsManager, GenUtils):
if source and file:
raise TypeError("Cannot compile from a source string AND file.")
elif isinstance(file, basestring): # it's a filename.
- f = open(file) # Raises IOError.
+ encoding = settings.get('encoding')
+ if encoding:
+ f = codecs.open(file, 'r', encoding=encoding)
+ else:
+ f = open(file, 'r') # if no encoding is specified, use the
+ # builtin open function, which will
+ # effectively read data as a bytestream
source = f.read()
f.close()
self._filePath = file