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