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

Side by Side Diff: chrome/common/extensions/docs/server2/api_models.py

Issue 176973009: Doc server: support interfaces in src/extensions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rename path constants, update APIModels.GetModel Created 6 years, 9 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 # Copyright 2013 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import os 5 import os
6 import posixpath 6 import posixpath
7 7
8 from compiled_file_system import SingleFile, Unicode 8 from compiled_file_system import SingleFile, Unicode
9 from extensions_paths import API 9 from extensions_paths import API, CHROME_API
10 from file_system import FileNotFoundError 10 from file_system import FileNotFoundError
11 from future import Gettable, Future 11 from future import Gettable, Future
12 from schema_util import ProcessSchema 12 from schema_util import ProcessSchema
13 from third_party.json_schema_compiler.model import Namespace, UnixName 13 from third_party.json_schema_compiler.model import Namespace, UnixName
14 14
15 15
16 @SingleFile 16 @SingleFile
17 @Unicode 17 @Unicode
18 def _CreateAPIModel(path, data): 18 def _CreateAPIModel(path, data):
19 schema = ProcessSchema(path, data)[0] 19 schema = ProcessSchema(path, data)[0]
20 if not schema: return None 20 if not schema: return None
21 return Namespace(schema, schema['namespace']) 21 return Namespace(schema, schema['namespace'])
22 22
23
not at google - send to devlin 2014/03/07 01:04:52 this is actually python style [guide] apparently
Ken Rockot(use gerrit already) 2014/03/07 07:07:09 Done.
24 class APIModels(object): 23 class APIModels(object):
25 '''Tracks APIs and their Models. 24 '''Tracks APIs and their Models.
26 ''' 25 '''
27 26
28 def __init__(self, features_bundle, compiled_fs_factory, file_system): 27 def __init__(self, features_bundle, compiled_fs_factory, file_system):
29 self._features_bundle = features_bundle 28 self._features_bundle = features_bundle
30 self._model_cache = compiled_fs_factory.Create( 29 self._model_cache = compiled_fs_factory.Create(
31 file_system, _CreateAPIModel, APIModels) 30 file_system, _CreateAPIModel, APIModels)
32 31
33 def GetNames(self): 32 def GetNames(self):
34 # API names appear alongside some of their methods/events/etc in the 33 # API names appear alongside some of their methods/events/etc in the
35 # features file. APIs are those which either implicitly or explicitly have 34 # features file. APIs are those which either implicitly or explicitly have
36 # no parent feature (e.g. app, app.window, and devtools.inspectedWindow are 35 # no parent feature (e.g. app, app.window, and devtools.inspectedWindow are
37 # APIs; runtime.onConnectNative is not). 36 # APIs; runtime.onConnectNative is not).
38 api_features = self._features_bundle.GetAPIFeatures().Get() 37 api_features = self._features_bundle.GetAPIFeatures().Get()
39 return [name for name, feature in api_features.iteritems() 38 return [name for name, feature in api_features.iteritems()
40 if ('.' not in name or 39 if ('.' not in name or
41 name.rsplit('.', 1)[0] not in api_features or 40 name.rsplit('.', 1)[0] not in api_features or
42 feature.get('noparent'))] 41 feature.get('noparent'))]
43 42
44 def GetModel(self, api_name): 43 def GetModel(self, api_name):
45 # Callers sometimes specify a filename which includes .json or .idl - if 44 # Callers sometimes specify a filename which includes .json or .idl - if
46 # so, believe them. They may even include the 'api/' prefix. 45 # so, believe them. They may even include the 'api/' prefix.
47 if os.path.splitext(api_name)[1] in ('.json', '.idl'): 46 if os.path.splitext(api_name)[1] in ('.json', '.idl'):
48 if not api_name.startswith(API): 47 if not api_name.startswith(API) and not api_name.startswith(CHROME_API):
not at google - send to devlin 2014/03/07 01:04:52 if not api_name.startswith((API, CHROME_API)):
Ken Rockot(use gerrit already) 2014/03/07 07:07:09 Done.
49 api_name = posixpath.join(API, api_name) 48 api_path = posixpath.join(API, api_name)
49 if self._model_cache.FileExists(api_path).Get():
not at google - send to devlin 2014/03/07 01:04:52 rather than adding a new method here, hold onto |f
Ken Rockot(use gerrit already) 2014/03/07 07:07:09 Done.
50 return self._model_cache.GetFromFile(api_path)
51 api_name = posixpath.join(CHROME_API, api_name)
50 return self._model_cache.GetFromFile(api_name) 52 return self._model_cache.GetFromFile(api_name)
51 53
52 assert not api_name.startswith(API) 54 assert not api_name.startswith(API) and not api_name.startswith(CHROME_API)
not at google - send to devlin 2014/03/07 01:04:52 ditto
Ken Rockot(use gerrit already) 2014/03/07 07:07:09 Done.
53 55
54 # API names are given as declarativeContent and app.window but file names 56 # API names are given as declarativeContent and app.window but file names
55 # will be declarative_content and app_window. 57 # will be declarative_content and app_window.
56 file_name = UnixName(api_name).replace('.', '_') 58 file_name = UnixName(api_name).replace('.', '_')
57 # Devtools APIs are in API/devtools/ not API/, and have their 59 # Devtools APIs are in API/devtools/ not API/, and have their
58 # "devtools" names removed from the file names. 60 # "devtools" names removed from the file names.
59 basename = posixpath.basename(file_name) 61 basename = posixpath.basename(file_name)
60 if 'devtools_' in basename: 62 if 'devtools_' in basename:
61 file_name = posixpath.join( 63 file_name = posixpath.join(
62 'devtools', file_name.replace(basename, 64 'devtools', file_name.replace(basename,
63 basename.replace('devtools_' , ''))) 65 basename.replace('devtools_' , '')))
64 66
65 futures = [self._model_cache.GetFromFile( 67 futures = [self._model_cache.GetFromFile(
66 posixpath.join(API, '%s.%s' % (file_name, ext))) 68 posixpath.join(api_path, '%s.%s' % (file_name, ext)))
67 for ext in ('json', 'idl')] 69 for ext in ('json', 'idl')
70 for api_path in (API, CHROME_API)]
68 def resolve(): 71 def resolve():
69 for future in futures: 72 for future in futures:
70 try: 73 try:
71 return future.Get() 74 return future.Get()
72 except FileNotFoundError: pass 75 except FileNotFoundError: pass
73 # Propagate the first FileNotFoundError if neither were found. 76 # Propagate the first FileNotFoundError if no files were found.
74 futures[0].Get() 77 futures[0].Get()
75 return Future(delegate=Gettable(resolve)) 78 return Future(delegate=Gettable(resolve))
76 79
77 def IterModels(self): 80 def IterModels(self):
78 future_models = [(name, self.GetModel(name)) for name in self.GetNames()] 81 future_models = [(name, self.GetModel(name)) for name in self.GetNames()]
79 for name, future_model in future_models: 82 for name, future_model in future_models:
80 try: 83 try:
81 model = future_model.Get() 84 model = future_model.Get()
82 except FileNotFoundError: 85 except FileNotFoundError:
83 continue 86 continue
84 if model: 87 if model:
85 yield name, model 88 yield name, model
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698