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

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

Issue 10546078: Extension docs server: APIDataSource (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Minor changes 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
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import json
6 import logging
7 import os
8 import time
9
10 from third_party.handlebar import Handlebar 5 from third_party.handlebar import Handlebar
11 6
12 class TemplateDataSource(object): 7 class TemplateDataSource(object):
13 def __init__(self, fetcher, base_paths, cache_timeout_seconds): 8 """This class fetches and compiles templates using the fetcher passed in with
14 logging.info('Template data source created: %s %d' % 9 |cache_builder|.
15 (' '.join(base_paths), cache_timeout_seconds)) 10 """
16 self._fetcher = fetcher 11 def __init__(self, cache_builder, base_paths):
17 self._template_cache = {} 12 self._cache = cache_builder.build(self._LoadTemplate)
18 self._base_paths = base_paths 13 self._base_paths = base_paths
19 self._cache_timeout_seconds = cache_timeout_seconds 14
15 def _LoadTemplate(self, template):
16 return Handlebar(template)
20 17
21 def Render(self, template_name, context): 18 def Render(self, template_name, context):
22 """This method will render a template named |template_name|, fetching all 19 """This method will render a template named |template_name|, fetching all
23 the partial templates needed with |self._fetcher|. Partials are retrieved 20 the partial templates needed from |self._cache|. Partials are retrieved
24 from the TemplateDataSource with the |get| method. 21 from the TemplateDataSource with the |get| method.
25 """ 22 """
26 template = self.get(template_name) 23 template = self.get(template_name)
27 if not template: 24 if not template:
28 return '' 25 return ''
29 # TODO error handling 26 # TODO error handling
30 return template.render(json.loads(context), {'templates': self}).text 27 return template.render(context, {'templates': self}).text
31
32 class _CachedTemplate(object):
33 def __init__(self, template, expiry):
34 self.template = template
35 self._expiry = expiry
36
37 def HasExpired(self):
38 return time.time() > self._expiry
39 28
40 def __getitem__(self, key): 29 def __getitem__(self, key):
41 return self.get(key) 30 return self.get(key)
42 31
43 def get(self, key): 32 def get(self, key):
44 index = key.rfind('.html') 33 index = key.rfind('.html')
45 if index > 0: 34 if index > 0:
46 key = key[:index] 35 key = key[:index]
47 path = key + '.html' 36 real_path = key + '.html'
48 if key in self._template_cache: 37 for base_path in self._base_paths:
49 if self._template_cache[key].HasExpired(): 38 try:
50 self._template_cache.pop(key) 39 return self._cache.get(base_path + '/' + real_path)
51 if key not in self._template_cache: 40 except:
52 logging.info('Template cache miss for: ' + path) 41 pass
53 compiled_template = None 42 return None
54 for base_path in self._base_paths:
55 try:
56 template = self._fetcher.FetchResource(base_path + path).content
57 compiled_template = Handlebar(template)
58 self._template_cache[key] = self._CachedTemplate(
59 compiled_template,
60 time.time() + self._cache_timeout_seconds)
61 break
62 except:
63 pass
64
65 return compiled_template
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698