summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorianb <devnull@localhost>2007-11-04 04:11:04 +0000
committerianb <devnull@localhost>2007-11-04 04:11:04 +0000
commit630224ecd88e57faf67679ced9c42a6454221990 (patch)
tree4b36c79aca0cef2a5744cd01c0c98895e4c61354
parent30c8a8f273ae155310f6ddb3dcb8ca5092c682e8 (diff)
downloadtempita-630224ecd88e57faf67679ced9c42a6454221990.tar.gz
Add the ability to grab the location of a template that is a static string
-rw-r--r--tempita/__init__.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/tempita/__init__.py b/tempita/__init__.py
index 4860adb..aa6cd8f 100644
--- a/tempita/__init__.py
+++ b/tempita/__init__.py
@@ -74,9 +74,27 @@ class Template(object):
default_encoding = 'utf8'
- def __init__(self, content, name=None, namespace=None):
+ def __init__(self, content, name=None, namespace=None, stacklevel=None):
self.content = content
self._unicode = isinstance(content, unicode)
+ if name is None and stacklevel is not None:
+ try:
+ caller = sys._getframe(stacklevel)
+ except ValueError:
+ pass
+ else:
+ globals = caller.f_globals
+ lineno = caller.f_lineno
+ if '__file__' in globals:
+ name = globals['__file__']
+ if name.endswith('.pyc') or name.endswith('.pyo'):
+ name = name[:-1]
+ elif '__name__' in globals:
+ name = globals['__name__']
+ else:
+ name = '<string>'
+ if lineno:
+ name += ':%s' % lineno
self.name = name
self._parsed = parse(content, name=name)
if namespace is None:
@@ -112,6 +130,7 @@ class Template(object):
% (args[0],))
kw = args[0]
ns = self.default_namespace.copy()
+ ns['__name__'] = self.name
ns.update(self.namespace)
ns.update(kw)
result = self._interpret(ns)