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

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

Issue 10835012: Extension Docs Server: Include a list of samples used in the api reference page. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 4 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
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 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 json 5 import json
6 import logging 6 import logging
7 import os 7 import os
8 8
9 from handlebar_dict_generator import HandlebarDictGenerator 9 from handlebar_dict_generator import HandlebarDictGenerator
10 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater 10 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater
11 import third_party.json_schema_compiler.model as model 11 import third_party.json_schema_compiler.model as model
12 import third_party.json_schema_compiler.idl_schema as idl_schema 12 import third_party.json_schema_compiler.idl_schema as idl_schema
13 import third_party.json_schema_compiler.idl_parser as idl_parser 13 import third_party.json_schema_compiler.idl_parser as idl_parser
14 14
15 class APIDataSource(object): 15 class APIDataSource(object):
16 """This class fetches and loads JSON APIs from the FileSystem passed in with 16 """This class fetches and loads JSON APIs from the FileSystem passed in with
17 |cache_builder|, so the APIs can be plugged into templates. 17 |cache_builder|, so the APIs can be plugged into templates.
18 """ 18 """
19 def __init__(self, cache_builder, base_path): 19 class Factory(object):
20 def __init__(self, cache_builder, base_path):
21 self._cache_builder = cache_builder
22 self._base_path = base_path
23
24 def Create(self, samples):
25 return APIDataSource(self._cache_builder, self._base_path, samples)
26
27 def __init__(self, cache_builder, base_path, samples):
28 self._permissions_cache = cache_builder.build(self._LoadPermissions)
20 self._json_cache = cache_builder.build(self._LoadJsonAPI) 29 self._json_cache = cache_builder.build(self._LoadJsonAPI)
21 self._idl_cache = cache_builder.build(self._LoadIdlAPI) 30 self._idl_cache = cache_builder.build(self._LoadIdlAPI)
22 self._permissions_cache = cache_builder.build(self._LoadPermissions)
23 self._base_path = base_path 31 self._base_path = base_path
24 32 self._samples = samples
25 def _LoadJsonAPI(self, api):
26 generator = HandlebarDictGenerator(
27 json.loads(json_comment_eater.Nom(api))[0])
28 return generator.Generate()
29
30 def _LoadIdlAPI(self, api):
31 idl = idl_parser.IDLParser().ParseData(api)
32 generator = HandlebarDictGenerator(idl_schema.IDLSchema(idl).process()[0])
33 return generator.Generate()
34
35 def _LoadPermissions(self, perms_json):
36 return json.loads(json_comment_eater.Nom(perms_json))
37 33
38 def _GetFeature(self, path): 34 def _GetFeature(self, path):
39 # Remove 'experimental_' from path name to match the keys in 35 # Remove 'experimental_' from path name to match the keys in
40 # _permissions_features.json. 36 # _permissions_features.json.
41 path = path.replace('experimental_', '') 37 path = path.replace('experimental_', '')
42 try: 38 try:
43 perms = self._permissions_cache.GetFromFile( 39 perms = self._permissions_cache.GetFromFile(
44 self._base_path + '/_permission_features.json') 40 self._base_path + '/_permission_features.json')
45 api_perms = perms.get(path, None) 41 api_perms = perms.get(path, None)
46 if api_perms['channel'] == 'dev': 42 if api_perms['channel'] == 'dev':
47 api_perms['dev'] = True 43 api_perms['dev'] = True
48 return api_perms 44 return api_perms
49 except Exception: 45 except Exception:
50 return None 46 return None
51 47
48 def _LoadPermissions(self, perms_json):
49 return json.loads(json_comment_eater.Nom(perms_json))
50
51 def _LoadJsonAPI(self, api):
52 generator = HandlebarDictGenerator(
53 json.loads(json_comment_eater.Nom(api))[0], self._samples)
54 return generator.Generate()
55
56 def _LoadIdlAPI(self, api):
57 idl = idl_parser.IDLParser().ParseData(api)
58 generator = HandlebarDictGenerator(
59 idl_schema.IDLSchema(idl).process()[0], self._samples)
60 return generator.Generate()
61
52 def _AddPermissionsDict(self, api_dict, path): 62 def _AddPermissionsDict(self, api_dict, path):
53 return_dict = { 'permissions': self._GetFeature(path) } 63 return_dict = { 'permissions': self._GetFeature(path) }
54 return_dict.update(api_dict) 64 return_dict.update(api_dict)
55 return return_dict 65 return return_dict
56 66
57 def __getitem__(self, key): 67 def __getitem__(self, key):
58 return self.get(key) 68 return self.get(key)
59 69
60 def get(self, key): 70 def get(self, key):
61 path, ext = os.path.splitext(key) 71 path, ext = os.path.splitext(key)
62 unix_name = model.UnixName(path) 72 unix_name = model.UnixName(path)
63 json_path = unix_name + '.json' 73 json_path = unix_name + '.json'
64 idl_path = unix_name + '.idl' 74 idl_path = unix_name + '.idl'
65 try: 75 try:
66 return self._AddPermissionsDict(self._json_cache.GetFromFile( 76 return self._AddPermissionsDict(self._json_cache.GetFromFile(
67 self._base_path + '/' + json_path), path) 77 self._base_path + '/' + json_path), path)
68 except Exception: 78 except Exception:
69 try: 79 try:
70 return self._AddPermissionsDict(self._idl_cache.GetFromFile( 80 return self._AddPermissionsDict(self._idl_cache.GetFromFile(
71 self._base_path + '/' + idl_path), path) 81 self._base_path + '/' + idl_path), path)
72 except Exception as e: 82 except Exception as e:
73 logging.warn(e) 83 logging.warn(e)
74 raise 84 raise
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698