Index: chrome/common/extensions/docs/server2/file_system.py |
diff --git a/chrome/common/extensions/docs/server2/file_system.py b/chrome/common/extensions/docs/server2/file_system.py |
index 6686f4c99f2b10922daf84e89b05bf62ccf1cbe0..ddb50271742f49059c24dc2e2891b22fab934823 100644 |
--- a/chrome/common/extensions/docs/server2/file_system.py |
+++ b/chrome/common/extensions/docs/server2/file_system.py |
@@ -2,7 +2,10 @@ |
# Use of this source code is governed by a BSD-style license that can be |
# found in the LICENSE file. |
+import posixpath |
+ |
from future import Gettable, Future |
+from path_util import IsDirectory, SplitParent, ToDirectory |
class _BaseFileSystemException(Exception): |
@@ -77,6 +80,27 @@ class FileSystem(object): |
read_single = self.Read([path]) |
return Future(delegate=Gettable(lambda: read_single.Get()[path])) |
+ def Exists(self, path): |
+ '''Returns a Future to the existence of |path|; True if |path| exists, |
+ False if not. This method will not throw a FileNotFoundError unlike |
+ the Read* methods, however it may still throw a FileSystemError. |
+ |
+ There are several ways to implement this method via the interface but this |
+ method exists to do so in a canonical and most efficient way for caching. |
+ ''' |
+ if path in ('', '/'): |
+ # There is always a root directory. |
+ return Future(value=True) |
+ |
+ parent, base = SplitParent(path) |
+ list_future = self.ReadSingle(ToDirectory(parent)) |
+ def resolve(): |
+ try: |
+ return base in list_future.Get() |
+ except FileNotFoundError: |
+ return False |
+ return Future(delegate=Gettable(resolve)) |
+ |
def Refresh(self): |
'''Asynchronously refreshes the content of the FileSystem, returning a |
future to its completion. |
@@ -105,17 +129,22 @@ class FileSystem(object): |
def Walk(self, root): |
'''Recursively walk the directories in a file system, starting with root. |
- Emulates os.walk from the standard os module. |
- If the root cannot be found, raises a FileNotFoundError. |
+ Behaviour is very similar to os.walk from the standard os module, yielding |
+ (base, dirs, files) recursively, where |base| is the base path of |files|, |
+ |dirs| relative to |root|, and |files| and |dirs| the list of files/dirs in |
+ |base| respectively. |
+ |
+ Note that directories will always end with a '/', files never will. |
+ |
+ If |root| cannot be found, raises a FileNotFoundError. |
For any other failure, raises a FileSystemError. |
''' |
- basepath = root.rstrip('/') + '/' |
+ root = ToDirectory(root) # TODO(kalman): assert IsDirectory(root) |
+ basepath = root |
def walk(root): |
- if not root.endswith('/'): |
- root += '/' |
- |
+ assert IsDirectory(root), root |
dirs, files = [], [] |
for f in self.ReadSingle(root).Get(): |