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

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

Issue 10545043: Extensions docs server: Design changes, partial template support. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments 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 | Annotate | Revision Log
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 json
6 import logging
7 import os
8 import time
9
10 from third_party.handlebar import Handlebar
11
12 class TemplateDataSource(object):
13 def __init__(self, fetcher, base_paths, cache_timeout_seconds):
14 logging.info('Template data source created: %s %d' %
15 (' '.join(base_paths), cache_timeout_seconds))
16 self._fetcher = fetcher
17 self._template_cache = {}
18 self._base_paths = base_paths
19 self._cache_timeout_seconds = cache_timeout_seconds
20
21 def Render(self, template_name, context):
22 template = self.get(template_name)
23 if not template:
24 return ''
25 # TODO error handling
26 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.
27
28 class _CachedTemplate(object):
29 def __init__(self, template, cache_timeout_seconds):
30 self.template = template
31 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.
32
33 def HasExpired(self):
34 return time.time() > self._expiry
35
36 def __getitem__(self, key):
37 return self.get(key)
38
39 def get(self, key):
40 index = key.rfind('.html')
41 if index > 0:
42 key = key[:index]
43 path = key + '.html'
44 if key in self._template_cache:
45 if self._template_cache[key].HasExpired():
46 self._template_cache.pop(key)
47 if key not in self._template_cache:
48 logging.info('Template cache miss for: ' + path)
49 compiled_template = None
50 for base_path in self._base_paths:
51 try:
52 template = self._fetcher.FetchResource(base_path + path).content
53 compiled_template = Handlebar(template)
54 self._template_cache[key] = self._CachedTemplate(
55 compiled_template,
56 self._cache_timeout_seconds)
57 break
58 except:
59 pass
60
61 return compiled_template
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698