OLD | NEW |
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 logging | 5 import logging |
| 6 import posixpath |
6 import traceback | 7 import traceback |
7 | 8 |
8 from data_source import DataSource | 9 from data_source import DataSource |
9 from extensions_paths import PRIVATE_TEMPLATES | 10 from extensions_paths import PRIVATE_TEMPLATES |
10 from file_system import FileNotFoundError | 11 from file_system import FileNotFoundError |
11 from future import Future | 12 from future import Collect |
12 | 13 |
13 | 14 |
14 class TemplateDataSource(DataSource): | 15 class TemplateDataSource(DataSource): |
15 '''Provides a DataSource interface for compiled templates. | 16 '''Provides a DataSource interface for compiled templates. |
16 ''' | 17 ''' |
17 | 18 |
18 def __init__(self, server_instance, _, partial_dir=PRIVATE_TEMPLATES): | 19 def __init__(self, server_instance, _, partial_dir=PRIVATE_TEMPLATES): |
19 self._template_cache = server_instance.compiled_fs_factory.ForTemplates( | 20 self._template_cache = server_instance.compiled_fs_factory.ForTemplates( |
20 server_instance.host_file_system_provider.GetTrunk()) | 21 server_instance.host_file_system_provider.GetTrunk()) |
21 self._partial_dir = partial_dir | 22 self._partial_dir = partial_dir |
| 23 self._file_system = server_instance.host_file_system_provider.GetTrunk() |
22 | 24 |
23 def get(self, path): | 25 def get(self, path): |
24 try: | 26 try: |
25 return self._template_cache.GetFromFile('%s/%s.html' % | 27 return self._template_cache.GetFromFile('%s/%s.html' % |
26 (self._partial_dir, path)).Get() | 28 (self._partial_dir, path)).Get() |
27 except FileNotFoundError: | 29 except FileNotFoundError: |
28 logging.warning(traceback.format_exc()) | 30 logging.warning(traceback.format_exc()) |
29 return None | 31 return None |
30 | 32 |
31 def Cron(self): | 33 def Cron(self): |
32 # TODO(kalman): Implement this; probably by finding all files that can be | 34 futures = [] |
33 # compiled to templates underneath |self._partial_dir| and compiling them. | 35 for root, _, files in self._file_system.Walk(self._partial_dir): |
34 return Future(value=()) | 36 futures += [self._template_cache.GetFromFile( |
| 37 posixpath.join(self._partial_dir, root, f)) |
| 38 for f in files |
| 39 if posixpath.splitext(f)[1] == '.html'] |
| 40 return Collect(futures) |
OLD | NEW |