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

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

Issue 14125010: Docserver: Add support for viewing docs with a codereview patch applied (Closed) Base URL: https://src.chromium.org/svn/trunk/src/
Patch Set: Created 7 years, 7 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
Property Changes:
Added: svn:eol-style
+ LF
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 from file_system import FileSystem, StatInfo, FileNotFoundError
6 from future import Future
7 import logging
8
9 class _AsyncFetchFuture(object):
10 def __init__(self,
11 svn_files_future,
12 patched_files_future,
13 svn_dirs_future,
14 dir_paths,
15 patcher):
16 self._svn_files_future = svn_files_future
17 self._patched_files_future = patched_files_future
18 self._svn_dirs_future = svn_dirs_future
19 self._dir_paths = dir_paths
20 self._patcher = patcher
21
22 def Get(self):
23 value = self._svn_files_future.Get()
24 value.update(self._patched_files_future.Get())
25 dirs_value = self._svn_dirs_future.Get()
not at google - send to devlin 2013/04/30 15:37:42 maybe call these files/dirs rather than generic va
方觉(Fang Jue) 2013/05/01 15:27:25 Done.
26 value.update({path: self._patcher.PatchDirectoryListing(path,
27 dirs_value[path])
28 for path in self._dir_paths})
29 return value
30
31 class Patcher(object):
not at google - send to devlin 2013/04/30 15:37:42 I think it would be nicer to have this in a separa
32 # Returns (added_files, deleted_files, modified_files).
33 def GetPatchedFiles(self):
not at google - send to devlin 2013/04/30 15:37:42 comments should be like: def GetPatchedFiles(self
方觉(Fang Jue) 2013/05/01 15:27:25 Done.
34 raise NotImplementedError()
35
36 # Returns patch version.
37 def GetVersion(self):
38 raise NotImplementedError()
39
40 # Apply the patch to files. Returns Future with patched data.
41 def Apply(self, paths, file_system, binary):
42 raise NotImplementedError()
43
44 def GetPatchedChildrenInPath(self, path):
45 assert path.endswith('/')
46 def _FindChildrenInPath(files, path):
47 result = []
48 for f in files:
49 if f.startswith(path):
not at google - send to devlin 2013/04/30 15:37:42 something like: result = [f[len(path):] for f in
方觉(Fang Jue) 2013/05/01 15:27:25 Suppose a patch contains: c/c/e/docs/templates/pub
50 f = f.split(path, 1)[1]
51 if '/' in f:
52 f = f.split('/', 1)[0] + '/'
53 result.append(f)
54 return result
55
56 added_files, deleted_files, modified_files = (
57 tuple(_FindChildrenInPath(files) for files in self.GetPatchedFiles()))
not at google - send to devlin 2013/04/30 15:37:42 this method doesn't appear to return anything..?
方觉(Fang Jue) 2013/05/01 15:27:25 oops.
58
59 def PatchDirectoryListing(self, path, value):
not at google - send to devlin 2013/04/30 15:37:42 s/value/original_dir_listing/ or similar.
方觉(Fang Jue) 2013/05/01 15:27:25 Done.
60 added, deleted, modified = self.GetPatchedChildrenInPath(path)
61 return list(set(value) + set(added) - set(deleted))
62
63 def PatchStat(self, path, stat_info):
64 version = self.GetVersion()
not at google - send to devlin 2013/04/30 15:37:42 when would GetVersion return None? can we just gua
方觉(Fang Jue) 2013/05/01 15:27:25 When it fails to fetch issue data from Rietveld or
65 if version is None:
66 return stat_info
67 version = 'patched_' + version
not at google - send to devlin 2013/04/30 15:37:42 This kind of logic is FileSystem specific, doesn't
方觉(Fang Jue) 2013/05/01 15:27:25 It seems that only the three NotImplemented method
68 directory = path.rsplit('/', 1)[0]
69 added, deleted, modified = GetPatchedChildrenInPath(directory + '/')
70 existing_children = added + modified
71
not at google - send to devlin 2013/04/30 15:37:42 It's a bit unclear to me how the logic below relat
72 # path is added in the patch
73 if stat_info is None and len(existing_children) != 0:
not at google - send to devlin 2013/04/30 15:37:42 how can existing_children be non-empty if the path
74 if path.endswith('/'):
75 stat_info = StatInfo(version,
76 {child: version for child in existing_children})
not at google - send to devlin 2013/04/30 15:37:42 see comment at the end of this method. might as we
77 else:
78 filename = path.rsplit('/', 1)[-1]
not at google - send to devlin 2013/04/30 15:37:42 perhaps assign this at the same time you assign di
79 if filename in existing_children:
80 stat_info = StatInfo(version,
81 {child: version for child in existing_children})
82 else:
83 if len(existing_children) > 0:
not at google - send to devlin 2013/04/30 15:37:42 you're using > 0 here and != 0 above - nice to be
84 stat_info.version = version
85 stat_info.child_versions.update(
86 {child: version for child in existing_children})
87 elif len(deleted) > 0:
88 stat_info.version = version
not at google - send to devlin 2013/04/30 15:37:42 It looks like stat_info can be None here, if exist
89
90 return stat_info
not at google - send to devlin 2013/04/30 15:37:42 indentation looks incorrect here, the "if stat_inf
91
92 class PatchedFileSystem(FileSystem):
93 ''' Class to fetch resources with a patch applied.
94 '''
95 def __init__(self, svn_file_system, patcher):
96 self._svn_file_system = svn_file_system
97 self._patcher = patcher
98
99 def Read(self, paths, binary=False):
100 patched_files = []
101 for files in self._patcher.GetPatchedFiles():
102 patched_files += files
not at google - send to devlin 2013/04/30 15:37:42 patched_files = set(self._patcher.GetPatchedFiles(
方觉(Fang Jue) 2013/05/01 15:27:25 It doesn't work.
103 dir_paths = [path for path in paths if path.endswith('/')]
not at google - send to devlin 2013/04/30 15:37:42 assign directly into a set here? I suspect that fi
方觉(Fang Jue) 2013/05/01 15:27:25 Done.
104 file_paths = list(set(paths) - set(dir_paths))
105 patched_paths = list(set(file_paths) & set(patched_files))
not at google - send to devlin 2013/04/30 15:37:42 looks like you're converting from a set to a list
方觉(Fang Jue) 2013/05/01 15:27:25 Done.
106 unpatched_paths = list(set(file_paths) - set(patched_files))
107 return Future(delegate=_AsyncFetchFuture(
108 self._svn_file_system.Read(unpatched_paths, binary),
109 self._patcher.Apply(patched_paths, self._svn_file_system, binary),
110 self._svn_file_system.Read(dir_paths, binary),
111 dir_paths,
112 self._patcher))
113
114 def Stat(self, path):
115 try:
116 stat_info = self._svn_file_system.Stat(path)
117 except FileNotFoundError as e:
not at google - send to devlin 2013/04/30 15:37:42 The unfortunate part of writing it like this is th
方觉(Fang Jue) 2013/05/01 15:27:25 Done.
118 stat_info = None
119 stat_info = self._patcher.PatchStat(path, stat_info)
120 if stat_info is None:
121 raise FileNotFoundError('Path %s was not found' % path)
122 return stat_info
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698