Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Side by Side Diff: chrome/common/extensions/docs/server2/template_fetcher.py

Issue 10500004: Die build.py, Die: Part 2 (LocalFetcher, Handlebar support, build script) (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Local is now a branch Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698