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 from data_source import DataSource | 5 from data_source import DataSource |
6 from docs_server_utils import StringIdentity | 6 from docs_server_utils import StringIdentity |
7 from environment import IsPreviewServer | 7 from environment import IsPreviewServer |
8 from file_system import FileNotFoundError | 8 from file_system import FileNotFoundError |
9 from future import Future, All | 9 from future import Future, All |
10 from jsc_view import JSCView, GetEventByNameFromEvents | 10 from jsc_view import JSCView, GetEventByNameFromEvents |
11 from platform_util import GetPlatforms | 11 from platform_util import GetPlatforms |
12 | 12 from samples_data_source import CreateSamplesView |
13 | |
14 class _LazySamplesGetter(object): | |
15 '''This class is needed so that an extensions API page does not have to fetch | |
16 the apps samples page and vice versa. | |
17 ''' | |
18 | |
19 def __init__(self, api_name, samples): | |
20 self._api_name = api_name | |
21 self._samples = samples | |
22 | |
23 def get(self, key): | |
24 return self._samples.FilterSamples(key, self._api_name) | |
25 | 13 |
26 | 14 |
27 class APIDataSource(DataSource): | 15 class APIDataSource(DataSource): |
28 '''This class fetches and loads JSON APIs from the FileSystem passed in with | 16 '''This class fetches and loads JSON APIs from the FileSystem passed in with |
29 |compiled_fs_factory|, so the APIs can be plugged into templates. | 17 |compiled_fs_factory|, so the APIs can be plugged into templates. |
30 ''' | 18 ''' |
31 def __init__(self, server_instance, request): | 19 def __init__(self, server_instance, request): |
32 file_system = server_instance.host_file_system_provider.GetTrunk() | 20 file_system = server_instance.host_file_system_provider.GetTrunk() |
33 self._json_cache = server_instance.compiled_fs_factory.ForJson(file_system) | 21 self._json_cache = server_instance.compiled_fs_factory.ForJson(file_system) |
34 self._template_cache = server_instance.compiled_fs_factory.ForTemplates( | 22 self._template_cache = server_instance.compiled_fs_factory.ForTemplates( |
35 file_system) | 23 file_system) |
36 self._platform_bundle = server_instance.platform_bundle | 24 self._platform_bundle = server_instance.platform_bundle |
37 self._view_cache = server_instance.object_store_creator.Create( | 25 self._view_cache = server_instance.object_store_creator.Create( |
38 APIDataSource, | 26 APIDataSource, |
39 # Update the models when any of templates, APIs, or Features change. | 27 # Update the models when any of templates, APIs, or Features change. |
40 category=StringIdentity(self._json_cache.GetIdentity(), | 28 category=StringIdentity(self._json_cache.GetIdentity(), |
41 self._template_cache.GetIdentity(), | 29 self._template_cache.GetIdentity(), |
42 self._platform_bundle.GetIdentity())) | 30 self._platform_bundle.GetIdentity())) |
43 | 31 |
44 # This caches the result of _LoadEventByName. | 32 # This caches the result of _LoadEventByName. |
45 self._event_byname_futures = {} | 33 self._event_byname_futures = {} |
46 self._samples = server_instance.samples_data_source_factory.Create(request) | 34 self._request = request |
47 | 35 |
48 def _LoadEventByName(self, platform): | 36 def _LoadEventByName(self, platform): |
49 '''All events have some members in common. We source their description | 37 '''All events have some members in common. We source their description |
50 from Event in events.json. | 38 from Event in events.json. |
51 ''' | 39 ''' |
52 if platform not in self._event_byname_futures: | 40 if platform not in self._event_byname_futures: |
53 future = self._GetSchemaView(platform, 'events') | 41 future = self._GetSchemaView(platform, 'events') |
54 self._event_byname_futures[platform] = Future( | 42 self._event_byname_futures[platform] = Future( |
55 callback=lambda: GetEventByNameFromEvents(future.Get())) | 43 callback=lambda: GetEventByNameFromEvents(future.Get())) |
56 return self._event_byname_futures[platform] | 44 return self._event_byname_futures[platform] |
(...skipping 20 matching lines...) Expand all Loading... | |
77 return jsc_view | 65 return jsc_view |
78 return Future(callback=resolve) | 66 return Future(callback=resolve) |
79 | 67 |
80 def _GetImpl(self, platform, api_name): | 68 def _GetImpl(self, platform, api_name): |
81 jsc_view_future = self._GetSchemaView(platform, api_name) | 69 jsc_view_future = self._GetSchemaView(platform, api_name) |
82 def resolve(): | 70 def resolve(): |
83 jsc_view = jsc_view_future.Get() | 71 jsc_view = jsc_view_future.Get() |
84 # Parsing samples on the preview server takes seconds and doesn't add | 72 # Parsing samples on the preview server takes seconds and doesn't add |
85 # anything. Don't do it. | 73 # anything. Don't do it. |
86 if not IsPreviewServer(): | 74 if not IsPreviewServer(): |
87 jsc_view['samples'] = _LazySamplesGetter( | 75 jsc_view['samples'] = type('getter', (object,), { |
ahernandez
2014/08/05 19:15:54
I hope this isn't too hacky, I thought it would be
not at google - send to devlin
2014/08/05 19:56:35
ok - fair enough, makes sense. That's pretty hacky
| |
88 jsc_view['name'], | 76 'get': lambda _, platform: CreateSamplesView( |
89 self._samples) | 77 self._platform_bundle.GetSamplesModel(platform).FilterSamples( |
78 jsc_view['name']), | |
79 self._request) | |
80 })() | |
90 return jsc_view | 81 return jsc_view |
91 return Future(callback=resolve) | 82 return Future(callback=resolve) |
92 | 83 |
93 def get(self, platform): | 84 def get(self, platform): |
94 '''Return a getter object so that templates can perform lookups such | 85 '''Return a getter object so that templates can perform lookups such |
95 as apis.extensions.runtime. | 86 as apis.extensions.runtime. |
96 ''' | 87 ''' |
97 getter = lambda: 0 | 88 getter = lambda: 0 |
98 getter.get = lambda api_name: self._GetImpl(platform, api_name).Get() | 89 getter.get = lambda api_name: self._GetImpl(platform, api_name).Get() |
99 return getter | 90 return getter |
100 | 91 |
101 def Cron(self): | 92 def Cron(self): |
102 futures = [] | 93 futures = [] |
103 for platform in GetPlatforms(): | 94 for platform in GetPlatforms(): |
104 futures += [self._GetImpl(platform, name) | 95 futures += [self._GetImpl(platform, name) |
105 for name in self._platform_bundle.GetAPIModels(platform).GetNames()] | 96 for name in self._platform_bundle.GetAPIModels(platform).GetNames()] |
106 return All(futures, except_pass=FileNotFoundError) | 97 return All(futures, except_pass=FileNotFoundError) |
OLD | NEW |