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 import copy | 5 import copy |
| 6 import logging | 6 import logging |
| 7 import os | 7 import os |
| 8 from collections import defaultdict, Mapping | 8 from collections import defaultdict, Mapping |
| 9 | 9 |
| 10 from branch_utility import BranchUtility | |
| 11 from availability_finder import _EXPERIMENTAL_MESSAGE | |
| 12 import svn_constants | |
| 10 import third_party.json_schema_compiler.json_parse as json_parse | 13 import third_party.json_schema_compiler.json_parse as json_parse |
| 11 import third_party.json_schema_compiler.model as model | 14 import third_party.json_schema_compiler.model as model |
| 12 import third_party.json_schema_compiler.idl_schema as idl_schema | 15 import third_party.json_schema_compiler.idl_schema as idl_schema |
| 13 import third_party.json_schema_compiler.idl_parser as idl_parser | 16 import third_party.json_schema_compiler.idl_parser as idl_parser |
| 14 | 17 |
| 15 def _RemoveNoDocs(item): | 18 def _RemoveNoDocs(item): |
| 16 if json_parse.IsDict(item): | 19 if json_parse.IsDict(item): |
| 17 if item.get('nodoc', False): | 20 if item.get('nodoc', False): |
| 18 return True | 21 return True |
| 19 for key, value in item.items(): | 22 for key, value in item.items(): |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 def _FormatValue(value): | 99 def _FormatValue(value): |
| 97 """Inserts commas every three digits for integer values. It is magic. | 100 """Inserts commas every three digits for integer values. It is magic. |
| 98 """ | 101 """ |
| 99 s = str(value) | 102 s = str(value) |
| 100 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) | 103 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) |
| 101 | 104 |
| 102 class _JSCModel(object): | 105 class _JSCModel(object): |
| 103 """Uses a Model from the JSON Schema Compiler and generates a dict that | 106 """Uses a Model from the JSON Schema Compiler and generates a dict that |
| 104 a Handlebar template can use for a data source. | 107 a Handlebar template can use for a data source. |
| 105 """ | 108 """ |
| 106 def __init__(self, json, ref_resolver, disable_refs, idl=False): | 109 def __init__(self, |
| 110 json, | |
| 111 ref_resolver, | |
| 112 disable_refs, | |
| 113 availability_finder, | |
| 114 intro_cache, | |
| 115 idl=False): | |
| 107 self._ref_resolver = ref_resolver | 116 self._ref_resolver = ref_resolver |
| 108 self._disable_refs = disable_refs | 117 self._disable_refs = disable_refs |
| 118 self._availability_finder = availability_finder | |
| 119 self._intro_tables = intro_cache.GetFromFile( | |
| 120 '%s/intro_tables.json' % svn_constants.JSON_PATH) | |
| 109 clean_json = copy.deepcopy(json) | 121 clean_json = copy.deepcopy(json) |
| 110 if _RemoveNoDocs(clean_json): | 122 if _RemoveNoDocs(clean_json): |
| 111 self._namespace = None | 123 self._namespace = None |
| 112 else: | 124 else: |
| 113 if idl: | 125 if idl: |
| 114 _DetectInlineableTypes(clean_json) | 126 _DetectInlineableTypes(clean_json) |
| 115 _InlineDocs(clean_json) | 127 _InlineDocs(clean_json) |
| 116 self._namespace = model.Namespace(clean_json, clean_json['namespace']) | 128 self._namespace = model.Namespace(clean_json, clean_json['namespace']) |
| 117 | 129 |
| 118 def _FormatDescription(self, description): | 130 def _FormatDescription(self, description): |
| 119 if self._disable_refs: | 131 if self._disable_refs: |
| 120 return description | 132 return description |
| 121 return self._ref_resolver.ResolveAllLinks(description, | 133 return self._ref_resolver.ResolveAllLinks(description, |
| 122 namespace=self._namespace.name) | 134 namespace=self._namespace.name) |
| 123 | 135 |
| 124 def _GetLink(self, link): | 136 def _GetLink(self, link): |
| 125 if self._disable_refs: | 137 if self._disable_refs: |
| 126 type_name = link.split('.', 1)[-1] | 138 type_name = link.split('.', 1)[-1] |
| 127 return { 'href': '#type-%s' % type_name, 'text': link, 'name': link } | 139 return { 'href': '#type-%s' % type_name, 'text': link, 'name': link } |
| 128 return self._ref_resolver.SafeGetLink(link, namespace=self._namespace.name) | 140 return self._ref_resolver.SafeGetLink(link, namespace=self._namespace.name) |
| 129 | 141 |
| 130 def ToDict(self): | 142 def ToDict(self): |
| 131 if self._namespace is None: | 143 if self._namespace is None: |
| 132 return {} | 144 return {} |
| 133 return { | 145 return { |
| 134 'name': self._namespace.name, | 146 'name': self._namespace.name, |
| 135 'description': self._namespace.description, | |
| 136 'types': self._GenerateTypes(self._namespace.types.values()), | 147 'types': self._GenerateTypes(self._namespace.types.values()), |
| 137 'functions': self._GenerateFunctions(self._namespace.functions), | 148 'functions': self._GenerateFunctions(self._namespace.functions), |
| 138 'events': self._GenerateEvents(self._namespace.events), | 149 'events': self._GenerateEvents(self._namespace.events), |
| 139 'properties': self._GenerateProperties(self._namespace.properties) | 150 'properties': self._GenerateProperties(self._namespace.properties), |
| 151 'intro_list': self._GetIntroTableList() | |
| 140 } | 152 } |
| 141 | 153 |
| 154 def _GetIntroTableList(self): | |
| 155 """Create a generic data structure that can be traversed by the templates | |
| 156 to create an API intro table. | |
| 157 """ | |
| 158 intro_list = [] | |
| 159 intro_list.append({ | |
| 160 'title': 'Description', | |
| 161 'content': [ | |
| 162 { 'text': self._FormatDescription(self._namespace.description) } | |
| 163 ] | |
| 164 }) | |
| 165 if self._IsExperimental(): | |
| 166 availability_text = _EXPERIMENTAL_MESSAGE | |
| 167 else: | |
| 168 availability_text = self._GetApiAvailabilityString() | |
| 169 intro_list.append({ | |
| 170 'title': 'Availability', | |
| 171 'content': [{ 'text': availability_text }] | |
| 172 }) | |
| 173 | |
| 174 # Look up the API name in intro_tables.json, which is structured similarly | |
| 175 # to the data structure being created. If the name is found, loop through | |
| 176 # the attributes and add them to this structure. | |
| 177 table_info = self._intro_tables.get(self._namespace.name) | |
| 178 if table_info is None: | |
| 179 return intro_list | |
| 180 | |
| 181 # The intro tables have a specific ordering that needs to be followed. | |
| 182 ordering = ('Permissions', 'Samples', 'Learn More') | |
| 183 | |
| 184 for category in ordering: | |
| 185 if category in table_info.keys(): | |
| 186 node = { 'title': category, 'content': [] } | |
| 187 for obj in table_info[category]: | |
| 188 node['content'].append(obj) | |
|
not at google - send to devlin
2013/07/09 23:11:55
node['content'] += table_info[category]
? or even
epeterson
2013/07/16 00:28:23
Done.
| |
| 189 intro_list.append(node) | |
| 190 return intro_list | |
| 191 | |
| 192 def _GetApiAvailabilityString(self): | |
| 193 return self._availability_finder.StringifyAvailability( | |
| 194 self._availability_finder.GetApiAvailability(self._namespace.name)) | |
| 195 | |
| 196 def _IsExperimental(self): | |
| 197 return self._namespace.name.startswith('experimental') | |
| 198 | |
| 142 def _GenerateTypes(self, types): | 199 def _GenerateTypes(self, types): |
| 143 return [self._GenerateType(t) for t in types] | 200 return [self._GenerateType(t) for t in types] |
| 144 | 201 |
| 145 def _GenerateType(self, type_): | 202 def _GenerateType(self, type_): |
| 146 type_dict = { | 203 type_dict = { |
| 147 'name': type_.simple_name, | 204 'name': type_.simple_name, |
| 148 'description': self._FormatDescription(type_.description), | 205 'description': self._FormatDescription(type_.description), |
| 149 'properties': self._GenerateProperties(type_.properties), | 206 'properties': self._GenerateProperties(type_.properties), |
| 150 'functions': self._GenerateFunctions(type_.functions), | 207 'functions': self._GenerateFunctions(type_.functions), |
| 151 'events': self._GenerateEvents(type_.events), | 208 'events': self._GenerateEvents(type_.events), |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 self._samples = samples | 375 self._samples = samples |
| 319 | 376 |
| 320 def get(self, key): | 377 def get(self, key): |
| 321 return self._samples.FilterSamples(key, self._api_name) | 378 return self._samples.FilterSamples(key, self._api_name) |
| 322 | 379 |
| 323 class APIDataSource(object): | 380 class APIDataSource(object): |
| 324 """This class fetches and loads JSON APIs from the FileSystem passed in with | 381 """This class fetches and loads JSON APIs from the FileSystem passed in with |
| 325 |compiled_fs_factory|, so the APIs can be plugged into templates. | 382 |compiled_fs_factory|, so the APIs can be plugged into templates. |
| 326 """ | 383 """ |
| 327 class Factory(object): | 384 class Factory(object): |
| 328 def __init__(self, compiled_fs_factory, base_path): | 385 def __init__(self, |
| 386 compiled_fs_factory, | |
| 387 base_path, | |
| 388 availability_finder_factory): | |
| 329 def create_compiled_fs(fn, category): | 389 def create_compiled_fs(fn, category): |
| 330 return compiled_fs_factory.Create(fn, APIDataSource, category=category) | 390 return compiled_fs_factory.Create(fn, APIDataSource, category=category) |
| 331 | 391 |
| 332 self._permissions_cache = create_compiled_fs(self._LoadPermissions, | 392 self._permissions_cache = create_compiled_fs(self._LoadPermissions, |
| 333 'permissions') | 393 'permissions') |
| 334 | 394 |
| 335 self._json_cache = create_compiled_fs( | 395 self._json_cache = create_compiled_fs( |
| 336 lambda api_name, api: self._LoadJsonAPI(api, False), | 396 lambda api_name, api: self._LoadJsonAPI(api, False), |
| 337 'json') | 397 'json') |
| 338 self._idl_cache = create_compiled_fs( | 398 self._idl_cache = create_compiled_fs( |
| 339 lambda api_name, api: self._LoadIdlAPI(api, False), | 399 lambda api_name, api: self._LoadIdlAPI(api, False), |
| 340 'idl') | 400 'idl') |
| 341 | 401 |
| 342 # These caches are used if an APIDataSource does not want to resolve the | 402 # These caches are used if an APIDataSource does not want to resolve the |
| 343 # $refs in an API. This is needed to prevent infinite recursion in | 403 # $refs in an API. This is needed to prevent infinite recursion in |
| 344 # ReferenceResolver. | 404 # ReferenceResolver. |
| 345 self._json_cache_no_refs = create_compiled_fs( | 405 self._json_cache_no_refs = create_compiled_fs( |
| 346 lambda api_name, api: self._LoadJsonAPI(api, True), | 406 lambda api_name, api: self._LoadJsonAPI(api, True), |
| 347 'json-no-refs') | 407 'json-no-refs') |
| 348 self._idl_cache_no_refs = create_compiled_fs( | 408 self._idl_cache_no_refs = create_compiled_fs( |
| 349 lambda api_name, api: self._LoadIdlAPI(api, True), | 409 lambda api_name, api: self._LoadIdlAPI(api, True), |
| 350 'idl-no-refs') | 410 'idl-no-refs') |
| 351 | 411 |
| 352 self._idl_names_cache = create_compiled_fs(self._GetIDLNames, 'idl-names') | 412 self._idl_names_cache = create_compiled_fs(self._GetIDLNames, 'idl-names') |
| 353 self._names_cache = create_compiled_fs(self._GetAllNames, 'names') | 413 self._names_cache = create_compiled_fs(self._GetAllNames, 'names') |
| 354 | 414 |
| 355 self._base_path = base_path | 415 self._base_path = base_path |
| 356 | 416 self._availability_finder = availability_finder_factory.Create() |
| 417 self._intro_cache = create_compiled_fs( | |
| 418 lambda _, json: json_parse.Parse(json), | |
| 419 'intro-cache') | |
| 357 # These must be set later via the SetFooDataSourceFactory methods. | 420 # These must be set later via the SetFooDataSourceFactory methods. |
| 358 self._ref_resolver_factory = None | 421 self._ref_resolver_factory = None |
| 359 self._samples_data_source_factory = None | 422 self._samples_data_source_factory = None |
| 360 | 423 |
| 361 def SetSamplesDataSourceFactory(self, samples_data_source_factory): | 424 def SetSamplesDataSourceFactory(self, samples_data_source_factory): |
| 362 self._samples_data_source_factory = samples_data_source_factory | 425 self._samples_data_source_factory = samples_data_source_factory |
| 363 | 426 |
| 364 def SetReferenceResolverFactory(self, ref_resolver_factory): | 427 def SetReferenceResolverFactory(self, ref_resolver_factory): |
| 365 self._ref_resolver_factory = ref_resolver_factory | 428 self._ref_resolver_factory = ref_resolver_factory |
| 366 | 429 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 392 samples, | 455 samples, |
| 393 disable_refs) | 456 disable_refs) |
| 394 | 457 |
| 395 def _LoadPermissions(self, file_name, json_str): | 458 def _LoadPermissions(self, file_name, json_str): |
| 396 return json_parse.Parse(json_str) | 459 return json_parse.Parse(json_str) |
| 397 | 460 |
| 398 def _LoadJsonAPI(self, api, disable_refs): | 461 def _LoadJsonAPI(self, api, disable_refs): |
| 399 return _JSCModel( | 462 return _JSCModel( |
| 400 json_parse.Parse(api)[0], | 463 json_parse.Parse(api)[0], |
| 401 self._ref_resolver_factory.Create() if not disable_refs else None, | 464 self._ref_resolver_factory.Create() if not disable_refs else None, |
| 402 disable_refs).ToDict() | 465 disable_refs, |
| 466 self._availability_finder, | |
| 467 self._intro_cache).ToDict() | |
| 403 | 468 |
| 404 def _LoadIdlAPI(self, api, disable_refs): | 469 def _LoadIdlAPI(self, api, disable_refs): |
| 405 idl = idl_parser.IDLParser().ParseData(api) | 470 idl = idl_parser.IDLParser().ParseData(api) |
| 406 return _JSCModel( | 471 return _JSCModel( |
| 407 idl_schema.IDLSchema(idl).process()[0], | 472 idl_schema.IDLSchema(idl).process()[0], |
| 408 self._ref_resolver_factory.Create() if not disable_refs else None, | 473 self._ref_resolver_factory.Create() if not disable_refs else None, |
| 409 disable_refs, | 474 disable_refs, |
| 475 self._availability_finder, | |
| 476 self._intro_cache, | |
| 410 idl=True).ToDict() | 477 idl=True).ToDict() |
| 411 | 478 |
| 412 def _GetIDLNames(self, base_dir, apis): | 479 def _GetIDLNames(self, base_dir, apis): |
| 413 return self._GetExtNames(apis, ['idl']) | 480 return self._GetExtNames(apis, ['idl']) |
| 414 | 481 |
| 415 def _GetAllNames(self, base_dir, apis): | 482 def _GetAllNames(self, base_dir, apis): |
| 416 return self._GetExtNames(apis, ['json', 'idl']) | 483 return self._GetExtNames(apis, ['json', 'idl']) |
| 417 | 484 |
| 418 def _GetExtNames(self, apis, exts): | 485 def _GetExtNames(self, apis, exts): |
| 419 return [model.UnixName(os.path.splitext(api)[0]) for api in apis | 486 return [model.UnixName(os.path.splitext(api)[0]) for api in apis |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 477 return feature_data | 544 return feature_data |
| 478 | 545 |
| 479 def _GenerateHandlebarContext(self, handlebar_dict, path): | 546 def _GenerateHandlebarContext(self, handlebar_dict, path): |
| 480 handlebar_dict['permissions'] = self._GetFeatureData(path) | 547 handlebar_dict['permissions'] = self._GetFeatureData(path) |
| 481 handlebar_dict['samples'] = _LazySamplesGetter(path, self._samples) | 548 handlebar_dict['samples'] = _LazySamplesGetter(path, self._samples) |
| 482 return handlebar_dict | 549 return handlebar_dict |
| 483 | 550 |
| 484 def _GetAsSubdirectory(self, name): | 551 def _GetAsSubdirectory(self, name): |
| 485 if name.startswith('experimental_'): | 552 if name.startswith('experimental_'): |
| 486 parts = name[len('experimental_'):].split('_', 1) | 553 parts = name[len('experimental_'):].split('_', 1) |
| 487 parts[1] = 'experimental_%s' % parts[1] | 554 if len(parts) > 1: |
| 488 return '/'.join(parts) | 555 parts[1] = 'experimental_%s' % parts[1] |
| 556 return '/'.join(parts) | |
| 557 return '%s/%s' % (parts[0], name) | |
| 489 return name.replace('_', '/', 1) | 558 return name.replace('_', '/', 1) |
| 490 | 559 |
| 491 def get(self, key): | 560 def get(self, key): |
| 492 if key.endswith('.html') or key.endswith('.json') or key.endswith('.idl'): | 561 if key.endswith('.html') or key.endswith('.json') or key.endswith('.idl'): |
| 493 path, ext = os.path.splitext(key) | 562 path, ext = os.path.splitext(key) |
| 494 else: | 563 else: |
| 495 path = key | 564 path = key |
| 496 unix_name = model.UnixName(path) | 565 unix_name = model.UnixName(path) |
| 497 idl_names = self._idl_names_cache.GetFromFileListing(self._base_path) | 566 idl_names = self._idl_names_cache.GetFromFileListing(self._base_path) |
| 498 names = self._names_cache.GetFromFileListing(self._base_path) | 567 names = self._names_cache.GetFromFileListing(self._base_path) |
| 499 if unix_name not in names and self._GetAsSubdirectory(unix_name) in names: | 568 if unix_name not in names and self._GetAsSubdirectory(unix_name) in names: |
| 500 unix_name = self._GetAsSubdirectory(unix_name) | 569 unix_name = self._GetAsSubdirectory(unix_name) |
| 501 | 570 |
| 502 if self._disable_refs: | 571 if self._disable_refs: |
| 503 cache, ext = ( | 572 cache, ext = ( |
| 504 (self._idl_cache_no_refs, '.idl') if (unix_name in idl_names) else | 573 (self._idl_cache_no_refs, '.idl') if (unix_name in idl_names) else |
| 505 (self._json_cache_no_refs, '.json')) | 574 (self._json_cache_no_refs, '.json')) |
| 506 else: | 575 else: |
| 507 cache, ext = ((self._idl_cache, '.idl') if (unix_name in idl_names) else | 576 cache, ext = ((self._idl_cache, '.idl') if (unix_name in idl_names) else |
| 508 (self._json_cache, '.json')) | 577 (self._json_cache, '.json')) |
| 509 return self._GenerateHandlebarContext( | 578 return self._GenerateHandlebarContext( |
| 510 cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)), | 579 cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)), |
| 511 path) | 580 path) |
| OLD | NEW |