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 | 6 |
7 from docs_server_utils import FormatKey | 7 from docs_server_utils import FormatKey |
8 from file_system import FileNotFoundError | 8 from file_system import FileNotFoundError |
9 from third_party.handlebar import Handlebar | 9 from third_party.handlebar import Handlebar |
10 | 10 |
11 EXTENSIONS_URL = '/chrome/extensions' | 11 EXTENSIONS_URL = '/chrome/extensions' |
12 | 12 |
13 def _MakeBranchDict(branch): | 13 def _MakeBranchDict(branch): |
14 return { | 14 return { |
15 'showWarning': branch != 'stable', | 15 'showWarning': branch != 'stable', |
16 'branches': [ | 16 'branches': [ |
17 { 'name': 'Stable', 'path': 'stable' }, | 17 { 'name': 'Stable', 'path': 'stable' }, |
18 { 'name': 'Dev', 'path': 'dev' }, | 18 { 'name': 'Dev', 'path': 'dev' }, |
19 { 'name': 'Beta', 'path': 'beta' }, | 19 { 'name': 'Beta', 'path': 'beta' }, |
20 { 'name': 'Trunk', 'path': 'trunk' } | 20 { 'name': 'Trunk', 'path': 'trunk' } |
21 ], | 21 ], |
22 'current': branch | 22 'current': None |
23 } | 23 } |
24 | 24 |
25 class TemplateDataSource(object): | 25 class TemplateDataSource(object): |
26 """Renders Handlebar templates, providing them with the context in which to | 26 """Renders Handlebar templates, providing them with the context in which to |
27 render. | 27 render. |
28 | 28 |
29 Also acts as a data source itself, providing partial Handlebar templates to | 29 Also acts as a data source itself, providing partial Handlebar templates to |
30 those it renders. | 30 those it renders. |
31 | 31 |
32 Each instance of TemplateDataSource is bound to a Request so that it can | 32 Each instance of TemplateDataSource is bound to a Request so that it can |
33 render templates with request-specific data (such as Accept-Language); use | 33 render templates with request-specific data (such as Accept-Language); use |
34 a Factory to cheaply construct these. | 34 a Factory to cheaply construct these. |
35 """ | 35 """ |
36 | 36 |
37 class Factory(object): | 37 class Factory(object): |
38 """A factory to create lightweight TemplateDataSource instances bound to | 38 """A factory to create lightweight TemplateDataSource instances bound to |
39 individual Requests. | 39 individual Requests. |
40 """ | 40 """ |
41 def __init__(self, | 41 def __init__(self, |
42 branch, | 42 branch, |
43 api_data_source_factory, | 43 api_data_source_factory, |
44 api_list_data_source, | 44 api_list_data_source, |
45 intro_data_source, | 45 intro_data_source, |
46 samples_data_source_factory, | 46 samples_data_source_factory, |
47 cache_builder, | 47 cache_builder, |
48 public_template_path, | 48 public_template_path, |
49 private_template_path): | 49 private_template_path): |
50 self._branch_info = _MakeBranchDict(branch) | 50 self._branch_info = _MakeBranchDict(branch) |
51 self._static_resources = ((('/' + branch) if branch != 'local' else '') + | |
52 '/static') | |
53 self._api_data_source_factory = api_data_source_factory | 51 self._api_data_source_factory = api_data_source_factory |
54 self._api_list_data_source = api_list_data_source | 52 self._api_list_data_source = api_list_data_source |
55 self._intro_data_source = intro_data_source | 53 self._intro_data_source = intro_data_source |
56 self._samples_data_source_factory = samples_data_source_factory | 54 self._samples_data_source_factory = samples_data_source_factory |
57 self._cache = cache_builder.build(Handlebar) | 55 self._cache = cache_builder.build(Handlebar) |
58 self._public_template_path = public_template_path | 56 self._public_template_path = public_template_path |
59 self._private_template_path = private_template_path | 57 self._private_template_path = private_template_path |
60 | 58 |
61 def Create(self, request): | 59 def Create(self, request, channel_name): |
not at google - send to devlin
2012/08/13 00:53:00
See comment in handler.py. This could go in the fa
cduvall
2012/08/13 21:45:04
Done.
| |
62 """Returns a new TemplateDataSource bound to |request|. | 60 """Returns a new TemplateDataSource bound to |request|. |
63 """ | 61 """ |
64 return TemplateDataSource( | 62 return TemplateDataSource( |
65 self._branch_info, | 63 self._branch_info, |
66 self._static_resources, | |
67 self._api_data_source_factory.Create(request), | 64 self._api_data_source_factory.Create(request), |
68 self._api_list_data_source, | 65 self._api_list_data_source, |
69 self._intro_data_source, | 66 self._intro_data_source, |
70 self._samples_data_source_factory.Create(request), | 67 self._samples_data_source_factory.Create(request), |
71 self._cache, | 68 self._cache, |
72 self._public_template_path, | 69 self._public_template_path, |
73 self._private_template_path, | 70 self._private_template_path, |
74 request) | 71 request, |
72 channel_name) | |
75 | 73 |
76 def __init__(self, | 74 def __init__(self, |
77 branch_info, | 75 branch_info, |
78 static_resources, | |
79 api_data_source, | 76 api_data_source, |
80 api_list_data_source, | 77 api_list_data_source, |
81 intro_data_source, | 78 intro_data_source, |
82 samples_data_source, | 79 samples_data_source, |
83 cache, | 80 cache, |
84 public_template_path, | 81 public_template_path, |
85 private_template_path, | 82 private_template_path, |
86 request): | 83 request, |
84 channel_name): | |
87 self._branch_info = branch_info | 85 self._branch_info = branch_info |
88 self._static_resources = static_resources | |
89 self._api_list_data_source = api_list_data_source | 86 self._api_list_data_source = api_list_data_source |
90 self._intro_data_source = intro_data_source | 87 self._intro_data_source = intro_data_source |
91 self._samples_data_source = samples_data_source | 88 self._samples_data_source = samples_data_source |
92 self._api_data_source = api_data_source | 89 self._api_data_source = api_data_source |
93 self._cache = cache | 90 self._cache = cache |
94 self._public_template_path = public_template_path | 91 self._public_template_path = public_template_path |
95 self._private_template_path = private_template_path | 92 self._private_template_path = private_template_path |
96 self._request = request | 93 self._request = request |
94 self._channel_name = channel_name | |
95 self._branch_info['current'] = channel_name | |
not at google - send to devlin
2012/08/13 00:53:00
If you do that, then you could make the current ch
cduvall
2012/08/13 21:45:04
Done.
| |
96 self._static_resources = ( | |
97 (('/' + channel_name) if channel_name != 'local' else '') + '/static') | |
not at google - send to devlin
2012/08/13 00:53:00
Will this cause problems when we need to configure
not at google - send to devlin
2012/08/13 05:47:12
I also note that SamplesDataSource does the same t
cduvall
2012/08/13 21:45:04
This won't cause any problems. Even if the static
| |
97 | 98 |
98 def Render(self, template_name): | 99 def Render(self, template_name): |
99 """This method will render a template named |template_name|, fetching all | 100 """This method will render a template named |template_name|, fetching all |
100 the partial templates needed from |self._cache|. Partials are retrieved | 101 the partial templates needed from |self._cache|. Partials are retrieved |
101 from the TemplateDataSource with the |get| method. | 102 from the TemplateDataSource with the |get| method. |
102 """ | 103 """ |
103 template = self.GetTemplate(self._public_template_path, template_name) | 104 template = self.GetTemplate(self._public_template_path, template_name) |
104 if not template: | 105 if not template: |
105 return '' | 106 return '' |
106 # TODO error handling | 107 # TODO error handling |
(...skipping 15 matching lines...) Expand all Loading... | |
122 def get(self, key): | 123 def get(self, key): |
123 return self.GetTemplate(self._private_template_path, key) | 124 return self.GetTemplate(self._private_template_path, key) |
124 | 125 |
125 def GetTemplate(self, base_path, template_name): | 126 def GetTemplate(self, base_path, template_name): |
126 real_path = FormatKey(template_name) | 127 real_path = FormatKey(template_name) |
127 try: | 128 try: |
128 return self._cache.GetFromFile(base_path + '/' + real_path) | 129 return self._cache.GetFromFile(base_path + '/' + real_path) |
129 except FileNotFoundError as e: | 130 except FileNotFoundError as e: |
130 logging.error(e) | 131 logging.error(e) |
131 return None | 132 return None |
OLD | NEW |