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 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, MarkFirst, MarkLast |
| 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 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 def __repr__(self): | 260 def __repr__(self): |
| 260 return '%s > %s' % (self._namespace_name, ' > '.join(self._lookup_path)) | 261 return '%s > %s' % (self._namespace_name, ' > '.join(self._lookup_path)) |
| 261 | 262 |
| 262 | 263 |
| 263 class _JSCModel(object): | 264 class _JSCModel(object): |
| 264 '''Uses a Model from the JSON Schema Compiler and generates a dict that | 265 '''Uses a Model from the JSON Schema Compiler and generates a dict that |
| 265 a Handlebar template can use for a data source. | 266 a Handlebar template can use for a data source. |
| 266 ''' | 267 ''' |
| 267 | 268 |
| 268 def __init__(self, | 269 def __init__(self, |
| 270 content_script_apis, | |
| 269 namespace, | 271 namespace, |
| 270 availability_finder, | 272 availability_finder, |
| 271 json_cache, | 273 json_cache, |
| 272 template_cache, | 274 template_cache, |
| 273 features_bundle, | 275 features_bundle, |
| 274 event_byname_future): | 276 event_byname_future): |
| 277 self._content_script_apis = content_script_apis | |
| 275 self._availability = availability_finder.GetAPIAvailability(namespace.name) | 278 self._availability = availability_finder.GetAPIAvailability(namespace.name) |
| 276 self._current_node = _APINodeCursor(availability_finder, namespace.name) | 279 self._current_node = _APINodeCursor(availability_finder, namespace.name) |
| 277 self._api_availabilities = json_cache.GetFromFile( | 280 self._api_availabilities = json_cache.GetFromFile( |
| 278 posixpath.join(JSON_TEMPLATES, 'api_availabilities.json')) | 281 posixpath.join(JSON_TEMPLATES, 'api_availabilities.json')) |
| 279 self._intro_tables = json_cache.GetFromFile( | 282 self._intro_tables = json_cache.GetFromFile( |
| 280 posixpath.join(JSON_TEMPLATES, 'intro_tables.json')) | 283 posixpath.join(JSON_TEMPLATES, 'intro_tables.json')) |
| 281 self._api_features = features_bundle.GetAPIFeatures() | 284 self._api_features = features_bundle.GetAPIFeatures() |
| 282 self._template_cache = template_cache | 285 self._template_cache = template_cache |
| 283 self._event_byname_future = event_byname_future | 286 self._event_byname_future = event_byname_future |
| 284 self._namespace = namespace | 287 self._namespace = namespace |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 537 else: | 540 else: |
| 538 dst_dict['simple_type'] = type_.property_type.name | 541 dst_dict['simple_type'] = type_.property_type.name |
| 539 | 542 |
| 540 def _GetIntroTableList(self): | 543 def _GetIntroTableList(self): |
| 541 '''Create a generic data structure that can be traversed by the templates | 544 '''Create a generic data structure that can be traversed by the templates |
| 542 to create an API intro table. | 545 to create an API intro table. |
| 543 ''' | 546 ''' |
| 544 intro_rows = [ | 547 intro_rows = [ |
| 545 self._GetIntroDescriptionRow(), | 548 self._GetIntroDescriptionRow(), |
| 546 self._GetIntroAvailabilityRow() | 549 self._GetIntroAvailabilityRow() |
| 547 ] + self._GetIntroDependencyRows() | 550 ] + self._GetIntroDependencyRows() + self._GetIntroContentScriptRow() |
| 548 | 551 |
| 549 # Add rows using data from intro_tables.json, overriding any existing rows | 552 # Add rows using data from intro_tables.json, overriding any existing rows |
| 550 # if they share the same 'title' attribute. | 553 # if they share the same 'title' attribute. |
| 551 row_titles = [row['title'] for row in intro_rows] | 554 row_titles = [row['title'] for row in intro_rows] |
| 552 for misc_row in self._GetMiscIntroRows(): | 555 for misc_row in self._GetMiscIntroRows(): |
| 553 if misc_row['title'] in row_titles: | 556 if misc_row['title'] in row_titles: |
| 554 intro_rows[row_titles.index(misc_row['title'])] = misc_row | 557 intro_rows[row_titles.index(misc_row['title'])] = misc_row |
| 555 else: | 558 else: |
| 556 intro_rows.append(misc_row) | 559 intro_rows.append(misc_row) |
| 557 | 560 |
| 558 return intro_rows | 561 return intro_rows |
| 559 | 562 |
| 560 def _GetAvailabilityTemplate(self, status=None, version=None, scheduled=None): | 563 def _GetAvailabilityTemplate(self, status=None, version=None, scheduled=None): |
| 561 '''Returns an object that the templates use to display availability | 564 '''Returns an object that the templates use to display availability |
| 562 information. | 565 information. |
| 563 ''' | 566 ''' |
| 564 if status is None: | 567 if status is None: |
| 565 availability_info = self._current_node.GetAvailability() | 568 availability_info = self._current_node.GetAvailability() |
| 566 if availability_info is None: | 569 if availability_info is None: |
| 567 return None | 570 return None |
| 568 status = availability_info.channel | 571 status = availability_info.channel |
| 569 version = availability_info.version | 572 version = availability_info.version |
| 570 return { | 573 return { |
| 571 'partial': self._template_cache.GetFromFile( | 574 'partial': self._template_cache.GetFromFile( |
| 572 '%sintro_tables/%s_message.html' % (PRIVATE_TEMPLATES, status)).Get(), | 575 '%sintro_tables/%s_message.html' % (PRIVATE_TEMPLATES, status)).Get(), |
| 573 'scheduled': scheduled, | 576 'scheduled': scheduled, |
| 574 'version': version | 577 'version': version |
| 575 } | 578 } |
| 576 | 579 |
| 580 def _GetIntroContentScriptRow(self): | |
| 581 content_script_support = self._content_script_apis.get(self._namespace.name) | |
| 582 if content_script_support is None: | |
| 583 return [] | |
| 584 if content_script_support.restrictedTo: | |
|
ahernandez
2014/07/16 20:35:34
Should this code be moved into a function somewher
not at google - send to devlin
2014/07/21 17:51:19
maybe, though I can't think of an appropriate plac
| |
| 585 content_script_support.restrictedTo.sort(key=itemgetter('node')) | |
| 586 MarkFirst(content_script_support.restrictedTo) | |
| 587 MarkLast(content_script_support.restrictedTo) | |
| 588 return [{ | |
| 589 'title': 'Content Scripts', | |
| 590 'content': [{ | |
| 591 'partial': self._template_cache.GetFromFile( | |
| 592 posixpath.join(PRIVATE_TEMPLATES, | |
| 593 'intro_tables', | |
| 594 'content_scripts.html')).Get(), | |
| 595 'contentScriptSupport': content_script_support.__dict__ | |
| 596 }] | |
| 597 }] | |
| 598 | |
| 577 def _GetIntroDescriptionRow(self): | 599 def _GetIntroDescriptionRow(self): |
| 578 ''' Generates the 'Description' row data for an API intro table. | 600 ''' Generates the 'Description' row data for an API intro table. |
| 579 ''' | 601 ''' |
| 580 return { | 602 return { |
| 581 'title': 'Description', | 603 'title': 'Description', |
| 582 'content': [ | 604 'content': [ |
| 583 { 'text': self._namespace.description } | 605 { 'text': self._namespace.description } |
| 584 ] | 606 ] |
| 585 } | 607 } |
| 586 | 608 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 726 from Event in events.json. | 748 from Event in events.json. |
| 727 ''' | 749 ''' |
| 728 if platform not in self._event_byname_futures: | 750 if platform not in self._event_byname_futures: |
| 729 future = self._GetSchemaModel(platform, 'events') | 751 future = self._GetSchemaModel(platform, 'events') |
| 730 self._event_byname_futures[platform] = Future( | 752 self._event_byname_futures[platform] = Future( |
| 731 callback=lambda: _GetEventByNameFromEvents(future.Get())) | 753 callback=lambda: _GetEventByNameFromEvents(future.Get())) |
| 732 return self._event_byname_futures[platform] | 754 return self._event_byname_futures[platform] |
| 733 | 755 |
| 734 def _GetSchemaModel(self, platform, api_name): | 756 def _GetSchemaModel(self, platform, api_name): |
| 735 object_store_key = '/'.join((platform, api_name)) | 757 object_store_key = '/'.join((platform, api_name)) |
| 758 api_models = self._platform_bundle.GetAPIModels(platform) | |
| 736 jsc_model_future = self._model_cache.Get(object_store_key) | 759 jsc_model_future = self._model_cache.Get(object_store_key) |
| 737 model_future = self._platform_bundle.GetAPIModels(platform).GetModel( | 760 model_future = api_models.GetModel(api_name) |
| 738 api_name) | 761 content_script_apis_future = api_models.GetContentScriptAPIs() |
| 739 def resolve(): | 762 def resolve(): |
| 740 jsc_model = jsc_model_future.Get() | 763 jsc_model = jsc_model_future.Get() |
| 741 if jsc_model is None: | 764 if jsc_model is None: |
| 742 jsc_model = _JSCModel( | 765 jsc_model = _JSCModel( |
| 766 content_script_apis_future.Get(), | |
| 743 model_future.Get(), | 767 model_future.Get(), |
| 744 self._platform_bundle.GetAvailabilityFinder(platform), | 768 self._platform_bundle.GetAvailabilityFinder(platform), |
| 745 self._json_cache, | 769 self._json_cache, |
| 746 self._template_cache, | 770 self._template_cache, |
| 747 self._platform_bundle.GetFeaturesBundle(platform), | 771 self._platform_bundle.GetFeaturesBundle(platform), |
| 748 self._LoadEventByName(platform)).ToDict() | 772 self._LoadEventByName(platform)).ToDict() |
| 749 self._model_cache.Set(object_store_key, jsc_model) | 773 self._model_cache.Set(object_store_key, jsc_model) |
| 750 return jsc_model | 774 return jsc_model |
| 751 return Future(callback=resolve) | 775 return Future(callback=resolve) |
| 752 | 776 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 770 getter = lambda: 0 | 794 getter = lambda: 0 |
| 771 getter.get = lambda api_name: self._GetImpl(platform, api_name).Get() | 795 getter.get = lambda api_name: self._GetImpl(platform, api_name).Get() |
| 772 return getter | 796 return getter |
| 773 | 797 |
| 774 def Cron(self): | 798 def Cron(self): |
| 775 futures = [] | 799 futures = [] |
| 776 for platform in GetPlatforms(): | 800 for platform in GetPlatforms(): |
| 777 futures += [self._GetImpl(platform, name) | 801 futures += [self._GetImpl(platform, name) |
| 778 for name in self._platform_bundle.GetAPIModels(platform).GetNames()] | 802 for name in self._platform_bundle.GetAPIModels(platform).GetNames()] |
| 779 return Collect(futures, except_pass=FileNotFoundError) | 803 return Collect(futures, except_pass=FileNotFoundError) |
| OLD | NEW |