Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: chrome/common/extensions/docs/server2/api_data_source.py

Issue 656673003: Docserver: Cache the samples. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/common/extensions/docs/server2/app.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 data_source import DataSource 7 from data_source import DataSource
8 from docs_server_utils import StringIdentity 8 from docs_server_utils import StringIdentity
9 from environment import IsPreviewServer 9 from environment import IsPreviewServer
10 from file_system import FileNotFoundError 10 from file_system import FileNotFoundError
11 from future import Future, All 11 from future import Future, All
12 from jsc_view import JSCView, GetEventByNameFromEvents 12 from jsc_view import CreateJSCView, GetEventByNameFromEvents
13 from platform_util import GetPlatforms 13 from platform_util import GetPlatforms
14 from samples_data_source import CreateSamplesView
15 14
16 15
17 class APIDataSource(DataSource): 16 class APIDataSource(DataSource):
18 '''This class fetches and loads JSON APIs from the FileSystem passed in with 17 '''This class fetches and loads JSON APIs from the FileSystem passed in with
19 |compiled_fs_factory|, so the APIs can be plugged into templates. 18 |compiled_fs_factory|, so the APIs can be plugged into templates.
20 ''' 19 '''
21 def __init__(self, server_instance, request): 20 def __init__(self, server_instance, request):
22 file_system = server_instance.host_file_system_provider.GetMaster() 21 file_system = server_instance.host_file_system_provider.GetMaster()
23 self._json_cache = server_instance.compiled_fs_factory.ForJson(file_system) 22 self._json_cache = server_instance.compiled_fs_factory.ForJson(file_system)
24 self._template_cache = server_instance.compiled_fs_factory.ForTemplates( 23 self._template_cache = server_instance.compiled_fs_factory.ForTemplates(
(...skipping 19 matching lines...) Expand all
44 self._event_byname_futures[platform] = Future( 43 self._event_byname_futures[platform] = Future(
45 callback=lambda: GetEventByNameFromEvents(future.Get())) 44 callback=lambda: GetEventByNameFromEvents(future.Get()))
46 return self._event_byname_futures[platform] 45 return self._event_byname_futures[platform]
47 46
48 def _GetSchemaView(self, platform, api_name): 47 def _GetSchemaView(self, platform, api_name):
49 object_store_key = '/'.join((platform, api_name)) 48 object_store_key = '/'.join((platform, api_name))
50 api_models = self._platform_bundle.GetAPIModels(platform) 49 api_models = self._platform_bundle.GetAPIModels(platform)
51 jsc_view_future = self._view_cache.Get(object_store_key) 50 jsc_view_future = self._view_cache.Get(object_store_key)
52 model_future = api_models.GetModel(api_name) 51 model_future = api_models.GetModel(api_name)
53 content_script_apis_future = api_models.GetContentScriptAPIs() 52 content_script_apis_future = api_models.GetContentScriptAPIs()
53
54 # Parsing samples on the preview server takes forever, so disable it.
55 if IsPreviewServer():
56 samples_futures = Future(value=[])
57 else:
58 samples_future = (self._platform_bundle.GetSamplesModel(platform)
59 .FilterSamples(api_name))
60
54 def resolve(): 61 def resolve():
55 jsc_view = jsc_view_future.Get() 62 jsc_view = jsc_view_future.Get()
56 if jsc_view is None: 63 if jsc_view is None:
57 jsc_view = JSCView( 64 jsc_view = CreateJSCView(
58 content_script_apis_future.Get(), 65 content_script_apis_future.Get(),
59 model_future.Get(), 66 model_future.Get(),
60 self._platform_bundle.GetAvailabilityFinder(platform), 67 self._platform_bundle.GetAvailabilityFinder(platform),
61 self._json_cache, 68 self._json_cache,
62 self._template_cache, 69 self._template_cache,
63 self._platform_bundle.GetFeaturesBundle(platform), 70 self._platform_bundle.GetFeaturesBundle(platform),
64 self._LoadEventByName(platform), 71 self._LoadEventByName(platform),
65 platform).ToDict() 72 platform,
73 samples_future.Get(),
74 self._request)
66 self._view_cache.Set(object_store_key, jsc_view) 75 self._view_cache.Set(object_store_key, jsc_view)
67 return jsc_view 76 return jsc_view
68 return Future(callback=resolve) 77 return Future(callback=resolve)
69 78
70 def _GetImpl(self, platform, api_name):
71 jsc_view_future = self._GetSchemaView(platform, api_name)
72 def resolve():
73 jsc_view = jsc_view_future.Get()
74 # Parsing samples on the preview server takes seconds and doesn't add
75 # anything. Don't do it.
76 if not IsPreviewServer():
77 samples_model = self._platform_bundle.GetSamplesModel(platform)
78 # Creates an object that lazily gets samples.
79 jsc_view['samples'] = type('getter', (object,), {
80 'get': lambda _, platform: CreateSamplesView(
81 samples_model.FilterSamples(jsc_view['name']), self._request)
82 })()
83 return jsc_view
84 return Future(callback=resolve)
85
86 def get(self, platform): 79 def get(self, platform):
87 '''Return a getter object so that templates can perform lookups such 80 '''Return a getter object so that templates can perform lookups such
88 as apis.extensions.runtime. 81 as apis.extensions.runtime.
89 ''' 82 '''
90 getter = lambda: 0 83 getter = lambda: 0
91 getter.get = lambda api_name: self._GetImpl(platform, api_name).Get() 84 getter.get = lambda api_name: self._GetSchemaView(platform, api_name).Get()
92 return getter 85 return getter
93 86
94 def GetRefreshPaths(self): 87 def GetRefreshPaths(self):
95 tasks = [] 88 tasks = []
96 for platform in GetPlatforms(): 89 for platform in GetPlatforms():
97 tasks += ['%s/%s' % (platform, api) 90 tasks += ['%s/%s' % (platform, api)
98 for api in 91 for api in
99 self._platform_bundle.GetAPIModels(platform).GetNames()] 92 self._platform_bundle.GetAPIModels(platform).GetNames()]
100 return tasks 93 return tasks
101 94
102 def Refresh(self, path): 95 def Refresh(self, path):
103 platform, api = path.split('/') 96 platform, api = path.split('/')
104 logging.info('Refreshing %s/%s' % (platform, api)) 97 logging.info('Refreshing %s/%s' % (platform, api))
105 future = self._GetImpl(platform, api) 98 future = self._GetSchemaView(platform, api)
106 return All([future], except_pass=FileNotFoundError) 99 return All([future], except_pass=FileNotFoundError)
OLDNEW
« no previous file with comments | « no previous file | chrome/common/extensions/docs/server2/app.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698