Mercurial > genshi > genshi-test
comparison markup/template.py @ 22:31b13ddf9f53
Fix for the template engine plugin: the search path is now ignored if the requested template path is absolute.
author | cmlenz |
---|---|
date | Tue, 20 Jun 2006 15:10:24 +0000 |
parents | eca77129518a |
children | 00835401c8cc |
comparison
equal
deleted
inserted
replaced
21:eca77129518a | 22:31b13ddf9f53 |
---|---|
47 import re | 47 import re |
48 from StringIO import StringIO | 48 from StringIO import StringIO |
49 | 49 |
50 from markup.core import Attributes, Namespace, Stream, StreamEventKind | 50 from markup.core import Attributes, Namespace, Stream, StreamEventKind |
51 from markup.eval import Expression | 51 from markup.eval import Expression |
52 from markup.filters import IncludeFilter | |
53 from markup.input import HTML, XMLParser, XML | 52 from markup.input import HTML, XMLParser, XML |
54 from markup.path import Path | 53 from markup.path import Path |
55 | 54 |
56 __all__ = ['Context', 'BadDirectiveError', 'TemplateError', | 55 __all__ = ['Context', 'BadDirectiveError', 'TemplateError', |
57 'TemplateSyntaxError', 'TemplateNotFound', 'Template', | 56 'TemplateSyntaxError', 'TemplateNotFound', 'Template', |
871 self._mtime = {} | 870 self._mtime = {} |
872 | 871 |
873 def load(self, filename, relative_to=None): | 872 def load(self, filename, relative_to=None): |
874 """Load the template with the given name. | 873 """Load the template with the given name. |
875 | 874 |
876 This method searches the search path trying to locate a template | 875 If the `filename` parameter is relative, this method searches the search |
877 matching the given name. If no such template is found, a | 876 path trying to locate a template matching the given name. If the file |
878 `TemplateNotFound` exception is raised. Otherwise, a `Template` object | 877 name is an absolute path, the search path is not bypassed. |
879 representing the requested template is returned. | |
880 | 878 |
881 Template searches are cached to avoid having to parse the same template | 879 If requested template is not found, a `TemplateNotFound` exception is |
882 file more than once. Thus, subsequent calls of this method with the | 880 raised. Otherwise, a `Template` object is returned that represents the |
883 same template file name will return the same `Template` object. | 881 parsed template. |
882 | |
883 Template instances are cached to avoid having to parse the same | |
884 template file more than once. Thus, subsequent calls of this method | |
885 with the same template file name will return the same `Template` | |
886 object (unless the `auto_reload` option is enabled and the file was | |
887 changed since the last parse.) | |
884 | 888 |
885 If the `relative_to` parameter is provided, the `filename` is | 889 If the `relative_to` parameter is provided, the `filename` is |
886 interpreted as being relative to that path. | 890 interpreted as being relative to that path. |
887 | 891 |
888 @param filename: the relative path of the template file to load | 892 @param filename: the relative path of the template file to load |
891 directly | 895 directly |
892 """ | 896 """ |
893 if relative_to: | 897 if relative_to: |
894 filename = posixpath.join(posixpath.dirname(relative_to), filename) | 898 filename = posixpath.join(posixpath.dirname(relative_to), filename) |
895 filename = os.path.normpath(filename) | 899 filename = os.path.normpath(filename) |
900 | |
901 # First check the cache to avoid reparsing the same file | |
896 try: | 902 try: |
897 tmpl = self._cache[filename] | 903 tmpl = self._cache[filename] |
898 if not self.auto_reload or \ | 904 if not self.auto_reload or \ |
899 os.path.getmtime(tmpl.filepath) == self._mtime[filename]: | 905 os.path.getmtime(tmpl.filepath) == self._mtime[filename]: |
900 return tmpl | 906 return tmpl |
901 except KeyError: | 907 except KeyError: |
902 pass | 908 pass |
903 for dirname in self.search_path: | 909 |
910 # Bypass the search path if the filename is absolute | |
911 search_path = self.search_path | |
912 if os.path.isabs(filename): | |
913 search_path = [os.path.dirname(filename)] | |
914 | |
915 for dirname in search_path: | |
904 filepath = os.path.join(dirname, filename) | 916 filepath = os.path.join(dirname, filename) |
905 try: | 917 try: |
906 fileobj = file(filepath, 'rt') | 918 fileobj = file(filepath, 'rt') |
907 try: | 919 try: |
920 from markup.filters import IncludeFilter | |
908 tmpl = Template(fileobj, basedir=dirname, filename=filename) | 921 tmpl = Template(fileobj, basedir=dirname, filename=filename) |
909 tmpl.filters.append(IncludeFilter(self)) | 922 tmpl.filters.append(IncludeFilter(self)) |
910 finally: | 923 finally: |
911 fileobj.close() | 924 fileobj.close() |
912 self._cache[filename] = tmpl | 925 self._cache[filename] = tmpl |