Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/template_data_source.py |
| diff --git a/chrome/common/extensions/docs/server2/template_data_source.py b/chrome/common/extensions/docs/server2/template_data_source.py |
| index 003cbf097425a227fadb5a37e2b6c17cf556d6d1..d836fbf475a6027a955847aad613b7a22c4dcf9d 100644 |
| --- a/chrome/common/extensions/docs/server2/template_data_source.py |
| +++ b/chrome/common/extensions/docs/server2/template_data_source.py |
| @@ -2,21 +2,18 @@ |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| -import json |
| -import logging |
| -import os |
| -import time |
| - |
| from third_party.handlebar import Handlebar |
| class TemplateDataSource(object): |
| - def __init__(self, fetcher, base_paths, cache_timeout_seconds): |
| - logging.info('Template data source created: %s %d' % |
| - (' '.join(base_paths), cache_timeout_seconds)) |
| - self._fetcher = fetcher |
| - self._template_cache = {} |
| + """This class fetches and compiles templates using the fetcher passed in with |
| + |cache_builder|. |
| + """ |
| + def _LoadTemplate(self, template): |
| + return Handlebar(template) |
|
not at google - send to devlin
2012/06/18 19:30:40
nit: either make this static / pull outside the cl
cduvall
2012/06/18 19:48:46
Done.
|
| + |
| + def __init__(self, cache_builder, base_paths): |
| + self._cache = cache_builder.build(self._LoadTemplate) |
| self._base_paths = base_paths |
| - self._cache_timeout_seconds = cache_timeout_seconds |
| def Render(self, template_name, context): |
| """This method will render a template named |template_name|, fetching all |
| @@ -27,15 +24,7 @@ class TemplateDataSource(object): |
| if not template: |
| return '' |
| # TODO error handling |
| - return template.render(json.loads(context), {'templates': self}).text |
| - |
| - class _CachedTemplate(object): |
| - def __init__(self, template, expiry): |
| - self.template = template |
| - self._expiry = expiry |
| - |
| - def HasExpired(self): |
| - return time.time() > self._expiry |
| + return template.render(context, {'templates': self}).text |
| def __getitem__(self, key): |
| return self.get(key) |
| @@ -44,22 +33,10 @@ class TemplateDataSource(object): |
| index = key.rfind('.html') |
| if index > 0: |
| key = key[:index] |
| - path = key + '.html' |
| - if key in self._template_cache: |
| - if self._template_cache[key].HasExpired(): |
| - self._template_cache.pop(key) |
| - if key not in self._template_cache: |
| - logging.info('Template cache miss for: ' + path) |
| - compiled_template = None |
| - for base_path in self._base_paths: |
| - try: |
| - template = self._fetcher.FetchResource(base_path + path).content |
| - compiled_template = Handlebar(template) |
| - self._template_cache[key] = self._CachedTemplate( |
| - compiled_template, |
| - time.time() + self._cache_timeout_seconds) |
| - break |
| - except: |
| - pass |
| - |
| - return compiled_template |
| + real_path = key + '.html' |
| + for base_path in self._base_paths: |
| + try: |
| + return self._cache.get(base_path + '/' + real_path) |
| + except: |
| + pass |
| + return None |