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