Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9587)

Unified Diff: chrome/common/extensions/docs/server2/api_models.py

Issue 46243003: Docserver: Add an APIModels class which tracks both the list of available APIs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: couple more tests Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/common/extensions/docs/server2/api_models_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/common/extensions/docs/server2/api_models.py
diff --git a/chrome/common/extensions/docs/server2/api_models.py b/chrome/common/extensions/docs/server2/api_models.py
new file mode 100644
index 0000000000000000000000000000000000000000..c88bb8187dc1c21c254f92deb5f10f2da2d6913a
--- /dev/null
+++ b/chrome/common/extensions/docs/server2/api_models.py
@@ -0,0 +1,63 @@
+# Copyright 2013 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import logging
+import os
+import posixpath
+
+from file_system import FileNotFoundError
+from future import Gettable, Future
+from schema_util import ProcessSchema
+from svn_constants import API_PATH
+from third_party.json_schema_compiler.model import Namespace, UnixName
+
+
+def _CreateAPIModel(path, data):
+ schema = ProcessSchema(path, data)
+ if os.path.splitext(path)[1] == '.json':
+ schema = schema[0]
+ return Namespace(schema, schema['namespace'])
+
+
+class APIModels(object):
+ '''Tracks APIs and their Models.
+ '''
+
+ def __init__(self, features_bundle, compiled_fs_factory, file_system):
+ self._features_bundle = features_bundle
+ self._model_cache = compiled_fs_factory.Create(
+ file_system, _CreateAPIModel, APIModels)
+
+ def GetNames(self):
+ return self._features_bundle.GetAPIFeatures().keys()
+
+ def GetModel(self, api_name):
+ # Callers sometimes specify a full path (.json or .idl) themselves. If so,
Yoyo Zhou 2013/10/30 23:22:19 This isn't always a full path, sometimes just the
not at google - send to devlin 2013/10/30 23:50:05 Done.
+ # believe them.
+ if os.path.splitext(api_name)[1] in ('.json', '.idl'):
+ if not api_name.startswith(API_PATH + '/'):
+ api_name = posixpath.join(API_PATH, api_name)
+ return self._model_cache.GetFromFile(api_name)
+
+ assert not api_name.startswith(API_PATH)
+
+ # API names are given as declarativeContent and app.window but file names
+ # will be declarative_content and app_window.
+ file_name = UnixName(api_name).replace('.', '_')
+ # Devtools APIs are in API_PATH/devtools/ not API_PATH/, and have their
+ # "devtools" names removed from the file names.
+ if 'devtools_' in file_name:
Yoyo Zhou 2013/10/30 23:22:19 perhaps os.path.basename(file_name).startswith('de
not at google - send to devlin 2013/10/30 23:50:05 Done.
+ file_name = posixpath.join('devtools', file_name.replace('devtools_', ''))
+
+ futures = [self._model_cache.GetFromFile('%s/%s.%s' %
+ (API_PATH, file_name, ext))
+ for ext in ('json', 'idl')]
+ def resolve():
+ for future in futures:
+ try:
+ return future.Get()
+ except FileNotFoundError: pass
+ # Propagate the first FileNotFoundError.
Yoyo Zhou 2013/10/30 23:22:19 ...if both were filenotfound
not at google - send to devlin 2013/10/30 23:50:05 Done.
+ futures[0].Get()
+ return Future(delegate=Gettable(resolve))
« no previous file with comments | « no previous file | chrome/common/extensions/docs/server2/api_models_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698