Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import logging | |
| 6 import os | |
| 7 import time | |
| 8 | |
| 9 from third_party.handlebar import Handlebar | |
| 10 | |
| 11 class TemplateDataSource(object): | |
|
not at google - send to devlin
2012/06/07 04:43:57
I was thinking, should be build all the template r
cduvall
2012/06/08 00:39:23
Done.
| |
| 12 def __init__(self, fetcher, base_paths, cache_timeout): | |
|
not at google - send to devlin
2012/06/07 04:43:57
cache_timeout_seconds
cduvall
2012/06/08 00:39:23
Done.
| |
| 13 logging.info('Template data source created.') | |
|
not at google - send to devlin
2012/06/07 04:43:57
It's ok if you want to leave logging in for now (t
cduvall
2012/06/08 00:39:23
Done.
| |
| 14 self._fetcher = fetcher | |
| 15 self._template_cache = {} | |
| 16 self._base_paths = base_paths | |
| 17 self._cache_timeout = cache_timeout | |
| 18 | |
| 19 def __getitem__(self, key): | |
| 20 return self.get(key) | |
| 21 | |
| 22 def get(self, key): | |
| 23 index = key.rfind('.html') | |
| 24 if index > 0: | |
| 25 key = key[:index] | |
| 26 path = key + '.html' | |
| 27 if key in self._template_cache: | |
| 28 compiled_template, compile_time = self._template_cache[key] | |
| 29 if (time.time() - compile_time) > self._cache_timeout: | |
| 30 self._template_cache.pop(key) | |
| 31 if key not in self._template_cache: | |
| 32 logging.info('Template cache miss for: ' + path) | |
| 33 compiled_template = None | |
| 34 for base_path in self._base_paths: | |
| 35 try: | |
| 36 template = self._fetcher.FetchResource(base_path + path).content | |
| 37 compiled_template = Handlebar(template) | |
| 38 self._template_cache[key] = (compiled_template, time.time()) | |
|
not at google - send to devlin
2012/06/07 04:43:57
Consider making the cache entries actual objects t
cduvall
2012/06/08 00:39:23
Done.
| |
| 39 break | |
| 40 except: | |
| 41 pass | |
| 42 | |
| 43 return compiled_template | |
| OLD | NEW |