Index: chrome/common/extensions/docs/server2/template_fetcher.py |
diff --git a/chrome/common/extensions/docs/server2/template_fetcher.py b/chrome/common/extensions/docs/server2/template_fetcher.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..62be1a6152a1d31fbf2f3289d89bfb051b8886aa |
--- /dev/null |
+++ b/chrome/common/extensions/docs/server2/template_fetcher.py |
@@ -0,0 +1,48 @@ |
+# 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 logging |
+import os |
+import time |
+ |
+from third_party.handlebar import Handlebar |
+ |
+# Cache templates for 5 minutes. |
+CACHE_TIMEOUT = 300 |
+ |
+# Cached templates stored as (branch, path) -> (template, cache time) |
+# The template cache is global so it will not be erased each time a new |
+# TemplateFetcher is created. |
+TEMPLATE_CACHE = {} |
not at google - send to devlin
2012/06/04 23:09:51
See comment below as to what this could be.
|
+ |
+class TemplateFetcher(object): |
+ def __init__(self, fetcher): |
+ self.fetcher = fetcher |
not at google - send to devlin
2012/06/04 23:09:51
should be _fetcher
|
+ |
+ def __getitem__(self, key): |
+ if key not in TEMPLATE_CACHE: |
+ self.FetchTemplate(key[0], key[1]) |
+ return TEMPLATE_CACHE[key][0] |
+ |
+ 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
|
+ template_dict = {} |
+ for k, v in TEMPLATE_CACHE.iteritems(): |
+ # For each template in the branch, add the template to the dictionary with |
+ # its name as the key. This can be used with Handlebar for partials. |
+ if k[0] == branch: |
+ template_dict[os.path.basename(k[1])] = v[0] |
+ |
+ return template_dict |
+ |
+ def FetchTemplate(self, branch, path): |
+ key = (branch, path) |
+ # Check the template cache and whether the cache has expired. |
+ if key in TEMPLATE_CACHE: |
+ if TEMPLATE_CACHE[key][1] < CACHE_TIMEOUT: |
+ return TEMPLATE_CACHE[key][0] |
+ logging.info('Template cache miss for: ' + path) |
+ 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
|
+ compiled_template = Handlebar(template) |
+ TEMPLATE_CACHE[key] = (compiled_template, time.clock()) |
+ return compiled_template |