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/path_canonicalizer.py

Issue 532903002: Docserver: Convert more future Get calls to Thens (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 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 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 from collections import defaultdict 5 from collections import defaultdict
6 import posixpath 6 import posixpath
7 7
8 from future import Future 8 from future import Future
9 from path_util import SplitParent 9 from path_util import SplitParent
10 from special_paths import SITE_VERIFICATION_FILE 10 from special_paths import SITE_VERIFICATION_FILE
(...skipping 20 matching lines...) Expand all
31 object_store_creator, 31 object_store_creator,
32 strip_extensions): 32 strip_extensions):
33 # |strip_extensions| is a list of file extensions (e.g. .html) that should 33 # |strip_extensions| is a list of file extensions (e.g. .html) that should
34 # be stripped for a path's canonical form. 34 # be stripped for a path's canonical form.
35 self._cache = object_store_creator.Create( 35 self._cache = object_store_creator.Create(
36 PathCanonicalizer, category=file_system.GetIdentity()) 36 PathCanonicalizer, category=file_system.GetIdentity())
37 self._file_system = file_system 37 self._file_system = file_system
38 self._strip_extensions = strip_extensions 38 self._strip_extensions = strip_extensions
39 39
40 def _LoadCache(self): 40 def _LoadCache(self):
41 cached_future = self._cache.GetMulti(('canonical_paths', 41 def resolve(cached):
ahernandez 2014/09/03 19:16:07 Nit: choose a better name for this, like load.
42 'simplified_paths_map'))
43
44 def resolve():
45 # |canonical_paths| is the pre-calculated set of canonical paths. 42 # |canonical_paths| is the pre-calculated set of canonical paths.
46 # |simplified_paths_map| is a lazily populated mapping of simplified file 43 # |simplified_paths_map| is a lazily populated mapping of simplified file
47 # names to a list of full paths that contain them. For example, 44 # names to a list of full paths that contain them. For example,
48 # - browseraction: [extensions/browserAction.html] 45 # - browseraction: [extensions/browserAction.html]
49 # - storage: [apps/storage.html, extensions/storage.html] 46 # - storage: [apps/storage.html, extensions/storage.html]
50 cached = cached_future.Get()
51 canonical_paths, simplified_paths_map = ( 47 canonical_paths, simplified_paths_map = (
52 cached.get('canonical_paths'), cached.get('simplified_paths_map')) 48 cached.get('canonical_paths'), cached.get('simplified_paths_map'))
53 49
54 if canonical_paths is None: 50 if canonical_paths is None:
55 assert simplified_paths_map is None 51 assert simplified_paths_map is None
56 canonical_paths = set() 52 canonical_paths = set()
57 simplified_paths_map = defaultdict(list) 53 simplified_paths_map = defaultdict(list)
58 for base, dirs, files in self._file_system.Walk(''): 54 for base, dirs, files in self._file_system.Walk(''):
59 for path in dirs + files: 55 for path in dirs + files:
60 path_without_ext, ext = posixpath.splitext(path) 56 path_without_ext, ext = posixpath.splitext(path)
61 canonical_path = posixpath.join(base, path_without_ext) 57 canonical_path = posixpath.join(base, path_without_ext)
62 if (ext not in self._strip_extensions or 58 if (ext not in self._strip_extensions or
63 path == SITE_VERIFICATION_FILE): 59 path == SITE_VERIFICATION_FILE):
64 canonical_path += ext 60 canonical_path += ext
65 canonical_paths.add(canonical_path) 61 canonical_paths.add(canonical_path)
66 simplified_paths_map[_Normalize(path, splittext=True)].append( 62 simplified_paths_map[_Normalize(path, splittext=True)].append(
67 canonical_path) 63 canonical_path)
68 # Store |simplified_paths_map| sorted. Ties in length are broken by 64 # Store |simplified_paths_map| sorted. Ties in length are broken by
69 # taking the shortest, lexicographically smallest path. 65 # taking the shortest, lexicographically smallest path.
70 for path_list in simplified_paths_map.itervalues(): 66 for path_list in simplified_paths_map.itervalues():
71 path_list.sort(key=lambda p: (len(p), p)) 67 path_list.sort(key=lambda p: (len(p), p))
72 self._cache.SetMulti({ 68 self._cache.SetMulti({
73 'canonical_paths': canonical_paths, 69 'canonical_paths': canonical_paths,
74 'simplified_paths_map': simplified_paths_map, 70 'simplified_paths_map': simplified_paths_map,
75 }) 71 })
76 else: 72 else:
77 assert simplified_paths_map is not None 73 assert simplified_paths_map is not None
78 74
79 return canonical_paths, simplified_paths_map 75 return canonical_paths, simplified_paths_map
76 return self._cache.GetMulti(('canonical_paths',
77 'simplified_paths_map')).Then(resolve)
80 78
81 return Future(callback=resolve)
82 79
83 def Canonicalize(self, path): 80 def Canonicalize(self, path):
84 '''Returns the canonical path for |path|. 81 '''Returns the canonical path for |path|.
85 ''' 82 '''
86 canonical_paths, simplified_paths_map = self._LoadCache().Get() 83 canonical_paths, simplified_paths_map = self._LoadCache().Get()
87 84
88 # Path may already be the canonical path. 85 # Path may already be the canonical path.
89 if path in canonical_paths: 86 if path in canonical_paths:
90 return path 87 return path
91 88
(...skipping 20 matching lines...) Expand all
112 max_prefix_length = len(_CommonNormalizedPrefix(max_prefix, path)) 109 max_prefix_length = len(_CommonNormalizedPrefix(max_prefix, path))
113 for path_for_file in potential_paths[1:]: 110 for path_for_file in potential_paths[1:]:
114 prefix_length = len(_CommonNormalizedPrefix(path_for_file, path)) 111 prefix_length = len(_CommonNormalizedPrefix(path_for_file, path))
115 if prefix_length > max_prefix_length: 112 if prefix_length > max_prefix_length:
116 max_prefix, max_prefix_length = path_for_file, prefix_length 113 max_prefix, max_prefix_length = path_for_file, prefix_length
117 114
118 return max_prefix 115 return max_prefix
119 116
120 def Cron(self): 117 def Cron(self):
121 return self._LoadCache() 118 return self._LoadCache()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698