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 |
|
cduvall
2013/03/21 18:43:53
Add the newline back in after this (separates pyth
epeterson
2013/03/25 19:35:11
Done.
| |
| 8 | |
| 9 import compiled_file_system as compiled_fs | 8 import compiled_file_system as compiled_fs |
| 10 from file_system import FileNotFoundError | 9 from file_system import FileNotFoundError |
| 11 import third_party.json_schema_compiler.json_parse as json_parse | 10 import third_party.json_schema_compiler.json_parse as json_parse |
| 12 import third_party.json_schema_compiler.model as model | 11 import third_party.json_schema_compiler.model as model |
| 13 import third_party.json_schema_compiler.idl_schema as idl_schema | 12 import third_party.json_schema_compiler.idl_schema as idl_schema |
| 14 import third_party.json_schema_compiler.idl_parser as idl_parser | 13 import third_party.json_schema_compiler.idl_parser as idl_parser |
| 14 from third_party.json_schema_compiler.model import ParseException | |
| 15 | 15 |
| 16 # Increment this version when there are changes to the data stored in any of | 16 # Increment this version when there are changes to the data stored in any of |
| 17 # the caches used by APIDataSource. This would include changes to model.py in | 17 # the caches used by APIDataSource. This would include changes to model.py in |
| 18 # JSON schema compiler! This allows the cache to be invalidated without having | 18 # JSON schema compiler! This allows the cache to be invalidated without having |
| 19 # to flush memcache on the production server. | 19 # to flush memcache on the production server. |
| 20 _VERSION = 15 | 20 _VERSION = 16 |
| 21 | 21 |
| 22 def _RemoveNoDocs(item): | 22 def _RemoveNoDocs(item): |
| 23 if json_parse.IsDict(item): | 23 if json_parse.IsDict(item): |
| 24 if item.get('nodoc', False): | 24 if item.get('nodoc', False): |
| 25 return True | 25 return True |
| 26 to_remove = [] | 26 to_remove = [] |
| 27 for key, value in item.items(): | 27 for key, value in item.items(): |
| 28 if _RemoveNoDocs(value): | 28 if _RemoveNoDocs(value): |
| 29 to_remove.append(key) | 29 to_remove.append(key) |
| 30 for k in to_remove: | 30 for k in to_remove: |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 46 def _FormatValue(value): | 46 def _FormatValue(value): |
| 47 """Inserts commas every three digits for integer values. It is magic. | 47 """Inserts commas every three digits for integer values. It is magic. |
| 48 """ | 48 """ |
| 49 s = str(value) | 49 s = str(value) |
| 50 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) | 50 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) |
| 51 | 51 |
| 52 class _JSCModel(object): | 52 class _JSCModel(object): |
| 53 """Uses a Model from the JSON Schema Compiler and generates a dict that | 53 """Uses a Model from the JSON Schema Compiler and generates a dict that |
| 54 a Handlebar template can use for a data source. | 54 a Handlebar template can use for a data source. |
| 55 """ | 55 """ |
| 56 def __init__(self, json, ref_resolver, disable_refs): | 56 def __init__(self, |
| 57 json, | |
| 58 ref_resolver, | |
| 59 disable_refs, | |
| 60 availability_data_source=None): | |
| 57 self._ref_resolver = ref_resolver | 61 self._ref_resolver = ref_resolver |
| 58 self._disable_refs = disable_refs | 62 self._disable_refs = disable_refs |
| 63 self._availability_data_source = availability_data_source | |
| 59 clean_json = copy.deepcopy(json) | 64 clean_json = copy.deepcopy(json) |
| 60 if _RemoveNoDocs(clean_json): | 65 if _RemoveNoDocs(clean_json): |
| 61 self._namespace = None | 66 self._namespace = None |
| 62 else: | 67 else: |
| 63 self._namespace = model.Namespace(clean_json, clean_json['namespace']) | 68 self._namespace = model.Namespace(clean_json, clean_json['namespace']) |
| 64 | 69 |
| 65 def _FormatDescription(self, description): | 70 def _FormatDescription(self, description): |
| 66 if self._disable_refs: | 71 if self._disable_refs: |
| 67 return description | 72 return description |
| 68 return self._ref_resolver.ResolveAllLinks(description, | 73 return self._ref_resolver.ResolveAllLinks(description, |
| 69 namespace=self._namespace.name) | 74 namespace=self._namespace.name) |
| 70 | 75 |
| 71 def _GetLink(self, link): | 76 def _GetLink(self, link): |
| 72 if self._disable_refs: | 77 if self._disable_refs: |
| 73 type_name = link.split('.', 1)[-1] | 78 type_name = link.split('.', 1)[-1] |
| 74 return { 'href': '#type-%s' % type_name, 'text': link, 'name': link } | 79 return { 'href': '#type-%s' % type_name, 'text': link, 'name': link } |
| 75 return self._ref_resolver.SafeGetLink(link, namespace=self._namespace.name) | 80 return self._ref_resolver.SafeGetLink(link, namespace=self._namespace.name) |
| 76 | 81 |
| 77 def ToDict(self): | 82 def ToDict(self): |
| 78 if self._namespace is None: | 83 if self._namespace is None: |
| 79 return {} | 84 return {} |
| 85 if self._namespace.availability is None: | |
| 86 availability = self._GetAvailability() | |
| 87 if availability is not None: | |
| 88 #availability found using filesystem | |
|
cduvall
2013/03/21 18:43:53
Space after # in all comments.
epeterson
2013/03/25 19:35:11
Done.
| |
| 89 availability_generated = True | |
| 90 else: | |
| 91 #we don't know anything about this api's availability | |
| 92 availability = None | |
|
cduvall
2013/03/21 18:43:53
No need to set to None, we already know it is
epeterson
2013/03/25 19:35:11
Done.
| |
| 93 availability_generated = None | |
|
cduvall
2013/03/21 18:43:53
Should be False not None
epeterson
2013/03/25 19:35:11
Done.
| |
| 94 else: | |
| 95 #availability was already present in a .json/.idl file | |
| 96 availability = self._namespace.availability | |
| 97 availability_generated = None | |
|
cduvall
2013/03/21 18:43:53
Just set availability and availability_generated a
epeterson
2013/03/25 19:35:11
Done.
| |
| 80 return { | 98 return { |
| 81 'name': self._namespace.name, | 99 'name': self._namespace.name, |
| 100 'description': self._namespace.description, | |
| 101 'availability': availability, | |
| 102 'availability_generated': availability_generated, | |
| 82 'types': self._GenerateTypes(self._namespace.types.values()), | 103 'types': self._GenerateTypes(self._namespace.types.values()), |
| 83 'functions': self._GenerateFunctions(self._namespace.functions), | 104 'functions': self._GenerateFunctions(self._namespace.functions), |
| 84 'events': self._GenerateEvents(self._namespace.events), | 105 'events': self._GenerateEvents(self._namespace.events), |
| 85 'properties': self._GenerateProperties(self._namespace.properties) | 106 'properties': self._GenerateProperties(self._namespace.properties) |
| 86 } | 107 } |
| 87 | 108 |
| 109 def _GetAvailability(self): | |
| 110 """Returns the earliest version of chrome, represented by a version number, | |
| 111 that this api was available in | |
| 112 """ | |
| 113 if self._availability_data_source is not None: | |
| 114 return self._availability_data_source.FindEarliestAvailability( | |
| 115 self._namespace.name) | |
| 116 | |
| 88 def _GenerateTypes(self, types): | 117 def _GenerateTypes(self, types): |
| 89 return [self._GenerateType(t) for t in types] | 118 return [self._GenerateType(t) for t in types] |
| 90 | 119 |
| 91 def _GenerateType(self, type_): | 120 def _GenerateType(self, type_): |
| 92 type_dict = { | 121 type_dict = { |
| 93 'name': type_.simple_name, | 122 'name': type_.simple_name, |
| 94 'description': self._FormatDescription(type_.description), | 123 'description': self._FormatDescription(type_.description), |
| 95 'properties': self._GenerateProperties(type_.properties), | 124 'properties': self._GenerateProperties(type_.properties), |
| 96 'functions': self._GenerateFunctions(type_.functions), | 125 'functions': self._GenerateFunctions(type_.functions), |
| 97 'events': self._GenerateEvents(type_.events), | 126 'events': self._GenerateEvents(type_.events), |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 265 def get(self, key): | 294 def get(self, key): |
| 266 return self._samples.FilterSamples(key, self._api_name) | 295 return self._samples.FilterSamples(key, self._api_name) |
| 267 | 296 |
| 268 class APIDataSource(object): | 297 class APIDataSource(object): |
| 269 """This class fetches and loads JSON APIs from the FileSystem passed in with | 298 """This class fetches and loads JSON APIs from the FileSystem passed in with |
| 270 |cache_factory|, so the APIs can be plugged into templates. | 299 |cache_factory|, so the APIs can be plugged into templates. |
| 271 """ | 300 """ |
| 272 class Factory(object): | 301 class Factory(object): |
| 273 def __init__(self, | 302 def __init__(self, |
| 274 cache_factory, | 303 cache_factory, |
| 275 base_path): | 304 base_path, |
| 305 availability_data_source=None): | |
| 276 self._permissions_cache = cache_factory.Create(self._LoadPermissions, | 306 self._permissions_cache = cache_factory.Create(self._LoadPermissions, |
| 277 compiled_fs.PERMS, | 307 compiled_fs.PERMS, |
| 278 version=_VERSION) | 308 version=_VERSION) |
| 279 self._json_cache = cache_factory.Create( | 309 self._json_cache = cache_factory.Create( |
| 280 lambda api_name, api: self._LoadJsonAPI(api, False), | 310 lambda api_name, api: self._LoadJsonAPI(api, False), |
| 281 compiled_fs.JSON, | 311 compiled_fs.JSON, |
| 282 version=_VERSION) | 312 version=_VERSION) |
| 283 self._idl_cache = cache_factory.Create( | 313 self._idl_cache = cache_factory.Create( |
| 284 lambda api_name, api: self._LoadIdlAPI(api, False), | 314 lambda api_name, api: self._LoadIdlAPI(api, False), |
| 285 compiled_fs.IDL, | 315 compiled_fs.IDL, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 296 lambda api_name, api: self._LoadIdlAPI(api, True), | 326 lambda api_name, api: self._LoadIdlAPI(api, True), |
| 297 compiled_fs.IDL_NO_REFS, | 327 compiled_fs.IDL_NO_REFS, |
| 298 version=_VERSION) | 328 version=_VERSION) |
| 299 self._idl_names_cache = cache_factory.Create(self._GetIDLNames, | 329 self._idl_names_cache = cache_factory.Create(self._GetIDLNames, |
| 300 compiled_fs.IDL_NAMES, | 330 compiled_fs.IDL_NAMES, |
| 301 version=_VERSION) | 331 version=_VERSION) |
| 302 self._names_cache = cache_factory.Create(self._GetAllNames, | 332 self._names_cache = cache_factory.Create(self._GetAllNames, |
| 303 compiled_fs.NAMES, | 333 compiled_fs.NAMES, |
| 304 version=_VERSION) | 334 version=_VERSION) |
| 305 self._base_path = base_path | 335 self._base_path = base_path |
| 306 | 336 self._availability_data_source = availability_data_source |
| 307 # These must be set later via the SetFooDataSourceFactory methods. | 337 # These must be set later via the SetFooDataSourceFactory methods. |
| 308 self._ref_resolver_factory = None | 338 self._ref_resolver_factory = None |
| 309 self._samples_data_source_factory = None | 339 self._samples_data_source_factory = None |
| 310 | 340 |
| 311 def SetSamplesDataSourceFactory(self, samples_data_source_factory): | 341 def SetSamplesDataSourceFactory(self, samples_data_source_factory): |
| 312 self._samples_data_source_factory = samples_data_source_factory | 342 self._samples_data_source_factory = samples_data_source_factory |
| 313 | 343 |
| 314 def SetReferenceResolverFactory(self, ref_resolver_factory): | 344 def SetReferenceResolverFactory(self, ref_resolver_factory): |
| 315 self._ref_resolver_factory = ref_resolver_factory | 345 self._ref_resolver_factory = ref_resolver_factory |
| 316 | 346 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 342 samples, | 372 samples, |
| 343 disable_refs) | 373 disable_refs) |
| 344 | 374 |
| 345 def _LoadPermissions(self, file_name, json_str): | 375 def _LoadPermissions(self, file_name, json_str): |
| 346 return json_parse.Parse(json_str) | 376 return json_parse.Parse(json_str) |
| 347 | 377 |
| 348 def _LoadJsonAPI(self, api, disable_refs): | 378 def _LoadJsonAPI(self, api, disable_refs): |
| 349 return _JSCModel( | 379 return _JSCModel( |
| 350 json_parse.Parse(api)[0], | 380 json_parse.Parse(api)[0], |
| 351 self._ref_resolver_factory.Create() if not disable_refs else None, | 381 self._ref_resolver_factory.Create() if not disable_refs else None, |
| 352 disable_refs).ToDict() | 382 disable_refs, |
| 383 self._availability_data_source).ToDict() | |
| 353 | 384 |
| 354 def _LoadIdlAPI(self, api, disable_refs): | 385 def _LoadIdlAPI(self, api, disable_refs): |
| 355 idl = idl_parser.IDLParser().ParseData(api) | 386 idl = idl_parser.IDLParser().ParseData(api) |
| 356 return _JSCModel( | 387 return _JSCModel( |
| 357 idl_schema.IDLSchema(idl).process()[0], | 388 idl_schema.IDLSchema(idl).process()[0], |
| 358 self._ref_resolver_factory.Create() if not disable_refs else None, | 389 self._ref_resolver_factory.Create() if not disable_refs else None, |
| 359 disable_refs).ToDict() | 390 disable_refs, |
| 391 self._availability_data_source).ToDict() | |
| 360 | 392 |
| 361 def _GetIDLNames(self, base_dir, apis): | 393 def _GetIDLNames(self, base_dir, apis): |
| 362 return [ | 394 return [ |
| 363 model.UnixName(os.path.splitext(api[len('%s/' % self._base_path):])[0]) | 395 model.UnixName(os.path.splitext(api[len('%s/' % self._base_path):])[0]) |
| 364 for api in apis if api.endswith('.idl') | 396 for api in apis if api.endswith('.idl') |
| 365 ] | 397 ] |
| 366 | 398 |
| 367 def _GetAllNames(self, base_dir, apis): | 399 def _GetAllNames(self, base_dir, apis): |
| 368 return [ | 400 return [ |
| 369 model.UnixName(os.path.splitext(api[len('%s/' % self._base_path):])[0]) | 401 model.UnixName(os.path.splitext(api[len('%s/' % self._base_path):])[0]) |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 435 handlebar_dict['samples'] = _LazySamplesGetter(path, self._samples) | 467 handlebar_dict['samples'] = _LazySamplesGetter(path, self._samples) |
| 436 return handlebar_dict | 468 return handlebar_dict |
| 437 | 469 |
| 438 def _GetAsSubdirectory(self, name): | 470 def _GetAsSubdirectory(self, name): |
| 439 if name.startswith('experimental_'): | 471 if name.startswith('experimental_'): |
| 440 parts = name[len('experimental_'):].split('_', 1) | 472 parts = name[len('experimental_'):].split('_', 1) |
| 441 parts[1] = 'experimental_%s' % parts[1] | 473 parts[1] = 'experimental_%s' % parts[1] |
| 442 return '/'.join(parts) | 474 return '/'.join(parts) |
| 443 return name.replace('_', '/', 1) | 475 return name.replace('_', '/', 1) |
| 444 | 476 |
| 445 def get(self, key): | 477 def _DetermineNamesForGet(self, key): |
| 446 if key.endswith('.html') or key.endswith('.json') or key.endswith('.idl'): | 478 if key.endswith('.html') or key.endswith('.json') or key.endswith('.idl'): |
| 447 path, ext = os.path.splitext(key) | 479 path, ext = os.path.splitext(key) |
| 448 else: | 480 else: |
| 449 path = key | 481 path = key |
| 450 unix_name = model.UnixName(path) | 482 unix_name = model.UnixName(path) |
| 451 idl_names = self._idl_names_cache.GetFromFileListing(self._base_path) | |
| 452 names = self._names_cache.GetFromFileListing(self._base_path) | 483 names = self._names_cache.GetFromFileListing(self._base_path) |
| 453 if unix_name not in names and self._GetAsSubdirectory(unix_name) in names: | 484 if unix_name not in names and self._GetAsSubdirectory(unix_name) in names: |
| 454 unix_name = self._GetAsSubdirectory(unix_name) | 485 unix_name = self._GetAsSubdirectory(unix_name) |
| 486 return (path, unix_name) | |
| 455 | 487 |
| 488 def _DetermineCacheForGet(self, unix_name): | |
| 489 idl_names = self._idl_names_cache.GetFromFileListing(self._base_path) | |
| 456 if self._disable_refs: | 490 if self._disable_refs: |
| 457 cache, ext = ( | 491 cache, ext = ( |
| 458 (self._idl_cache_no_refs, '.idl') if (unix_name in idl_names) else | 492 (self._idl_cache_no_refs, '.idl') if (unix_name in idl_names) else |
| 459 (self._json_cache_no_refs, '.json')) | 493 (self._json_cache_no_refs, '.json')) |
| 460 else: | 494 else: |
| 461 cache, ext = ((self._idl_cache, '.idl') if (unix_name in idl_names) else | 495 cache, ext = ((self._idl_cache, '.idl') if (unix_name in idl_names) else |
| 462 (self._json_cache, '.json')) | 496 (self._json_cache, '.json')) |
| 497 return (cache, ext) | |
| 498 | |
| 499 def get(self, key): | |
| 500 path, unix_name = self._DetermineNamesForGet(key) | |
| 501 cache, ext = self._DetermineCacheForGet(unix_name) | |
| 463 return self._GenerateHandlebarContext( | 502 return self._GenerateHandlebarContext( |
| 464 cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)), | 503 cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)), |
| 465 path) | 504 path) |
| 505 | |
| 506 def tryGet(self, key, tryNonUnix=False): | |
|
cduvall
2013/03/21 18:43:53
parameter names are unix_hacker_style
method names
epeterson
2013/03/25 19:35:11
Done.
| |
| 507 """Like get(self, key), only doesn't generate and return HandlebarContext. | |
| 508 Used for checking existence of an api: cache will raise a FileNotFoundError | |
| 509 if an api cannot be located in the cache. | |
| 510 """ | |
| 511 camel_name, unix_name = self._DetermineNamesForGet(key) | |
| 512 cache, ext = self._DetermineCacheForGet(unix_name) | |
| 513 try: | |
| 514 if tryNonUnix is True: | |
| 515 return cache.GetFromFile('%s/%s%s' % (self._base_path, camel_name, ext)) | |
| 516 return cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)) | |
| 517 except ParseException: | |
| 518 #Some api's in older versions trip up the JSC (i.e. v.18 tabs.json lacks | |
| 519 #a "type" attribute for its "url" property) | |
| 520 logging.error("Caught Parse Exception") | |
| 521 return {} | |
| OLD | NEW |