| 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..a2e1b69a92dace03f3e80173d2ae64249e4e4cce 100644
|
| --- a/chrome/common/extensions/docs/server2/template_data_source.py
|
| +++ b/chrome/common/extensions/docs/server2/template_data_source.py
|
| @@ -2,40 +2,29 @@
|
| # 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 __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 _LoadTemplate(self, template):
|
| + return Handlebar(template)
|
|
|
| def Render(self, template_name, context):
|
| """This method will render a template named |template_name|, fetching all
|
| - the partial templates needed with |self._fetcher|. Partials are retrieved
|
| + the partial templates needed from |self._cache|. Partials are retrieved
|
| from the TemplateDataSource with the |get| method.
|
| """
|
| template = self.get(template_name)
|
| 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
|
|
|