Edgewall Software

Changeset 1099


Ignore:
Timestamp:
Apr 15, 2010, 11:07:15 PM (13 years ago)
Author:
cmlenz
Message:

Templates instantiated without a loader now get an implicit loader based on their file path, or the current directory as a fallback. Closes #320.

Location:
trunk/genshi/template
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/genshi/template/base.py

    r1083 r1099  
    396396        self.allow_exec = allow_exec
    397397        self._init_filters()
     398        self._init_loader()
    398399        self._prepared = False
    399400
     
    420421
    421422    def _init_filters(self):
    422         self.filters = [self._flatten]
    423         if self.loader:
    424             self.filters.append(self._include)
     423        self.filters = [self._flatten, self._include]
     424
     425    def _init_loader(self):
     426        if self.loader is None:
     427            from genshi.template.loader import TemplateLoader
     428            if self.filename:
     429                if self.filepath != self.filename:
     430                    basedir = os.path.normpath(self.filepath)[:-len(
     431                        os.path.normpath(self.filename))
     432                    ]
     433                else:
     434                    basedir = os.path.dirname(self.filename)
     435            else:
     436                basedir = '.'
     437            self.loader = TemplateLoader([os.path.abspath(basedir)])
    425438
    426439    @property
  • trunk/genshi/template/loader.py

    r1077 r1099  
    130130        self._cache = LRUCache(max_cache_size)
    131131        self._uptodate = {}
     132        self._lock = threading.RLock()
     133
     134    def __getstate__(self):
     135        state = self.__dict__.copy()
     136        state['_lock'] = None
     137        return state
     138
     139    def __setstate__(self, state):
     140        self.__dict__ = state
    132141        self._lock = threading.RLock()
    133142
  • trunk/genshi/template/markup.py

    r1097 r1099  
    7171        Template._init_filters(self)
    7272        # Make sure the include filter comes after the match filter
    73         if self.loader:
    74             self.filters.remove(self._include)
    75         self.filters += [self._match]
    76         if self.loader:
    77             self.filters.append(self._include)
     73        self.filters.remove(self._include)
     74        self.filters += [self._match, self._include]
    7875
    7976    def _parse(self, source, encoding):
  • trunk/genshi/template/tests/loader.py

    r1088 r1099  
    150150        loader = TemplateLoader()
    151151        tmpl = loader.load(os.path.join(self.dirname, 'tmpl2.html'))
     152        self.assertEqual("""<html>
     153              <div>Included</div>
     154            </html>""", tmpl.generate().render(encoding=None))
     155
     156    def test_relative_include_without_loader(self):
     157        file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
     158        try:
     159            file1.write("""<div>Included</div>""")
     160        finally:
     161            file1.close()
     162
     163        file2 = open(os.path.join(self.dirname, 'tmpl2.html'), 'w')
     164        try:
     165            file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
     166              <xi:include href="tmpl1.html" />
     167            </html>""")
     168        finally:
     169            file2.close()
     170
     171        tmpl = MarkupTemplate("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
     172              <xi:include href="tmpl1.html" />
     173            </html>""", os.path.join(self.dirname, 'tmpl2.html'), 'tmpl2.html')
     174        self.assertEqual("""<html>
     175              <div>Included</div>
     176            </html>""", tmpl.generate().render(encoding=None))
     177
     178    def test_relative_include_without_loader_relative(self):
     179        file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
     180        try:
     181            file1.write("""<div>Included</div>""")
     182        finally:
     183            file1.close()
     184
     185        file2 = open(os.path.join(self.dirname, 'tmpl2.html'), 'w')
     186        try:
     187            file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
     188              <xi:include href="tmpl1.html" />
     189            </html>""")
     190        finally:
     191            file2.close()
     192
     193        tmpl = MarkupTemplate("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
     194              <xi:include href="tmpl1.html" />
     195            </html>""", filename=os.path.join(self.dirname, 'tmpl2.html'))
    152196        self.assertEqual("""<html>
    153197              <div>Included</div>
Note: See TracChangeset for help on using the changeset viewer.