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 9695fd6dcebed8a20b8653c536f31dafa4272e1b..7c7c5ad445e258d9c1f5d6d60da13cf24cc4f6bd 100644 |
| --- a/chrome/common/extensions/docs/server2/api_data_source.py |
| +++ b/chrome/common/extensions/docs/server2/api_data_source.py |
| @@ -3,10 +3,12 @@ |
| # found in the LICENSE file. |
| import copy |
| +import json as json_library |
| import logging |
| import os |
| import collections |
| +import svn_constants |
| 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 |
| @@ -80,9 +82,18 @@ 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, |
| + svn_file_system): |
| self._ref_resolver = ref_resolver |
| self._disable_refs = disable_refs |
| + self._availability_data_source = availability_data_source |
| + json_string = svn_file_system.ReadSingle('%s/intro_tables.json' |
|
not at google - send to devlin
2013/04/30 18:34:19
nicer line break would be
svn_file_system.ReadSing
epeterson
2013/05/13 02:38:10
Done.
|
| + % svn_constants.JSON_PATH) |
| + self._intro_json = json_library.loads(json_string) |
| clean_json = copy.deepcopy(json) |
| if _RemoveNoDocs(clean_json): |
| self._namespace = None |
| @@ -107,12 +118,56 @@ class _JSCModel(object): |
| return {} |
| return { |
| 'name': self._namespace.name, |
| + 'description': self._namespace.description, |
| + 'availability': self._GetAvailability(), |
| + 'experimental': self._CheckExperimental(), |
| + 'intro_permissions': self._GetPermissions(), |
| + 'learn_more': self._GetMoreLearning(), |
| '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): |
| + """Finds the earliest stable version of chrome where an API was available |
| + using AvailabilityDataSource. If an API hasn't been released to stable yet, |
| + then the development channel that the API is currently available on will |
| + be returned instead. |
| + """ |
| + if not self._CheckExperimental(): |
| + return self._availability_data_source.GetAvailability( |
| + self._namespace.name) |
| + # A special message for experimental APIs' availability will be displayed. |
| + return None |
| + |
| + def _CheckExperimental(self): |
| + return self._namespace.name.startswith('experimental') |
| + |
| + def _GetPermissions(self): |
| + """This is a temporary fix for generating API intro tables automatically. |
| + Information on an API's permissions is pulled from a json file so that it |
| + can be sent to the templates along with other relevant intro data. |
| + """ |
| + table_info = self._intro_json.get(self._namespace.name, None) |
| + try: |
| + return table_info[0]['Permissions'] |
| + except: |
| + # The intro-table will display a "Permissions: None" message. |
| + return None |
|
not at google - send to devlin
2013/04/30 18:34:19
pls don't use exceptions for control flow. Also as
epeterson
2013/05/13 02:38:10
Done.
|
| + |
| + def _GetMoreLearning(self): |
| + """We can't automatically generated the "Learn more" field in an API's. |
| + intro table; instead, we pull it from a json file so that it can be sent to |
| + the templates along with other relevant intro data. |
| + """ |
| + table_info = self._intro_json.get(self._namespace.name, None) |
| + try: |
| + return table_info[1]['Learn more'] |
| + except: |
| + # Not all APIs have "Learn more" information. |
| + return None |
|
not at google - send to devlin
2013/04/30 18:34:19
ditto regarding the filtering, not assuming it's t
epeterson
2013/05/13 02:38:10
Done.
|
| + |
| def _GenerateTypes(self, types): |
| return [self._GenerateType(t) for t in types] |
| @@ -299,7 +354,11 @@ class APIDataSource(object): |
| |compiled_fs_factory|, so the APIs can be plugged into templates. |
| """ |
| class Factory(object): |
| - def __init__(self, compiled_fs_factory, base_path): |
| + def __init__(self, |
| + compiled_fs_factory, |
| + base_path, |
| + availability_data_source_factory, |
| + svn_file_system): |
| def create_compiled_fs(fn, category): |
| return compiled_fs_factory.Create(fn, APIDataSource, category=category) |
| @@ -327,7 +386,8 @@ class APIDataSource(object): |
| self._names_cache = create_compiled_fs(self._GetAllNames, 'names') |
| self._base_path = base_path |
| - |
| + self._availability_data_source = availability_data_source_factory.Create() |
| + self._svn_file_system = svn_file_system |
| # These must be set later via the SetFooDataSourceFactory methods. |
| self._ref_resolver_factory = None |
| self._samples_data_source_factory = None |
| @@ -373,14 +433,18 @@ 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, |
| + self._svn_file_system).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, |
| + self._svn_file_system).ToDict() |
| def _GetIDLNames(self, base_dir, apis): |
| return self._GetExtNames(apis, ['idl']) |
| @@ -457,21 +521,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 |
| @@ -479,6 +548,11 @@ 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) |