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

Side by Side 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: yoz Created 7 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/common/extensions/docs/server2/api_models_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import logging
6 import os
7 import posixpath
8
9 from file_system import FileNotFoundError
10 from future import Gettable, Future
11 from schema_util import ProcessSchema
12 from svn_constants import API_PATH
13 from third_party.json_schema_compiler.model import Namespace, UnixName
14
15
16 def _CreateAPIModel(path, data):
17 schema = ProcessSchema(path, data)
18 if os.path.splitext(path)[1] == '.json':
19 schema = schema[0]
20 return Namespace(schema, schema['namespace'])
21
22
23 class APIModels(object):
24 '''Tracks APIs and their Models.
25 '''
26
27 def __init__(self, features_bundle, compiled_fs_factory, file_system):
28 self._features_bundle = features_bundle
29 self._model_cache = compiled_fs_factory.Create(
30 file_system, _CreateAPIModel, APIModels)
31
32 def GetNames(self):
33 return self._features_bundle.GetAPIFeatures().keys()
34
35 def GetModel(self, api_name):
36 # Callers sometimes specify a filename which includes .json or .idl - if
37 # so, believe them. They may even include the 'api/' prefix.
38 if os.path.splitext(api_name)[1] in ('.json', '.idl'):
39 if not api_name.startswith(API_PATH + '/'):
40 api_name = posixpath.join(API_PATH, api_name)
41 return self._model_cache.GetFromFile(api_name)
42
43 assert not api_name.startswith(API_PATH)
44
45 # API names are given as declarativeContent and app.window but file names
46 # will be declarative_content and app_window.
47 file_name = UnixName(api_name).replace('.', '_')
48 # Devtools APIs are in API_PATH/devtools/ not API_PATH/, and have their
49 # "devtools" names removed from the file names.
50 basename = posixpath.basename(file_name)
51 if basename.startswith('devtools_'):
52 file_name = posixpath.join(
53 'devtools', file_name.replace(basename, basename[len('devtools_'):]))
54
55 futures = [self._model_cache.GetFromFile('%s/%s.%s' %
56 (API_PATH, file_name, ext))
57 for ext in ('json', 'idl')]
58 def resolve():
59 for future in futures:
60 try:
61 return future.Get()
62 except FileNotFoundError: pass
63 # Propagate the first FileNotFoundError if neither were found.
64 futures[0].Get()
65 return Future(delegate=Gettable(resolve))
OLDNEW
« 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