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 posixpath |
7 import traceback | 7 import traceback |
8 | 8 |
9 from data_source import DataSource | 9 from data_source import DataSource |
10 from docs_server_utils import FormatKey | 10 from docs_server_utils import FormatKey |
11 from extensions_paths import ( | 11 from extensions_paths import ( |
12 ARTICLES_TEMPLATES, INTROS_TEMPLATES, PRIVATE_TEMPLATES) | 12 ARTICLES_TEMPLATES, INTROS_TEMPLATES, PRIVATE_TEMPLATES) |
13 from file_system import FileNotFoundError | 13 from file_system import FileNotFoundError |
14 from future import All | 14 from future import All |
15 from path_util import AssertIsDirectory | 15 from path_util import AssertIsDirectory, Join |
16 | 16 |
17 | 17 |
18 class TemplateDataSource(DataSource): | 18 class TemplateDataSource(DataSource): |
19 '''Provides a DataSource interface for compiled templates. | 19 '''Provides a DataSource interface for compiled templates. |
20 ''' | 20 ''' |
21 def __init__(self, server_instance, request=None): | 21 def __init__(self, server_instance, request=None): |
22 self._dir = type(self)._BASE | 22 self._dir = type(self)._BASE |
23 AssertIsDirectory(self._dir) | 23 AssertIsDirectory(self._dir) |
24 self._request = request | 24 self._request = request |
25 self._template_cache = server_instance.compiled_fs_factory.ForTemplates( | 25 self._template_cache = server_instance.compiled_fs_factory.ForTemplates( |
26 server_instance.host_file_system_provider.GetTrunk()) | 26 server_instance.host_file_system_provider.GetTrunk()) |
27 self._file_system = server_instance.host_file_system_provider.GetTrunk() | 27 self._file_system = server_instance.host_file_system_provider.GetTrunk() |
28 | 28 |
29 def get(self, path): | 29 def get(self, path): |
30 try: | 30 try: |
31 return self._template_cache.GetFromFile('%s%s' % | 31 return self._template_cache.GetFromFile('%s%s' % |
32 (self._dir, FormatKey(path))).Get() | 32 (self._dir, FormatKey(path))).Get() |
33 except FileNotFoundError: | 33 except FileNotFoundError: |
34 logging.warning(traceback.format_exc()) | 34 logging.warning(traceback.format_exc()) |
35 return None | 35 return None |
36 | 36 |
37 def Cron(self): | 37 def Cron(self): |
38 futures = [] | 38 def map_cron_paths(op): |
39 for root, _, files in self._file_system.Walk(self._dir): | 39 results = [] |
40 futures += [self._template_cache.GetFromFile( | 40 for root, _, files in self._file_system.Walk(self._dir): |
41 posixpath.join(self._dir, root, FormatKey(f))) | 41 results += [op(Join(self._dir, root, FormatKey(f))) |
42 for f in files | 42 for f in files |
43 if posixpath.splitext(f)[1] == '.html'] | 43 if posixpath.splitext(f)[1] == '.html'] |
44 return All(futures) | 44 return results |
| 45 |
| 46 # Immediately stat everything so that files are guaranteed to be eventually |
| 47 # up to date. See http://crbug.com/398042 for background. |
| 48 # |
| 49 # XXX: For all: do this asynchronously. Just forget about content. Perhaps |
| 50 # run a second round of Crons that we allow to fail. |
| 51 All(map_cron_paths(self._file_system.StatAsync)).Get() |
| 52 |
| 53 # Update content in the future. |
| 54 # XXX don't do this, see comment in APIModels. |
| 55 # XXX: Perhaps there should be a method like CronForPaths, RunCronForPaths, |
| 56 # CachePaths, whatever... on CompiledFileSystem. That way it can implement |
| 57 # this behaviour and I don't have to keep commenting what's going on. |
| 58 return All(map_cron_paths(self._template_cache.GetFromFile)) |
45 | 59 |
46 | 60 |
47 class ArticleDataSource(TemplateDataSource): | 61 class ArticleDataSource(TemplateDataSource): |
48 '''Serves templates for Articles. | 62 '''Serves templates for Articles. |
49 ''' | 63 ''' |
50 _BASE = ARTICLES_TEMPLATES | 64 _BASE = ARTICLES_TEMPLATES |
51 | 65 |
52 | 66 |
53 class IntroDataSource(TemplateDataSource): | 67 class IntroDataSource(TemplateDataSource): |
54 '''Serves templates for Intros. | 68 '''Serves templates for Intros. |
55 ''' | 69 ''' |
56 _BASE = INTROS_TEMPLATES | 70 _BASE = INTROS_TEMPLATES |
57 | 71 |
58 | 72 |
59 class PartialDataSource(TemplateDataSource): | 73 class PartialDataSource(TemplateDataSource): |
60 '''Serves templates for private templates. | 74 '''Serves templates for private templates. |
61 ''' | 75 ''' |
62 _BASE = PRIVATE_TEMPLATES | 76 _BASE = PRIVATE_TEMPLATES |
OLD | NEW |