| 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 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 return '-'.join([prefix, node.simple_name]) | 52 return '-'.join([prefix, node.simple_name]) |
| 53 | 53 |
| 54 | 54 |
| 55 def _FormatValue(value): | 55 def _FormatValue(value): |
| 56 '''Inserts commas every three digits for integer values. It is magic. | 56 '''Inserts commas every three digits for integer values. It is magic. |
| 57 ''' | 57 ''' |
| 58 s = str(value) | 58 s = str(value) |
| 59 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) | 59 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) |
| 60 | 60 |
| 61 | 61 |
| 62 | |
| 63 | |
| 64 class JSCView(object): | 62 class JSCView(object): |
| 65 '''Uses a Model from the JSON Schema Compiler and generates a dict that | 63 '''Uses a Model from the JSON Schema Compiler and generates a dict that |
| 66 a Handlebar template can use for a data source. | 64 a Handlebar template can use for a data source. |
| 67 ''' | 65 ''' |
| 68 | 66 |
| 69 def __init__(self, | 67 def __init__(self, |
| 70 content_script_apis, | 68 content_script_apis, |
| 71 jsc_model, | 69 jsc_model, |
| 72 availability_finder, | 70 availability_finder, |
| 73 json_cache, | 71 json_cache, |
| 74 template_cache, | 72 template_cache, |
| 75 features_bundle, | 73 features_bundle, |
| 76 event_byname_future): | 74 event_byname_future, |
| 75 platform): |
| 77 self._content_script_apis = content_script_apis | 76 self._content_script_apis = content_script_apis |
| 78 self._availability = availability_finder.GetAPIAvailability(jsc_model.name) | 77 self._availability = availability_finder.GetAPIAvailability(jsc_model.name) |
| 79 self._current_node = APINodeCursor(availability_finder, jsc_model.name) | 78 self._current_node = APINodeCursor(availability_finder, jsc_model.name) |
| 80 self._api_availabilities = json_cache.GetFromFile( | 79 self._api_availabilities = json_cache.GetFromFile( |
| 81 posixpath.join(JSON_TEMPLATES, 'api_availabilities.json')) | 80 posixpath.join(JSON_TEMPLATES, 'api_availabilities.json')) |
| 82 self._intro_tables = json_cache.GetFromFile( | 81 self._intro_tables = json_cache.GetFromFile( |
| 83 posixpath.join(JSON_TEMPLATES, 'intro_tables.json')) | 82 posixpath.join(JSON_TEMPLATES, 'intro_tables.json')) |
| 84 self._api_features = features_bundle.GetAPIFeatures() | 83 self._api_features = features_bundle.GetAPIFeatures() |
| 85 self._template_cache = template_cache | 84 self._template_cache = template_cache |
| 86 self._event_byname_future = event_byname_future | 85 self._event_byname_future = event_byname_future |
| 87 self._jsc_model = jsc_model | 86 self._jsc_model = jsc_model |
| 87 self._platform = platform |
| 88 | 88 |
| 89 def _GetLink(self, link): | 89 def _GetLink(self, link): |
| 90 ref = link if '.' in link else (self._jsc_model.name + '.' + link) | 90 ref = link if '.' in link else (self._jsc_model.name + '.' + link) |
| 91 return { 'ref': ref, 'text': link, 'name': link } | 91 return { 'ref': ref, 'text': link, 'name': link } |
| 92 | 92 |
| 93 def ToDict(self): | 93 def ToDict(self): |
| 94 '''Returns a dictionary representation of |self._jsc_model|, which | 94 '''Returns a dictionary representation of |self._jsc_model|, which |
| 95 is a Namespace object from JSON Schema Compiler. | 95 is a Namespace object from JSON Schema Compiler. |
| 96 ''' | 96 ''' |
| 97 assert self._jsc_model is not None | 97 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 | 537 # 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 | 538 # similarly to the data structure being created. If the name is found, loop |
| 539 # through the attributes and add them to this structure. | 539 # through the attributes and add them to this structure. |
| 540 table_info = self._intro_tables.Get().get(self._jsc_model.name) | 540 table_info = self._intro_tables.Get().get(self._jsc_model.name) |
| 541 if table_info is None: | 541 if table_info is None: |
| 542 return misc_rows | 542 return misc_rows |
| 543 | 543 |
| 544 for category in table_info.iterkeys(): | 544 for category in table_info.iterkeys(): |
| 545 content = [] | 545 content = [] |
| 546 for node in table_info[category]: | 546 for node in table_info[category]: |
| 547 # Don't display nodes restricted to a different platform. |
| 548 if node.get('restrictedTo', self._platform) != self._platform: |
| 549 continue |
| 547 # If there is a 'partial' argument and it hasn't already been | 550 # If there is a 'partial' argument and it hasn't already been |
| 548 # converted to a Handlebar object, transform it to a template. | 551 # converted to a Handlebar object, transform it to a template. |
| 549 if 'partial' in node: | 552 if 'partial' in node: |
| 550 # Note: it's enough to copy() not deepcopy() because only a single | 553 # Note: it's enough to copy() not deepcopy() because only a single |
| 551 # top-level key is being modified. | 554 # top-level key is being modified. |
| 552 node = copy(node) | 555 node = copy(node) |
| 553 node['partial'] = self._template_cache.GetFromFile( | 556 node['partial'] = self._template_cache.GetFromFile( |
| 554 posixpath.join(PRIVATE_TEMPLATES, node['partial'])).Get() | 557 posixpath.join(PRIVATE_TEMPLATES, node['partial'])).Get() |
| 555 content.append(node) | 558 content.append(node) |
| 556 misc_rows.append({ 'title': category, 'content': content }) | 559 misc_rows.append({ 'title': category, 'content': content }) |
| 557 return misc_rows | 560 return misc_rows |
| OLD | NEW |