Chromium Code Reviews| 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..501563088ddf1ca18839edf2e3065990283a5a76 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 |
| @@ -74,17 +80,47 @@ class _JSCModel(object): |
| return { 'href': '#type-%s' % type_name, 'text': link, 'name': link } |
| return self._ref_resolver.SafeGetLink(link, namespace=self._namespace.name) |
| + |
|
cduvall
2013/03/27 23:01:10
delete extra newline
epeterson
2013/03/28 00:53:50
Done.
|
| def ToDict(self): |
| if self._namespace is None: |
| return {} |
| + availability = self._HandleAvailability() |
| 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. |
|
cduvall
2013/03/27 23:01:10
line this up with the """:
"""...
that this..
epeterson
2013/03/28 00:53:50
Done.
|
| + """ |
| + if self._availability_data_source is not None: |
| + 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. |
|
cduvall
2013/03/27 23:01:10
same
epeterson
2013/03/28 00:53:50
Done.
|
| + """ |
| + 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 |
| + |
| def _GenerateTypes(self, types): |
| return [self._GenerateType(t) for t in types] |
| @@ -272,7 +308,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 +340,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 +386,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 +481,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 +501,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): |
|
cduvall
2013/03/27 23:01:10
Rename to TryGet
epeterson
2013/03/28 00:53:50
Done.
|
| + """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 APIs in older versions trip up the JSC (i.e. v.18 tabs.json lacks |
| + # a "type" attribute for its "url" property) |
| + 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.
|
| + return {} |