Index: chrome/common/extensions/docs/server2/api_data_source.py |
diff --git a/chrome/common/extensions/docs/server2/api_data_source.py b/chrome/common/extensions/docs/server2/api_data_source.py |
index 08369f0e68c35be11ef55dea02988c57c2172740..7ae36739e7e9a95a458e523fc75f573ad7e049e9 100644 |
--- a/chrome/common/extensions/docs/server2/api_data_source.py |
+++ b/chrome/common/extensions/docs/server2/api_data_source.py |
@@ -12,12 +12,13 @@ import third_party.json_schema_compiler.json_parse as json_parse |
import third_party.json_schema_compiler.model as model |
import third_party.json_schema_compiler.idl_schema as idl_schema |
import third_party.json_schema_compiler.idl_parser as idl_parser |
+from third_party.json_schema_compiler.model import ParseException |
# Increment this version when there are changes to the data stored in any of |
# the caches used by APIDataSource. This would include changes to model.py in |
# JSON schema compiler! This allows the cache to be invalidated without having |
# to flush memcache on the production server. |
-_VERSION = 15 |
+_VERSION = 16 |
def _RemoveNoDocs(item): |
if json_parse.IsDict(item): |
@@ -53,9 +54,14 @@ class _JSCModel(object): |
"""Uses a Model from the JSON Schema Compiler and generates a dict that |
a Handlebar template can use for a data source. |
""" |
- def __init__(self, json, ref_resolver, disable_refs): |
+ def __init__(self, |
+ json, |
+ ref_resolver, |
+ disable_refs, |
+ availability_data_source=None): |
self._ref_resolver = ref_resolver |
self._disable_refs = disable_refs |
+ self._availability_data_source = availability_data_source |
clean_json = copy.deepcopy(json) |
if _RemoveNoDocs(clean_json): |
self._namespace = None |
@@ -77,14 +83,35 @@ class _JSCModel(object): |
def ToDict(self): |
if self._namespace is None: |
return {} |
+ availability = self._namespace.availability |
+ availability_generated = False |
+ if availability is None: |
+ availability = self._GetAvailability() |
+ if availability is not None: |
+ # 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.
|
+ availability_generated = True |
+ else: |
+ # 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,
|
+ 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.
|
return { |
'name': self._namespace.name, |
+ 'description': self._namespace.description, |
+ 'availability': availability, |
+ 'availability_generated': availability_generated, |
'types': self._GenerateTypes(self._namespace.types.values()), |
'functions': self._GenerateFunctions(self._namespace.functions), |
'events': self._GenerateEvents(self._namespace.events), |
'properties': self._GenerateProperties(self._namespace.properties) |
} |
+ def _GetAvailability(self): |
+ """Returns the earliest version of chrome, represented by a version number, |
+ that this api was available in |
+ """ |
+ if self._availability_data_source is not None: |
+ return self._availability_data_source.FindEarliestAvailability( |
+ self._namespace.name) |
+ |
def _GenerateTypes(self, types): |
return [self._GenerateType(t) for t in types] |
@@ -272,7 +299,8 @@ class APIDataSource(object): |
class Factory(object): |
def __init__(self, |
cache_factory, |
- base_path): |
+ base_path, |
+ availability_data_source=None): |
self._permissions_cache = cache_factory.Create(self._LoadPermissions, |
compiled_fs.PERMS, |
version=_VERSION) |
@@ -303,7 +331,7 @@ class APIDataSource(object): |
compiled_fs.NAMES, |
version=_VERSION) |
self._base_path = base_path |
- |
+ self._availability_data_source = availability_data_source |
# These must be set later via the SetFooDataSourceFactory methods. |
self._ref_resolver_factory = None |
self._samples_data_source_factory = None |
@@ -349,14 +377,16 @@ class APIDataSource(object): |
return _JSCModel( |
json_parse.Parse(api)[0], |
self._ref_resolver_factory.Create() if not disable_refs else None, |
- disable_refs).ToDict() |
+ disable_refs, |
+ self._availability_data_source).ToDict() |
def _LoadIdlAPI(self, api, disable_refs): |
idl = idl_parser.IDLParser().ParseData(api) |
return _JSCModel( |
idl_schema.IDLSchema(idl).process()[0], |
self._ref_resolver_factory.Create() if not disable_refs else None, |
- disable_refs).ToDict() |
+ disable_refs, |
+ self._availability_data_source).ToDict() |
def _GetIDLNames(self, base_dir, apis): |
return [ |
@@ -442,17 +472,19 @@ class APIDataSource(object): |
return '/'.join(parts) |
return name.replace('_', '/', 1) |
- def get(self, key): |
+ def _DetermineNamesForGet(self, key): |
if key.endswith('.html') or key.endswith('.json') or key.endswith('.idl'): |
path, ext = os.path.splitext(key) |
else: |
path = key |
unix_name = model.UnixName(path) |
- idl_names = self._idl_names_cache.GetFromFileListing(self._base_path) |
names = self._names_cache.GetFromFileListing(self._base_path) |
if unix_name not in names and self._GetAsSubdirectory(unix_name) in names: |
unix_name = self._GetAsSubdirectory(unix_name) |
+ return (path, unix_name) |
+ def _DetermineCacheForGet(self, unix_name): |
+ idl_names = self._idl_names_cache.GetFromFileListing(self._base_path) |
if self._disable_refs: |
cache, ext = ( |
(self._idl_cache_no_refs, '.idl') if (unix_name in idl_names) else |
@@ -460,6 +492,28 @@ class APIDataSource(object): |
else: |
cache, ext = ((self._idl_cache, '.idl') if (unix_name in idl_names) else |
(self._json_cache, '.json')) |
+ return (cache, ext) |
+ |
+ def get(self, key): |
+ path, unix_name = self._DetermineNamesForGet(key) |
+ cache, ext = self._DetermineCacheForGet(unix_name) |
return self._GenerateHandlebarContext( |
cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)), |
path) |
+ |
+ def tryGet(self, key, try_non_unix=False): |
+ """Like get(self, key), only doesn't generate and return HandlebarContext. |
+ Used for checking existence of an api: cache will raise a FileNotFoundError |
+ if an api cannot be located in the cache. |
+ """ |
+ camel_name, unix_name = self._DetermineNamesForGet(key) |
+ cache, ext = self._DetermineCacheForGet(unix_name) |
+ try: |
+ if try_non_unix is True: |
+ return cache.GetFromFile('%s/%s%s' % (self._base_path, camel_name, ext)) |
+ return cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)) |
+ except ParseException: |
+ #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.
|
+ #a "type" attribute for its "url" property) |
+ logging.error("Caught Parse Exception") |
+ return {} |