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

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

Issue 375133002: Docserver: Display API features that are available to content scripts (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nits Created 6 years, 5 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/api_data_source_test.py » ('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 from copy import copy 5 from copy import copy
6 import logging 6 import logging
7 import os 7 import os
8 import posixpath 8 import posixpath
9 9
10 from data_source import DataSource 10 from data_source import DataSource
11 from docs_server_utils import StringIdentity 11 from docs_server_utils import StringIdentity, MarkFirstAndLast
12 from environment import IsPreviewServer, IsReleaseServer 12 from environment import IsPreviewServer, IsReleaseServer
13 from extensions_paths import JSON_TEMPLATES, PRIVATE_TEMPLATES 13 from extensions_paths import JSON_TEMPLATES, PRIVATE_TEMPLATES
14 from file_system import FileNotFoundError 14 from file_system import FileNotFoundError
15 from future import Future, Collect 15 from future import Future, Collect
16 from operator import itemgetter
16 from platform_util import GetPlatforms 17 from platform_util import GetPlatforms
17 import third_party.json_schema_compiler.json_parse as json_parse 18 import third_party.json_schema_compiler.json_parse as json_parse
18 import third_party.json_schema_compiler.model as model 19 import third_party.json_schema_compiler.model as model
19 from third_party.json_schema_compiler.memoize import memoize 20 from third_party.json_schema_compiler.memoize import memoize
20 21
21 22
22 # The set of possible categories a node may belong to. 23 # The set of possible categories a node may belong to.
23 _NODE_CATEGORIES = ('types', 'functions', 'events', 'properties') 24 _NODE_CATEGORIES = ('types', 'functions', 'events', 'properties')
24 25
25 26
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 def __repr__(self): 272 def __repr__(self):
272 return '%s > %s' % (self._namespace_name, ' > '.join(self._lookup_path)) 273 return '%s > %s' % (self._namespace_name, ' > '.join(self._lookup_path))
273 274
274 275
275 class _JSCModel(object): 276 class _JSCModel(object):
276 '''Uses a Model from the JSON Schema Compiler and generates a dict that 277 '''Uses a Model from the JSON Schema Compiler and generates a dict that
277 a Handlebar template can use for a data source. 278 a Handlebar template can use for a data source.
278 ''' 279 '''
279 280
280 def __init__(self, 281 def __init__(self,
282 content_script_apis,
281 namespace, 283 namespace,
282 availability_finder, 284 availability_finder,
283 json_cache, 285 json_cache,
284 template_cache, 286 template_cache,
285 features_bundle, 287 features_bundle,
286 event_byname_future): 288 event_byname_future):
289 self._content_script_apis = content_script_apis
287 self._availability = availability_finder.GetAPIAvailability(namespace.name) 290 self._availability = availability_finder.GetAPIAvailability(namespace.name)
288 self._current_node = _APINodeCursor(availability_finder, namespace.name) 291 self._current_node = _APINodeCursor(availability_finder, namespace.name)
289 self._api_availabilities = json_cache.GetFromFile( 292 self._api_availabilities = json_cache.GetFromFile(
290 posixpath.join(JSON_TEMPLATES, 'api_availabilities.json')) 293 posixpath.join(JSON_TEMPLATES, 'api_availabilities.json'))
291 self._intro_tables = json_cache.GetFromFile( 294 self._intro_tables = json_cache.GetFromFile(
292 posixpath.join(JSON_TEMPLATES, 'intro_tables.json')) 295 posixpath.join(JSON_TEMPLATES, 'intro_tables.json'))
293 self._api_features = features_bundle.GetAPIFeatures() 296 self._api_features = features_bundle.GetAPIFeatures()
294 self._template_cache = template_cache 297 self._template_cache = template_cache
295 self._event_byname_future = event_byname_future 298 self._event_byname_future = event_byname_future
296 self._namespace = namespace 299 self._namespace = namespace
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 else: 552 else:
550 dst_dict['simple_type'] = type_.property_type.name 553 dst_dict['simple_type'] = type_.property_type.name
551 554
552 def _GetIntroTableList(self): 555 def _GetIntroTableList(self):
553 '''Create a generic data structure that can be traversed by the templates 556 '''Create a generic data structure that can be traversed by the templates
554 to create an API intro table. 557 to create an API intro table.
555 ''' 558 '''
556 intro_rows = [ 559 intro_rows = [
557 self._GetIntroDescriptionRow(), 560 self._GetIntroDescriptionRow(),
558 self._GetIntroAvailabilityRow() 561 self._GetIntroAvailabilityRow()
559 ] + self._GetIntroDependencyRows() 562 ] + self._GetIntroDependencyRows() + self._GetIntroContentScriptRow()
560 563
561 # Add rows using data from intro_tables.json, overriding any existing rows 564 # Add rows using data from intro_tables.json, overriding any existing rows
562 # if they share the same 'title' attribute. 565 # if they share the same 'title' attribute.
563 row_titles = [row['title'] for row in intro_rows] 566 row_titles = [row['title'] for row in intro_rows]
564 for misc_row in self._GetMiscIntroRows(): 567 for misc_row in self._GetMiscIntroRows():
565 if misc_row['title'] in row_titles: 568 if misc_row['title'] in row_titles:
566 intro_rows[row_titles.index(misc_row['title'])] = misc_row 569 intro_rows[row_titles.index(misc_row['title'])] = misc_row
567 else: 570 else:
568 intro_rows.append(misc_row) 571 intro_rows.append(misc_row)
569 572
570 return intro_rows 573 return intro_rows
571 574
572 def _CreateAvailabilityTemplate(self, status, scheduled, version): 575 def _CreateAvailabilityTemplate(self, status, scheduled, version):
573 '''Returns an object suitable for use in templates to display availability 576 '''Returns an object suitable for use in templates to display availability
574 information. 577 information.
575 ''' 578 '''
576 return { 579 return {
577 'partial': self._template_cache.GetFromFile( 580 'partial': self._template_cache.GetFromFile(
578 '%sintro_tables/%s_message.html' % (PRIVATE_TEMPLATES, status)).Get(), 581 '%sintro_tables/%s_message.html' % (PRIVATE_TEMPLATES, status)).Get(),
579 'scheduled': scheduled, 582 'scheduled': scheduled,
580 'version': version 583 'version': version
581 } 584 }
582 585
586 def _GetIntroContentScriptRow(self):
587 content_script_support = self._content_script_apis.get(self._namespace.name)
588 if content_script_support is None:
589 return []
590 if content_script_support.restrictedTo:
591 content_script_support.restrictedTo.sort(key=itemgetter('node'))
592 MarkFirstAndLast(content_script_support.restrictedTo)
593 return [{
594 'title': 'Content Scripts',
595 'content': [{
596 'partial': self._template_cache.GetFromFile(
597 posixpath.join(PRIVATE_TEMPLATES,
598 'intro_tables',
599 'content_scripts.html')).Get(),
600 'contentScriptSupport': content_script_support.__dict__
601 }]
602 }]
583 def _GetAvailabilityTemplate(self): 603 def _GetAvailabilityTemplate(self):
584 '''Gets availability for the current node and returns an appropriate 604 '''Gets availability for the current node and returns an appropriate
585 template object. 605 template object.
586 ''' 606 '''
587 # Displaying deprecated status takes precedence over when the API 607 # Displaying deprecated status takes precedence over when the API
588 # became stable. 608 # became stable.
589 availability_info = self._current_node.GetDeprecated() 609 availability_info = self._current_node.GetDeprecated()
590 if availability_info is not None: 610 if availability_info is not None:
591 status = 'deprecated' 611 status = 'deprecated'
592 else: 612 else:
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
749 from Event in events.json. 769 from Event in events.json.
750 ''' 770 '''
751 if platform not in self._event_byname_futures: 771 if platform not in self._event_byname_futures:
752 future = self._GetSchemaModel(platform, 'events') 772 future = self._GetSchemaModel(platform, 'events')
753 self._event_byname_futures[platform] = Future( 773 self._event_byname_futures[platform] = Future(
754 callback=lambda: _GetEventByNameFromEvents(future.Get())) 774 callback=lambda: _GetEventByNameFromEvents(future.Get()))
755 return self._event_byname_futures[platform] 775 return self._event_byname_futures[platform]
756 776
757 def _GetSchemaModel(self, platform, api_name): 777 def _GetSchemaModel(self, platform, api_name):
758 object_store_key = '/'.join((platform, api_name)) 778 object_store_key = '/'.join((platform, api_name))
779 api_models = self._platform_bundle.GetAPIModels(platform)
759 jsc_model_future = self._model_cache.Get(object_store_key) 780 jsc_model_future = self._model_cache.Get(object_store_key)
760 model_future = self._platform_bundle.GetAPIModels(platform).GetModel( 781 model_future = api_models.GetModel(api_name)
761 api_name) 782 content_script_apis_future = api_models.GetContentScriptAPIs()
762 def resolve(): 783 def resolve():
763 jsc_model = jsc_model_future.Get() 784 jsc_model = jsc_model_future.Get()
764 if jsc_model is None: 785 if jsc_model is None:
765 jsc_model = _JSCModel( 786 jsc_model = _JSCModel(
787 content_script_apis_future.Get(),
766 model_future.Get(), 788 model_future.Get(),
767 self._platform_bundle.GetAvailabilityFinder(platform), 789 self._platform_bundle.GetAvailabilityFinder(platform),
768 self._json_cache, 790 self._json_cache,
769 self._template_cache, 791 self._template_cache,
770 self._platform_bundle.GetFeaturesBundle(platform), 792 self._platform_bundle.GetFeaturesBundle(platform),
771 self._LoadEventByName(platform)).ToDict() 793 self._LoadEventByName(platform)).ToDict()
772 self._model_cache.Set(object_store_key, jsc_model) 794 self._model_cache.Set(object_store_key, jsc_model)
773 return jsc_model 795 return jsc_model
774 return Future(callback=resolve) 796 return Future(callback=resolve)
775 797
(...skipping 17 matching lines...) Expand all
793 getter = lambda: 0 815 getter = lambda: 0
794 getter.get = lambda api_name: self._GetImpl(platform, api_name).Get() 816 getter.get = lambda api_name: self._GetImpl(platform, api_name).Get()
795 return getter 817 return getter
796 818
797 def Cron(self): 819 def Cron(self):
798 futures = [] 820 futures = []
799 for platform in GetPlatforms(): 821 for platform in GetPlatforms():
800 futures += [self._GetImpl(platform, name) 822 futures += [self._GetImpl(platform, name)
801 for name in self._platform_bundle.GetAPIModels(platform).GetNames()] 823 for name in self._platform_bundle.GetAPIModels(platform).GetNames()]
802 return Collect(futures, except_pass=FileNotFoundError) 824 return Collect(futures, except_pass=FileNotFoundError)
OLDNEW
« no previous file with comments | « no previous file | chrome/common/extensions/docs/server2/api_data_source_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698