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

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: 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 5 import logging
7 import os 6 import os
8 import time 7 import time
9 8
10 from third_party.handlebar import Handlebar 9 from third_party.handlebar import Handlebar
11 10
12 class TemplateDataSource(object): 11 class TemplateDataSource(object):
13 def __init__(self, fetcher, base_paths, cache_timeout_seconds): 12 def __init__(self, fetcher, base_paths, cache_timeout_seconds):
14 logging.info('Template data source created: %s %d' % 13 logging.info('Template data source created: %s %d' %
15 (' '.join(base_paths), cache_timeout_seconds)) 14 (' '.join(base_paths), cache_timeout_seconds))
16 self._fetcher = fetcher 15 self._fetcher = fetcher
17 self._template_cache = {} 16 self._template_cache = {}
18 self._base_paths = base_paths 17 self._base_paths = base_paths
19 self._cache_timeout_seconds = cache_timeout_seconds 18 self._cache_timeout_seconds = cache_timeout_seconds
20 19
21 def Render(self, template_name, context): 20 def Render(self, template_name, context):
22 """This method will render a template named |template_name|, fetching all 21 """This method will render a template named |template_name|, fetching all
23 the partial templates needed with |self._fetcher|. Partials are retrieved 22 the partial templates needed with |self._fetcher|. Partials are retrieved
24 from the TemplateDataSource with the |get| method. 23 from the TemplateDataSource with the |get| method.
25 """ 24 """
26 template = self.get(template_name) 25 template = self.get(template_name)
27 if not template: 26 if not template:
28 return '' 27 return ''
29 # TODO error handling 28 # TODO error handling
30 return template.render(json.loads(context), {'templates': self}).text 29 return template.render(context, {'templates': self}).text
31 30
32 class _CachedTemplate(object): 31 class _CachedTemplate(object):
33 def __init__(self, template, expiry): 32 def __init__(self, template, expiry):
34 self.template = template 33 self.template = template
35 self._expiry = expiry 34 self._expiry = expiry
36 35
37 def HasExpired(self): 36 def HasExpired(self):
38 return time.time() > self._expiry 37 return time.time() > self._expiry
39 38
40 def __getitem__(self, key): 39 def __getitem__(self, key):
(...skipping 15 matching lines...) Expand all
56 template = self._fetcher.FetchResource(base_path + path).content 55 template = self._fetcher.FetchResource(base_path + path).content
57 compiled_template = Handlebar(template) 56 compiled_template = Handlebar(template)
58 self._template_cache[key] = self._CachedTemplate( 57 self._template_cache[key] = self._CachedTemplate(
59 compiled_template, 58 compiled_template,
60 time.time() + self._cache_timeout_seconds) 59 time.time() + self._cache_timeout_seconds)
61 break 60 break
62 except: 61 except:
63 pass 62 pass
64 63
65 return compiled_template 64 return compiled_template
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698