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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..514ad220905292303ec9fa114acd10fe07d9c716 |
| --- /dev/null |
| +++ b/chrome/common/extensions/docs/server2/template_data_source.py |
| @@ -0,0 +1,61 @@ |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +# 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 = {} |
| + self._base_paths = base_paths |
| + self._cache_timeout_seconds = cache_timeout_seconds |
| + |
| + def Render(self, template_name, context): |
| + template = self.get(template_name) |
| + if not template: |
| + return '' |
| + # TODO error handling |
| + return template.render(json.loads(context), {'templates': self}).text |
|
not at google - send to devlin
2012/06/08 01:42:53
nice!
You should add a quick comment around here
cduvall
2012/06/08 02:18:06
Done.
|
| + |
| + class _CachedTemplate(object): |
| + def __init__(self, template, cache_timeout_seconds): |
| + self.template = template |
| + self._expiry = time.time() + cache_timeout_seconds |
|
not at google - send to devlin
2012/06/08 01:42:53
I think that we should inject the expiry directly
cduvall
2012/06/08 02:18:06
Done.
|
| + |
| + def HasExpired(self): |
| + return time.time() > self._expiry |
| + |
| + def __getitem__(self, key): |
| + return self.get(key) |
| + |
| + def get(self, key): |
| + 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, |
| + self._cache_timeout_seconds) |
| + break |
| + except: |
| + pass |
| + |
| + return compiled_template |