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 # Cache templates for 5 minutes. | |
12 CACHE_TIMEOUT = 300 | |
13 | |
14 # Cached templates stored as (branch, path) -> (template, cache time) | |
15 # The template cache is global so it will not be erased each time a new | |
16 # TemplateFetcher is created. | |
17 TEMPLATE_CACHE = {} | |
not at google - send to devlin
2012/06/04 23:09:51
See comment below as to what this could be.
| |
18 | |
19 class TemplateFetcher(object): | |
20 def __init__(self, fetcher): | |
21 self.fetcher = fetcher | |
not at google - send to devlin
2012/06/04 23:09:51
should be _fetcher
| |
22 | |
23 def __getitem__(self, key): | |
24 if key not in TEMPLATE_CACHE: | |
25 self.FetchTemplate(key[0], key[1]) | |
26 return TEMPLATE_CACHE[key][0] | |
27 | |
28 def AsDict(self, branch): | |
not at google - send to devlin
2012/06/04 23:09:51
Yeah, this won't play so well with plugging stuff
| |
29 template_dict = {} | |
30 for k, v in TEMPLATE_CACHE.iteritems(): | |
31 # For each template in the branch, add the template to the dictionary with | |
32 # its name as the key. This can be used with Handlebar for partials. | |
33 if k[0] == branch: | |
34 template_dict[os.path.basename(k[1])] = v[0] | |
35 | |
36 return template_dict | |
37 | |
38 def FetchTemplate(self, branch, path): | |
39 key = (branch, path) | |
40 # Check the template cache and whether the cache has expired. | |
41 if key in TEMPLATE_CACHE: | |
42 if TEMPLATE_CACHE[key][1] < CACHE_TIMEOUT: | |
43 return TEMPLATE_CACHE[key][0] | |
44 logging.info('Template cache miss for: ' + path) | |
45 template = self.fetcher.FetchResource(branch, path).content | |
not at google - send to devlin
2012/06/04 23:09:51
Something to think about: this 5 minute thing is a
| |
46 compiled_template = Handlebar(template) | |
47 TEMPLATE_CACHE[key] = (compiled_template, time.clock()) | |
48 return compiled_template | |
OLD | NEW |