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

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

Issue 13470005: Refactor the devserver to make it easier to control caching (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cduvall, rebase Created 7 years, 8 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
« no previous file with comments | « no previous file | chrome/common/extensions/docs/server2/api_data_source_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
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 copy 5 import copy
6 import logging 6 import logging
7 import os 7 import os
8 8
9 import compiled_file_system as compiled_fs
10 from file_system import FileNotFoundError 9 from file_system import FileNotFoundError
11 import third_party.json_schema_compiler.json_parse as json_parse 10 import third_party.json_schema_compiler.json_parse as json_parse
12 import third_party.json_schema_compiler.model as model 11 import third_party.json_schema_compiler.model as model
13 import third_party.json_schema_compiler.idl_schema as idl_schema 12 import third_party.json_schema_compiler.idl_schema as idl_schema
14 import third_party.json_schema_compiler.idl_parser as idl_parser 13 import third_party.json_schema_compiler.idl_parser as idl_parser
15 14
16 # Increment this version when there are changes to the data stored in any of 15 # Increment this if the data model changes for APIDataSource.
17 # the caches used by APIDataSource. This would include changes to model.py in 16 # This would include changes to model.py in json_schema_compiler!
18 # JSON schema compiler! This allows the cache to be invalidated without having 17 _VERSION = 17
19 # to flush memcache on the production server.
20 _VERSION = 16
21 18
22 def _RemoveNoDocs(item): 19 def _RemoveNoDocs(item):
23 if json_parse.IsDict(item): 20 if json_parse.IsDict(item):
24 if item.get('nodoc', False): 21 if item.get('nodoc', False):
25 return True 22 return True
26 to_remove = [] 23 to_remove = []
27 for key, value in item.items(): 24 for key, value in item.items():
28 if _RemoveNoDocs(value): 25 if _RemoveNoDocs(value):
29 to_remove.append(key) 26 to_remove.append(key)
30 for k in to_remove: 27 for k in to_remove:
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 """ 258 """
262 def __init__(self, api_name, samples): 259 def __init__(self, api_name, samples):
263 self._api_name = api_name 260 self._api_name = api_name
264 self._samples = samples 261 self._samples = samples
265 262
266 def get(self, key): 263 def get(self, key):
267 return self._samples.FilterSamples(key, self._api_name) 264 return self._samples.FilterSamples(key, self._api_name)
268 265
269 class APIDataSource(object): 266 class APIDataSource(object):
270 """This class fetches and loads JSON APIs from the FileSystem passed in with 267 """This class fetches and loads JSON APIs from the FileSystem passed in with
271 |cache_factory|, so the APIs can be plugged into templates. 268 |compiled_fs_factory|, so the APIs can be plugged into templates.
272 """ 269 """
273 class Factory(object): 270 class Factory(object):
274 def __init__(self, 271 def __init__(self, compiled_fs_factory, base_path):
275 cache_factory, 272 def create_compiled_fs(fn, category):
276 base_path): 273 return compiled_fs_factory.Create(
277 self._permissions_cache = cache_factory.Create(self._LoadPermissions, 274 fn, APIDataSource, category=category, version=_VERSION)
278 compiled_fs.PERMS, 275
279 version=_VERSION) 276 self._permissions_cache = create_compiled_fs(self._LoadPermissions,
280 self._json_cache = cache_factory.Create( 277 'permissions')
278
279 self._json_cache = create_compiled_fs(
281 lambda api_name, api: self._LoadJsonAPI(api, False), 280 lambda api_name, api: self._LoadJsonAPI(api, False),
282 compiled_fs.JSON, 281 'json')
283 version=_VERSION) 282 self._idl_cache = create_compiled_fs(
284 self._idl_cache = cache_factory.Create(
285 lambda api_name, api: self._LoadIdlAPI(api, False), 283 lambda api_name, api: self._LoadIdlAPI(api, False),
286 compiled_fs.IDL, 284 'idl')
287 version=_VERSION)
288 285
289 # These caches are used if an APIDataSource does not want to resolve the 286 # These caches are used if an APIDataSource does not want to resolve the
290 # $refs in an API. This is needed to prevent infinite recursion in 287 # $refs in an API. This is needed to prevent infinite recursion in
291 # ReferenceResolver. 288 # ReferenceResolver.
292 self._json_cache_no_refs = cache_factory.Create( 289 self._json_cache_no_refs = create_compiled_fs(
293 lambda api_name, api: self._LoadJsonAPI(api, True), 290 lambda api_name, api: self._LoadJsonAPI(api, True),
294 compiled_fs.JSON_NO_REFS, 291 'json-no-refs')
295 version=_VERSION) 292 self._idl_cache_no_refs = create_compiled_fs(
296 self._idl_cache_no_refs = cache_factory.Create(
297 lambda api_name, api: self._LoadIdlAPI(api, True), 293 lambda api_name, api: self._LoadIdlAPI(api, True),
298 compiled_fs.IDL_NO_REFS, 294 'idl-no-refs')
299 version=_VERSION) 295
300 self._idl_names_cache = cache_factory.Create(self._GetIDLNames, 296 self._idl_names_cache = create_compiled_fs(self._GetIDLNames, 'idl-names')
301 compiled_fs.IDL_NAMES, 297 self._names_cache = create_compiled_fs(self._GetAllNames, 'names')
302 version=_VERSION) 298
303 self._names_cache = cache_factory.Create(self._GetAllNames,
304 compiled_fs.NAMES,
305 version=_VERSION)
306 self._base_path = base_path 299 self._base_path = base_path
307 300
308 # These must be set later via the SetFooDataSourceFactory methods. 301 # These must be set later via the SetFooDataSourceFactory methods.
309 self._ref_resolver_factory = None 302 self._ref_resolver_factory = None
310 self._samples_data_source_factory = None 303 self._samples_data_source_factory = None
311 304
312 def SetSamplesDataSourceFactory(self, samples_data_source_factory): 305 def SetSamplesDataSourceFactory(self, samples_data_source_factory):
313 self._samples_data_source_factory = samples_data_source_factory 306 self._samples_data_source_factory = samples_data_source_factory
314 307
315 def SetReferenceResolverFactory(self, ref_resolver_factory): 308 def SetReferenceResolverFactory(self, ref_resolver_factory):
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 disable_refs).ToDict() 346 disable_refs).ToDict()
354 347
355 def _LoadIdlAPI(self, api, disable_refs): 348 def _LoadIdlAPI(self, api, disable_refs):
356 idl = idl_parser.IDLParser().ParseData(api) 349 idl = idl_parser.IDLParser().ParseData(api)
357 return _JSCModel( 350 return _JSCModel(
358 idl_schema.IDLSchema(idl).process()[0], 351 idl_schema.IDLSchema(idl).process()[0],
359 self._ref_resolver_factory.Create() if not disable_refs else None, 352 self._ref_resolver_factory.Create() if not disable_refs else None,
360 disable_refs).ToDict() 353 disable_refs).ToDict()
361 354
362 def _GetIDLNames(self, base_dir, apis): 355 def _GetIDLNames(self, base_dir, apis):
363 return [ 356 return self._GetExtNames(apis, ['idl'])
364 model.UnixName(os.path.splitext(api[len('%s/' % self._base_path):])[0])
365 for api in apis if api.endswith('.idl')
366 ]
367 357
368 def _GetAllNames(self, base_dir, apis): 358 def _GetAllNames(self, base_dir, apis):
369 return [ 359 return self._GetExtNames(apis, ['json', 'idl'])
370 model.UnixName(os.path.splitext(api[len('%s/' % self._base_path):])[0]) 360
371 for api in apis 361 def _GetExtNames(self, apis, exts):
372 ] 362 return [model.UnixName(os.path.splitext(api)[0]) for api in apis
363 if os.path.splitext(api)[1][1:] in exts]
373 364
374 def __init__(self, 365 def __init__(self,
375 permissions_cache, 366 permissions_cache,
376 json_cache, 367 json_cache,
377 idl_cache, 368 idl_cache,
378 json_cache_no_refs, 369 json_cache_no_refs,
379 idl_cache_no_refs, 370 idl_cache_no_refs,
380 names_cache, 371 names_cache,
381 idl_names_cache, 372 idl_names_cache,
382 base_path, 373 base_path,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 if self._disable_refs: 448 if self._disable_refs:
458 cache, ext = ( 449 cache, ext = (
459 (self._idl_cache_no_refs, '.idl') if (unix_name in idl_names) else 450 (self._idl_cache_no_refs, '.idl') if (unix_name in idl_names) else
460 (self._json_cache_no_refs, '.json')) 451 (self._json_cache_no_refs, '.json'))
461 else: 452 else:
462 cache, ext = ((self._idl_cache, '.idl') if (unix_name in idl_names) else 453 cache, ext = ((self._idl_cache, '.idl') if (unix_name in idl_names) else
463 (self._json_cache, '.json')) 454 (self._json_cache, '.json'))
464 return self._GenerateHandlebarContext( 455 return self._GenerateHandlebarContext(
465 cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)), 456 cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)),
466 path) 457 path)
OLDNEW
« no previous file with comments | « no previous file | chrome/common/extensions/docs/server2/api_data_source_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698