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..84b959066456b8a1f59d435716722eada471db44 100644 |
--- a/chrome/common/extensions/docs/server2/api_data_source.py |
+++ b/chrome/common/extensions/docs/server2/api_data_source.py |
@@ -17,7 +17,7 @@ import third_party.json_schema_compiler.idl_parser as idl_parser |
# 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 +53,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 +82,43 @@ class _JSCModel(object): |
def ToDict(self): |
if self._namespace is None: |
return {} |
+ availability = self._HandleAvailability() |
not at google - send to devlin
2013/04/02 10:29:39
inline?
epeterson
2013/04/03 01:21:43
Done.
|
return { |
'name': self._namespace.name, |
+ 'description': self._namespace.description, |
+ 'availability': availability, |
'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: |
not at google - send to devlin
2013/04/02 10:29:39
when is there no availability data source?
epeterson
2013/04/03 01:21:43
Each channel instance has an APIDataSourceFactory
|
+ return self._availability_data_source.FindEarliestAvailability( |
+ self._namespace.name) |
+ |
+ def _HandleAvailability(self): |
+ """Finds the availability for an api, and creates a string describing |
+ the availability depending on its original format. |
+ """ |
+ availability = self._namespace.availability |
+ availability_string = 'Not currently available' |
+ # Check to see if the .json/.idl already contains an availability field. |
+ if availability is None: |
+ availability = self._GetAvailability() |
+ |
+ if availability is not None: |
+ if availability.isdigit(): |
+ availability_string = 'Google Chrome %s' % availability |
+ else: |
+ availability_string = ('Google Chrome %s Channel Only' % |
+ availability.capitalize()) |
+ return availability_string |
not at google - send to devlin
2013/04/02 10:29:39
All of this string logic needs to be embedded in t
not at google - send to devlin
2013/04/02 21:58:16
Ok - so I think it's fine to submit the server cha
|
+ |
def _GenerateTypes(self, types): |
return [self._GenerateType(t) for t in types] |
@@ -272,7 +306,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 +338,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 +384,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 [ |
@@ -438,21 +475,26 @@ class APIDataSource(object): |
def _GetAsSubdirectory(self, name): |
if name.startswith('experimental_'): |
parts = name[len('experimental_'):].split('_', 1) |
- parts[1] = 'experimental_%s' % parts[1] |
- return '/'.join(parts) |
+ if len(parts) > 1: |
+ parts[1] = 'experimental_%s' % parts[1] |
+ return '/'.join(parts) |
+ else: |
+ return '%s/%s' % (parts[0], name) |
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 +502,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): |
not at google - send to devlin
2013/04/02 10:29:39
Note to future self: unclear what this should be r
epeterson
2013/04/03 03:59:57
You're right, it doesn't make sense to return an u
|
+ """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 model.ParseException as e: |
+ # Some APIs in older versions trip up the JSC (i.e. v.18 tabs.json lacks |
+ # a "type" attribute for its "url" property) |
+ logging.warning('Caught parse exception for %s: %s' % (unix_name, e)) |
+ return {} |