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