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 |
83 | |
cduvall
2013/03/27 23:01:10
delete extra newline
epeterson
2013/03/28 00:53:50
Done.
| |
77 def ToDict(self): | 84 def ToDict(self): |
78 if self._namespace is None: | 85 if self._namespace is None: |
79 return {} | 86 return {} |
87 availability = self._HandleAvailability() | |
80 return { | 88 return { |
81 'name': self._namespace.name, | 89 'name': self._namespace.name, |
90 'description': self._namespace.description, | |
91 'availability': availability, | |
82 'types': self._GenerateTypes(self._namespace.types.values()), | 92 'types': self._GenerateTypes(self._namespace.types.values()), |
83 'functions': self._GenerateFunctions(self._namespace.functions), | 93 'functions': self._GenerateFunctions(self._namespace.functions), |
84 'events': self._GenerateEvents(self._namespace.events), | 94 'events': self._GenerateEvents(self._namespace.events), |
85 'properties': self._GenerateProperties(self._namespace.properties) | 95 'properties': self._GenerateProperties(self._namespace.properties) |
86 } | 96 } |
87 | 97 |
98 def _GetAvailability(self): | |
99 """Returns the earliest version of chrome, represented by a version number, | |
100 that this api was available in. | |
cduvall
2013/03/27 23:01:10
line this up with the """:
"""...
that this..
epeterson
2013/03/28 00:53:50
Done.
| |
101 """ | |
102 if self._availability_data_source is not None: | |
103 return self._availability_data_source.FindEarliestAvailability( | |
104 self._namespace.name) | |
105 | |
106 def _HandleAvailability(self): | |
107 """Finds the availability for an api, and creates a string describing | |
108 the availability depending on its original format. | |
cduvall
2013/03/27 23:01:10
same
epeterson
2013/03/28 00:53:50
Done.
| |
109 """ | |
110 availability = self._namespace.availability | |
111 availability_string = 'Not currently available' | |
112 # Check to see if the .json/.idl already contains an availability field. | |
113 if availability is None: | |
114 availability = self._GetAvailability() | |
115 | |
116 if availability is not None: | |
117 if availability.isdigit(): | |
118 availability_string = ('Google Chrome %s' % availability) | |
119 else: | |
120 availability_string = ('Google Chrome %s Channel Only' % | |
121 availability.capitalize()) | |
122 return availability_string | |
123 | |
88 def _GenerateTypes(self, types): | 124 def _GenerateTypes(self, types): |
89 return [self._GenerateType(t) for t in types] | 125 return [self._GenerateType(t) for t in types] |
90 | 126 |
91 def _GenerateType(self, type_): | 127 def _GenerateType(self, type_): |
92 type_dict = { | 128 type_dict = { |
93 'name': type_.simple_name, | 129 'name': type_.simple_name, |
94 'description': self._FormatDescription(type_.description), | 130 'description': self._FormatDescription(type_.description), |
95 'properties': self._GenerateProperties(type_.properties), | 131 'properties': self._GenerateProperties(type_.properties), |
96 'functions': self._GenerateFunctions(type_.functions), | 132 'functions': self._GenerateFunctions(type_.functions), |
97 'events': self._GenerateEvents(type_.events), | 133 'events': self._GenerateEvents(type_.events), |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 def get(self, key): | 301 def get(self, key): |
266 return self._samples.FilterSamples(key, self._api_name) | 302 return self._samples.FilterSamples(key, self._api_name) |
267 | 303 |
268 class APIDataSource(object): | 304 class APIDataSource(object): |
269 """This class fetches and loads JSON APIs from the FileSystem passed in with | 305 """This class fetches and loads JSON APIs from the FileSystem passed in with |
270 |cache_factory|, so the APIs can be plugged into templates. | 306 |cache_factory|, so the APIs can be plugged into templates. |
271 """ | 307 """ |
272 class Factory(object): | 308 class Factory(object): |
273 def __init__(self, | 309 def __init__(self, |
274 cache_factory, | 310 cache_factory, |
275 base_path): | 311 base_path, |
312 availability_data_source=None): | |
276 self._permissions_cache = cache_factory.Create(self._LoadPermissions, | 313 self._permissions_cache = cache_factory.Create(self._LoadPermissions, |
277 compiled_fs.PERMS, | 314 compiled_fs.PERMS, |
278 version=_VERSION) | 315 version=_VERSION) |
279 self._json_cache = cache_factory.Create( | 316 self._json_cache = cache_factory.Create( |
280 lambda api_name, api: self._LoadJsonAPI(api, False), | 317 lambda api_name, api: self._LoadJsonAPI(api, False), |
281 compiled_fs.JSON, | 318 compiled_fs.JSON, |
282 version=_VERSION) | 319 version=_VERSION) |
283 self._idl_cache = cache_factory.Create( | 320 self._idl_cache = cache_factory.Create( |
284 lambda api_name, api: self._LoadIdlAPI(api, False), | 321 lambda api_name, api: self._LoadIdlAPI(api, False), |
285 compiled_fs.IDL, | 322 compiled_fs.IDL, |
(...skipping 10 matching lines...) Expand all Loading... | |
296 lambda api_name, api: self._LoadIdlAPI(api, True), | 333 lambda api_name, api: self._LoadIdlAPI(api, True), |
297 compiled_fs.IDL_NO_REFS, | 334 compiled_fs.IDL_NO_REFS, |
298 version=_VERSION) | 335 version=_VERSION) |
299 self._idl_names_cache = cache_factory.Create(self._GetIDLNames, | 336 self._idl_names_cache = cache_factory.Create(self._GetIDLNames, |
300 compiled_fs.IDL_NAMES, | 337 compiled_fs.IDL_NAMES, |
301 version=_VERSION) | 338 version=_VERSION) |
302 self._names_cache = cache_factory.Create(self._GetAllNames, | 339 self._names_cache = cache_factory.Create(self._GetAllNames, |
303 compiled_fs.NAMES, | 340 compiled_fs.NAMES, |
304 version=_VERSION) | 341 version=_VERSION) |
305 self._base_path = base_path | 342 self._base_path = base_path |
306 | 343 self._availability_data_source = availability_data_source |
307 # These must be set later via the SetFooDataSourceFactory methods. | 344 # These must be set later via the SetFooDataSourceFactory methods. |
308 self._ref_resolver_factory = None | 345 self._ref_resolver_factory = None |
309 self._samples_data_source_factory = None | 346 self._samples_data_source_factory = None |
310 | 347 |
311 def SetSamplesDataSourceFactory(self, samples_data_source_factory): | 348 def SetSamplesDataSourceFactory(self, samples_data_source_factory): |
312 self._samples_data_source_factory = samples_data_source_factory | 349 self._samples_data_source_factory = samples_data_source_factory |
313 | 350 |
314 def SetReferenceResolverFactory(self, ref_resolver_factory): | 351 def SetReferenceResolverFactory(self, ref_resolver_factory): |
315 self._ref_resolver_factory = ref_resolver_factory | 352 self._ref_resolver_factory = ref_resolver_factory |
316 | 353 |
(...skipping 25 matching lines...) Expand all Loading... | |
342 samples, | 379 samples, |
343 disable_refs) | 380 disable_refs) |
344 | 381 |
345 def _LoadPermissions(self, file_name, json_str): | 382 def _LoadPermissions(self, file_name, json_str): |
346 return json_parse.Parse(json_str) | 383 return json_parse.Parse(json_str) |
347 | 384 |
348 def _LoadJsonAPI(self, api, disable_refs): | 385 def _LoadJsonAPI(self, api, disable_refs): |
349 return _JSCModel( | 386 return _JSCModel( |
350 json_parse.Parse(api)[0], | 387 json_parse.Parse(api)[0], |
351 self._ref_resolver_factory.Create() if not disable_refs else None, | 388 self._ref_resolver_factory.Create() if not disable_refs else None, |
352 disable_refs).ToDict() | 389 disable_refs, |
390 self._availability_data_source).ToDict() | |
353 | 391 |
354 def _LoadIdlAPI(self, api, disable_refs): | 392 def _LoadIdlAPI(self, api, disable_refs): |
355 idl = idl_parser.IDLParser().ParseData(api) | 393 idl = idl_parser.IDLParser().ParseData(api) |
356 return _JSCModel( | 394 return _JSCModel( |
357 idl_schema.IDLSchema(idl).process()[0], | 395 idl_schema.IDLSchema(idl).process()[0], |
358 self._ref_resolver_factory.Create() if not disable_refs else None, | 396 self._ref_resolver_factory.Create() if not disable_refs else None, |
359 disable_refs).ToDict() | 397 disable_refs, |
398 self._availability_data_source).ToDict() | |
360 | 399 |
361 def _GetIDLNames(self, base_dir, apis): | 400 def _GetIDLNames(self, base_dir, apis): |
362 return [ | 401 return [ |
363 model.UnixName(os.path.splitext(api[len('%s/' % self._base_path):])[0]) | 402 model.UnixName(os.path.splitext(api[len('%s/' % self._base_path):])[0]) |
364 for api in apis if api.endswith('.idl') | 403 for api in apis if api.endswith('.idl') |
365 ] | 404 ] |
366 | 405 |
367 def _GetAllNames(self, base_dir, apis): | 406 def _GetAllNames(self, base_dir, apis): |
368 return [ | 407 return [ |
369 model.UnixName(os.path.splitext(api[len('%s/' % self._base_path):])[0]) | 408 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) | 474 handlebar_dict['samples'] = _LazySamplesGetter(path, self._samples) |
436 return handlebar_dict | 475 return handlebar_dict |
437 | 476 |
438 def _GetAsSubdirectory(self, name): | 477 def _GetAsSubdirectory(self, name): |
439 if name.startswith('experimental_'): | 478 if name.startswith('experimental_'): |
440 parts = name[len('experimental_'):].split('_', 1) | 479 parts = name[len('experimental_'):].split('_', 1) |
441 parts[1] = 'experimental_%s' % parts[1] | 480 parts[1] = 'experimental_%s' % parts[1] |
442 return '/'.join(parts) | 481 return '/'.join(parts) |
443 return name.replace('_', '/', 1) | 482 return name.replace('_', '/', 1) |
444 | 483 |
445 def get(self, key): | 484 def _DetermineNamesForGet(self, key): |
446 if key.endswith('.html') or key.endswith('.json') or key.endswith('.idl'): | 485 if key.endswith('.html') or key.endswith('.json') or key.endswith('.idl'): |
447 path, ext = os.path.splitext(key) | 486 path, ext = os.path.splitext(key) |
448 else: | 487 else: |
449 path = key | 488 path = key |
450 unix_name = model.UnixName(path) | 489 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) | 490 names = self._names_cache.GetFromFileListing(self._base_path) |
453 if unix_name not in names and self._GetAsSubdirectory(unix_name) in names: | 491 if unix_name not in names and self._GetAsSubdirectory(unix_name) in names: |
454 unix_name = self._GetAsSubdirectory(unix_name) | 492 unix_name = self._GetAsSubdirectory(unix_name) |
493 return (path, unix_name) | |
455 | 494 |
495 def _DetermineCacheForGet(self, unix_name): | |
496 idl_names = self._idl_names_cache.GetFromFileListing(self._base_path) | |
456 if self._disable_refs: | 497 if self._disable_refs: |
457 cache, ext = ( | 498 cache, ext = ( |
458 (self._idl_cache_no_refs, '.idl') if (unix_name in idl_names) else | 499 (self._idl_cache_no_refs, '.idl') if (unix_name in idl_names) else |
459 (self._json_cache_no_refs, '.json')) | 500 (self._json_cache_no_refs, '.json')) |
460 else: | 501 else: |
461 cache, ext = ((self._idl_cache, '.idl') if (unix_name in idl_names) else | 502 cache, ext = ((self._idl_cache, '.idl') if (unix_name in idl_names) else |
462 (self._json_cache, '.json')) | 503 (self._json_cache, '.json')) |
504 return (cache, ext) | |
505 | |
506 def get(self, key): | |
507 path, unix_name = self._DetermineNamesForGet(key) | |
508 cache, ext = self._DetermineCacheForGet(unix_name) | |
463 return self._GenerateHandlebarContext( | 509 return self._GenerateHandlebarContext( |
464 cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)), | 510 cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)), |
465 path) | 511 path) |
512 | |
513 def tryGet(self, key, try_non_unix=False): | |
cduvall
2013/03/27 23:01:10
Rename to TryGet
epeterson
2013/03/28 00:53:50
Done.
| |
514 """Like get(self, key), only doesn't generate and return HandlebarContext. | |
515 Used for checking existence of an api: cache will raise a FileNotFoundError | |
516 if an api cannot be located in the cache. | |
517 """ | |
518 camel_name, unix_name = self._DetermineNamesForGet(key) | |
519 cache, ext = self._DetermineCacheForGet(unix_name) | |
520 try: | |
521 if try_non_unix is True: | |
522 return cache.GetFromFile('%s/%s%s' % (self._base_path, camel_name, ext)) | |
523 return cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)) | |
524 except ParseException: | |
525 # Some APIs in older versions trip up the JSC (i.e. v.18 tabs.json lacks | |
526 # a "type" attribute for its "url" property) | |
527 logging.error("Caught Parse Exception") | |
cduvall
2013/03/27 23:01:10
Make this error say something like:
'Caught parse
epeterson
2013/03/28 00:53:50
Done.
| |
528 return {} | |
OLD | NEW |