OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 posixpath | 7 import posixpath |
8 | 8 |
9 from api_models import GetNodeCategories | 9 from api_models import GetNodeCategories |
10 from api_schema_graph import APINodeCursor | 10 from api_schema_graph import APINodeCursor |
11 from docs_server_utils import MarkFirstAndLast | 11 from docs_server_utils import MarkFirstAndLast |
12 from extensions_paths import JSON_TEMPLATES, PRIVATE_TEMPLATES | 12 from extensions_paths import JSON_TEMPLATES, PRIVATE_TEMPLATES |
13 from operator import itemgetter | 13 from operator import itemgetter |
| 14 from platform_util import PlatformToExtensionType |
14 import third_party.json_schema_compiler.model as model | 15 import third_party.json_schema_compiler.model as model |
15 | 16 |
16 | 17 |
17 def GetEventByNameFromEvents(events): | 18 def GetEventByNameFromEvents(events): |
18 '''Parses the dictionary |events| to find the definitions of members of the | 19 '''Parses the dictionary |events| to find the definitions of members of the |
19 type Event. Returns a dictionary mapping the name of a member to that | 20 type Event. Returns a dictionary mapping the name of a member to that |
20 member's definition. | 21 member's definition. |
21 ''' | 22 ''' |
22 assert 'types' in events, \ | 23 assert 'types' in events, \ |
23 'The dictionary |events| must contain the key "types".' | 24 'The dictionary |events| must contain the key "types".' |
(...skipping 28 matching lines...) Expand all Loading... |
52 return '-'.join([prefix, node.simple_name]) | 53 return '-'.join([prefix, node.simple_name]) |
53 | 54 |
54 | 55 |
55 def _FormatValue(value): | 56 def _FormatValue(value): |
56 '''Inserts commas every three digits for integer values. It is magic. | 57 '''Inserts commas every three digits for integer values. It is magic. |
57 ''' | 58 ''' |
58 s = str(value) | 59 s = str(value) |
59 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) | 60 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) |
60 | 61 |
61 | 62 |
62 | |
63 | |
64 class JSCView(object): | 63 class JSCView(object): |
65 '''Uses a Model from the JSON Schema Compiler and generates a dict that | 64 '''Uses a Model from the JSON Schema Compiler and generates a dict that |
66 a Handlebar template can use for a data source. | 65 a Handlebar template can use for a data source. |
67 ''' | 66 ''' |
68 | 67 |
69 def __init__(self, | 68 def __init__(self, |
70 content_script_apis, | 69 content_script_apis, |
71 jsc_model, | 70 jsc_model, |
72 availability_finder, | 71 availability_finder, |
73 json_cache, | 72 json_cache, |
74 template_cache, | 73 template_cache, |
75 features_bundle, | 74 features_bundle, |
76 event_byname_future): | 75 event_byname_future, |
| 76 platform): |
77 self._content_script_apis = content_script_apis | 77 self._content_script_apis = content_script_apis |
78 self._availability = availability_finder.GetAPIAvailability(jsc_model.name) | 78 self._availability = availability_finder.GetAPIAvailability(jsc_model.name) |
79 self._current_node = APINodeCursor(availability_finder, jsc_model.name) | 79 self._current_node = APINodeCursor(availability_finder, jsc_model.name) |
80 self._api_availabilities = json_cache.GetFromFile( | 80 self._api_availabilities = json_cache.GetFromFile( |
81 posixpath.join(JSON_TEMPLATES, 'api_availabilities.json')) | 81 posixpath.join(JSON_TEMPLATES, 'api_availabilities.json')) |
82 self._intro_tables = json_cache.GetFromFile( | 82 self._intro_tables = json_cache.GetFromFile( |
83 posixpath.join(JSON_TEMPLATES, 'intro_tables.json')) | 83 posixpath.join(JSON_TEMPLATES, 'intro_tables.json')) |
84 self._api_features = features_bundle.GetAPIFeatures() | 84 self._api_features = features_bundle.GetAPIFeatures() |
85 self._template_cache = template_cache | 85 self._template_cache = template_cache |
86 self._event_byname_future = event_byname_future | 86 self._event_byname_future = event_byname_future |
87 self._jsc_model = jsc_model | 87 self._jsc_model = jsc_model |
| 88 self._platform = platform |
88 | 89 |
89 def _GetLink(self, link): | 90 def _GetLink(self, link): |
90 ref = link if '.' in link else (self._jsc_model.name + '.' + link) | 91 ref = link if '.' in link else (self._jsc_model.name + '.' + link) |
91 return { 'ref': ref, 'text': link, 'name': link } | 92 return { 'ref': ref, 'text': link, 'name': link } |
92 | 93 |
93 def ToDict(self): | 94 def ToDict(self): |
94 '''Returns a dictionary representation of |self._jsc_model|, which | 95 '''Returns a dictionary representation of |self._jsc_model|, which |
95 is a Namespace object from JSON Schema Compiler. | 96 is a Namespace object from JSON Schema Compiler. |
96 ''' | 97 ''' |
97 assert self._jsc_model is not None | 98 assert self._jsc_model is not None |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 # Look up the API name in intro_tables.json, which is structured | 538 # Look up the API name in intro_tables.json, which is structured |
538 # similarly to the data structure being created. If the name is found, loop | 539 # similarly to the data structure being created. If the name is found, loop |
539 # through the attributes and add them to this structure. | 540 # through the attributes and add them to this structure. |
540 table_info = self._intro_tables.Get().get(self._jsc_model.name) | 541 table_info = self._intro_tables.Get().get(self._jsc_model.name) |
541 if table_info is None: | 542 if table_info is None: |
542 return misc_rows | 543 return misc_rows |
543 | 544 |
544 for category in table_info.iterkeys(): | 545 for category in table_info.iterkeys(): |
545 content = [] | 546 content = [] |
546 for node in table_info[category]: | 547 for node in table_info[category]: |
| 548 ext_type = PlatformToExtensionType(self._platform) |
| 549 # Don't display nodes restricted to a different platform. |
| 550 if ext_type not in node.get('extension_types', (ext_type,)): |
| 551 continue |
547 # If there is a 'partial' argument and it hasn't already been | 552 # If there is a 'partial' argument and it hasn't already been |
548 # converted to a Handlebar object, transform it to a template. | 553 # converted to a Handlebar object, transform it to a template. |
549 if 'partial' in node: | 554 if 'partial' in node: |
550 # Note: it's enough to copy() not deepcopy() because only a single | 555 # Note: it's enough to copy() not deepcopy() because only a single |
551 # top-level key is being modified. | 556 # top-level key is being modified. |
552 node = copy(node) | 557 node = copy(node) |
553 node['partial'] = self._template_cache.GetFromFile( | 558 node['partial'] = self._template_cache.GetFromFile( |
554 posixpath.join(PRIVATE_TEMPLATES, node['partial'])).Get() | 559 posixpath.join(PRIVATE_TEMPLATES, node['partial'])).Get() |
555 content.append(node) | 560 content.append(node) |
556 misc_rows.append({ 'title': category, 'content': content }) | 561 misc_rows.append({ 'title': category, 'content': content }) |
557 return misc_rows | 562 return misc_rows |
OLD | NEW |