Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 posixpath | 5 import posixpath |
| 6 import traceback | 6 import traceback |
| 7 | 7 |
| 8 from future import Future | 8 from future import Future |
| 9 from path_util import ( | 9 from path_util import ( |
| 10 AssertIsDirectory, AssertIsValid, IsDirectory, IsValid, SplitParent, | 10 AssertIsDirectory, AssertIsValid, IsDirectory, IsValid, SplitParent, |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 | 148 |
| 149 def GetIdentity(self): | 149 def GetIdentity(self): |
| 150 '''The identity of the file system, exposed for caching classes to | 150 '''The identity of the file system, exposed for caching classes to |
| 151 namespace their caches. this will usually depend on the configuration of | 151 namespace their caches. this will usually depend on the configuration of |
| 152 that file system - e.g. a LocalFileSystem with a base path of /var is | 152 that file system - e.g. a LocalFileSystem with a base path of /var is |
| 153 different to that of a SubversionFileSystem with a base path of /bar, is | 153 different to that of a SubversionFileSystem with a base path of /bar, is |
| 154 different to a LocalFileSystem with a base path of /usr. | 154 different to a LocalFileSystem with a base path of /usr. |
| 155 ''' | 155 ''' |
| 156 raise NotImplementedError(self.__class__) | 156 raise NotImplementedError(self.__class__) |
| 157 | 157 |
| 158 def Walk(self, root, depth=-1): | 158 def Walk(self, root, depth=-1, walk_delegate=None): |
| 159 '''Recursively walk the directories in a file system, starting with root. | 159 '''Recursively walk the directories in a file system, starting with root. |
| 160 | 160 |
| 161 Behaviour is very similar to os.walk from the standard os module, yielding | 161 Behaviour is very similar to os.walk from the standard os module, yielding |
| 162 (base, dirs, files) recursively, where |base| is the base path of |files|, | 162 (base, dirs, files) recursively, where |base| is the base path of |files|, |
| 163 |dirs| relative to |root|, and |files| and |dirs| the list of files/dirs in | 163 |dirs| relative to |root|, and |files| and |dirs| the list of files/dirs in |
| 164 |base| respectively. If |depth| is specified and greater than 0, Walk will | 164 |base| respectively. If |depth| is specified and greater than 0, Walk will |
| 165 only recurse |depth| times. | 165 only recurse |depth| times. |
| 166 | 166 |
| 167 |walk_delegate|, if specified, should be a callback of signature | |
|
not at google - send to devlin
2014/08/29 19:28:31
Neat!
How about a more descriptive name than |wal
| |
| 168 | |
| 169 def my_walk_delegate(root):, | |
| 170 | |
| 171 which returns a tuple (dirs, files), where |dirs| is a list of directory | |
| 172 names under |root|, and |files| is a list of file names under |root|. | |
| 173 | |
| 167 Note that directories will always end with a '/', files never will. | 174 Note that directories will always end with a '/', files never will. |
| 168 | 175 |
| 169 If |root| cannot be found, raises a FileNotFoundError. | 176 If |root| cannot be found, raises a FileNotFoundError. |
| 170 For any other failure, raises a FileSystemError. | 177 For any other failure, raises a FileSystemError. |
| 171 ''' | 178 ''' |
| 172 AssertIsDirectory(root) | 179 AssertIsDirectory(root) |
| 173 basepath = root | 180 basepath = root |
| 174 | 181 |
| 175 def walk(root, depth): | 182 def walk(root, depth): |
| 176 if depth == 0: | 183 if depth == 0: |
| 177 return | 184 return |
| 178 AssertIsDirectory(root) | 185 AssertIsDirectory(root) |
| 179 dirs, files = [], [] | |
| 180 | 186 |
| 181 for f in self.ReadSingle(root).Get(): | 187 if walk_delegate: |
| 182 if IsDirectory(f): | 188 dirs, files = walk_delegate(root) |
| 183 dirs.append(f) | 189 else: |
| 184 else: | 190 dirs, files = [], [] |
| 185 files.append(f) | 191 for f in self.ReadSingle(root).Get(): |
| 192 if IsDirectory(f): | |
| 193 dirs.append(f) | |
| 194 else: | |
| 195 files.append(f) | |
| 186 | 196 |
| 187 yield root[len(basepath):].rstrip('/'), dirs, files | 197 yield root[len(basepath):].rstrip('/'), dirs, files |
| 188 | 198 |
| 189 for d in dirs: | 199 for d in dirs: |
| 190 for walkinfo in walk(root + d, depth - 1): | 200 for walkinfo in walk(root + d, depth - 1): |
| 191 yield walkinfo | 201 yield walkinfo |
| 192 | 202 |
| 193 for walkinfo in walk(root, depth): | 203 for walkinfo in walk(root, depth): |
| 194 yield walkinfo | 204 yield walkinfo |
| 195 | 205 |
| 196 def __eq__(self, other): | 206 def __eq__(self, other): |
| 197 return (isinstance(other, FileSystem) and | 207 return (isinstance(other, FileSystem) and |
| 198 self.GetIdentity() == other.GetIdentity()) | 208 self.GetIdentity() == other.GetIdentity()) |
| 199 | 209 |
| 200 def __ne__(self, other): | 210 def __ne__(self, other): |
| 201 return not (self == other) | 211 return not (self == other) |
| 202 | 212 |
| 203 def __repr__(self): | 213 def __repr__(self): |
| 204 return '<%s>' % type(self).__name__ | 214 return '<%s>' % type(self).__name__ |
| 205 | 215 |
| 206 def __str__(self): | 216 def __str__(self): |
| 207 return repr(self) | 217 return repr(self) |
| OLD | NEW |